Hello World
Installation
Using npm:
// Install full build
$ npm install --save @laces/laces
// Cherry-pick only what you need
$ npm install --save @laces/toSnakeCase
Use in Node.js:
// Load the full build
const laces = require('@laces/laces')
// Cherry-pick methods for smaller bundles
const toSnakeCase = require('@laces/toSnakeCase')
To contribute please read the contributing guide and code of conduct
endsWith
Determines if a string ends with a pattern
Arguments
string
(string): The string to query.pattern
(string): The pattern to match
Returns
(boolean): Returns true if the string ends with pattern and false otherwise
Examples
endsWith('my string', 'ing');
// returns true
endsWith('my string' 'eng');
// returns false
isAlpha
Determines if a string only contains characters in english alphabet
Arguments
string
(string): The string to query.
Returns
(boolean): Returns true if the string only contains alpha characters
Examples
isAlpha('my string');
// returns true
isAlpha('my-string');
// returns false
isAlpha('555-5555');
// returns false
isAlphaNumeric
Determines if a string only contains characters in english alphabet or numbers
Arguments
string
(string): The string to query.
Returns
(boolean): Returns true if the string only contains alpha characters or numbers
Examples
isAlphaNumeric('my string');
// returns true
isAlphaNumeric('5555555');
// returns true
isAlphaNumeric('my number 5555555');
// returns true
isAlphaNumeric('my-string');
// returns false
isBetween
Determines if a string is surrounded by a left and right pattern
Arguments
string
(string): The string to query.[leftPattern='']
(string): The pattern to match at the beginning of the string.[rightPattern='']
(string): The pattern to match at the end of the string.
Returns
(boolean): Returns true if the string is surrounded by patterns
Examples
isBetween('my string');
// returns true
isBetween('my string', 'my');
// returns true
isBetween('my string', 'my', 'ing');
// returns true
isBetween('my string', '', 'eng');
// returns false
isBlank
Determines if a string only contains whitespace
Arguments
[string=null]
(string): The string to query.
Returns
(boolean): Returns true if the string only contains whitespace
Examples
isBlank('');
// returns true
isBlank(' ');
// returns true
isBlank('\n');
// returns true
isBlank('my string');
// returns false
isEmpty
Determines if a string contains no characters
Arguments
[string='']
(string): The string to query.
Returns
(boolean): Returns true if the string contains no characters
Examples
isEmpty('');
// returns true
isEmpty(' ');
// returns false
isInteger
Determines if a string can be represented as a valid integer
Arguments
string
(string): The string to query.
Returns
(boolean): Returns true if the string represents a valid integer
Examples
isInteger('1');
// returns true
isInteger('-1');
// returns true
isInteger('1.5');
// returns false
isInteger('my string');
// returns false
isMatch
Constant time equality check of two strings
Arguments
subject
(string): The subject string.compareTo
(string): The string to compare to the subject string.
Returns
(boolean): Returns true if the string are equal
Examples
isMatch('password', 'password');
// returns true
isMatch('password', 'wrong password');
// returns false
isNaturalNumber
Determines if a string represents a natural number
Arguments
string
(string): The string to query.
Returns
(boolean): Returns true if the string represents a valid natural number
Examples
isBlank('1');
// returns true
isBlank('-1');
// returns false
isBlank('1.5');
// returns false
isBlank('my string');
// returns false
isNumeric
Determines if a string represents a valid number
Arguments
string
(string): The string to query.[config=defaultConfig]
(Object): Configuration to specify what numbers sets to include
Returns
(boolean): Returns true if the string represents a valid number
Examples
isNumeric('1');
// returns true
isNumeric('-1');
// returns true
isNumeric('1.5');
// returns true
isNumeric('-1', { integers: false });
// returns false
isNumeric('1.5', { real: false });
// returns false
isNumeric('my string');
// returns false
isReal
Determines if a string represents a valid real number
Arguments
string
(string): The string to query.
Returns
(boolean): Returns true if the string represents a valid real number
Examples
isNumeric('1');
// returns true
isNumeric('-1');
// returns true
isNumeric('1.5');
// returns true
isNumeric('my string');
// returns false
startsWith
Determines if a string starts with a pattern
Arguments
string
(string): The string to query.pattern
(string): The pattern to match
Returns
(boolean): Returns true if the string starts with pattern and false otherwise
Examples
startsWith('my string' 'my');
// returns true
startsWith('my string', 'ing');
// returns false
camalize
Creates a camel cased string.
Arguments
string
(string): The string to camel case.
Returns
(string): Returns the camel cased string
Examples
camalize('my string');
// returns 'myString'
capitalize
Creates a capitalized string.
Arguments
string
(string): The string to capitalize.
Returns
(string): Returns the capitalized string
Examples
capitalize('my string');
// returns 'My string'
charAt
Returns a character at a specified index.
Arguments
string
(string): The string to retrieve a character from.[index=0]
(number): Position in string.
Returns
(string): Returns chain function
Examples
charAt('my string');
// returns 'm'
charAt('my string', 1);
// returns 'y'
chompLeft
Removes a pattern from the beginning of a string.
Arguments
string
(string): The string to remove the pattern from.pattern
(string): Pattern to remove.
Returns
(string): Returns a string that does not start with the pattern
Examples
chompLeft('my string', 'my');
// returns 'string'
chompLeft('my string', 'non-matching-pattern');
// returns 'my string'
chompRight
Removes a pattern from the end of a string.
Arguments
string
(string): The string to remove the pattern from.pattern
(string): Pattern to remove.
Returns
(string): Returns a string that does not end with the pattern
Examples
chompRight('my string', 'string');
// returns 'my'
chompRight('my string', 'non-matching-pattern');
// returns 'my string'
clamp
Truncates a string with a specified pattern
Arguments
string
(string): The string to truncate.length
(number): Max string length before applying clamp.[pattern='...']
(string): Pattern to append to the string.
Returns
(string): Returns a string that may be truncated
Examples
clamp('my string', 1);
// returns 'my...'
clamp('my string', 1, '(...)');
// returns 'my(...)'
collapse
Removes whitespace from a string
Arguments
string
(string): String to remove whitespace from.
Returns
(string): Returns a string without whitespace
Examples
collapse(' my string ');
// returns 'mystring'
collapse('\tmy string\n');
// returns 'mystring'
escapeHtml
Replaces HTML characters with their HTML entities
Arguments
string
(string): The string to replace HTML characters.
Returns
(string): Returns string with HTML characters replaced with corresponding HTML entities
Examples
escapeHtml('<p>my string</p>');
// returns '<p>my string</p>'
escapeHtml('my string');
// returns 'my string'
escapeRegex
Escapes Regex special characters
Arguments
string
(string): The string to escape regex characters.
Returns
(string): Returns string with escaped regex characters.
Examples
endsWith('.*');
// returns '\.\*'
endsWith('my string');
// returns 'my string'
latinise
Replaces Latin characters with corresponding English characters
Arguments
string
(string): The string to replace Latin characters from.
Returns
(string): Returns string with replaced Latin characters.
Examples
endsWith('crème brûlée');
// returns 'creme brulee'
endsWith('my string');
// returns 'my string'
padLeft
Append a character n times to the string
Arguments
string
(string): The string to pad[amount=0]
(number): The number of times a character is appended[character=' ']
(string): The string to prepend
Returns
(string): Returns string with a character appended n times
Examples
padLeft('my string');
// returns 'my string'
padLeft('my string', 1);
// returns ' my string'
padLeft('my string', 1, '.');
// returns '.my string'
padRight
Prepend a character n times to the string
Arguments
string
(string): The string to pad[amount=0]
(number): The number of times a character is prepended[character=' ']
(string): The string to prepend
Returns
(string): Returns string with a character prepended n times
Examples
padRight('my string');
// returns 'my string'
padRight('my string', 1);
// returns ' my string'
padRight('my string', 1, '.');
// returns '.my string'
remove
Remove all occurrences of a pattern from a string
Arguments
string
(string): The string remove the pattern from[pattern='']
(string): The pattern to omit
Returns
(string): Returns string with the pattern omitted
Examples
remove('my string');
// returns 'my string'
remove('my string', 'my');
// returns ' string'
remove('my string', 'string');
// returns 'my '
remove('my my string', 'my ');
// returns 'string'
removeFirst
Remove first occurrence of a pattern from a string
Arguments
string
(string): The string remove the pattern from[pattern='']
(string): The pattern to omit
Returns
(string): Returns string with the pattern omitted
Examples
remove('my string');
// returns 'my string'
remove('my string', 'my');
// returns ' string'
remove('my string', 'string');
// returns 'my '
remove('my my string', 'my ');
// returns 'my string'
repeat
Concat a string to itself n times
Arguments
string
(string): The string to repeat[amount=1]
(number): The number of times to repeat
Returns
(string): Returns repeated string
Examples
repeat('my string');
// returns 'my string'
repeat('my string', 0);
// returns ''
repeat('my string', 2);
// returns 'my stringmy string'
replace
Replace a pattern in a string
Arguments
string
(string): The string that contains the pattern to replace.pattern
(string): The pattern to matchstrategy
(): A replacement string or function that returns a string
Returns
(string): Returns string with pattern replaced with strategy
Examples
replace('my string');
// returns 'my string'
replace('my string', 'string', 'replacement');
// returns 'my replacement'
replace('my string', 'string', (match) => match.toUppercase());
// returns 'my String'
reverse
Reverse a string. Supports reversing UTF-16 strings.
Arguments
string
(string): The string to reverse.
Returns
(string): Returns a reversed string
Examples
reverse('gnirts ym');
// returns 'my string'
reverse('mañana mañana');
// returns 'anañam anañam'
slugify
Convert a string to a valid URL
Arguments
[string='']
(string): The string to convert to a valid URL
Returns
(string): Returns string that represents a valid URL
Examples
repeat('my string');
// returns 'my-string'
template
Python style template strings
Arguments
string
(string): Template string.args
(): Strings to inject into the template
Returns
(string): Returns a string with only the last n characters
Examples
template('my {0} string', 'templated');
// returns 'my templated string'
template('my {0} string{1}', 'templated', '!');
// returns 'my templated string!'
template('{0} oh {0} a {1} string{2}', 'my', 'templated', '!')
// returns 'my oh my a templated string!'
takeFirst
Create a string that only contains first n characters
Arguments
string
(string): The string to remove characters from.[amount=1]
(number): The number of characters to include.
Returns
(string): Returns a string with only the first n characters
Examples
takeFirst('my string');
// returns 'm'
takeFirst('my string' 2);
// returns 'my'
takeLast
Create a string that only contains last n characters
Arguments
string
(string): The string to remove characters from.[amount=1]
(number): The number of characters to include.
Returns
(string): Returns a string with only the last n characters
Examples
takeLast('my string');
// returns 'g'
takeLast('my string' 2);
// returns 'ng'
takeWhile
Include characters in a string that match the predicate
Arguments
string
(string): The string to remove characters from.predicate
(function): Receives each character of string as argument.
Returns
(string): Returns a string that contains characters that meet predicate.
Examples
takeWhile('my string');
// returns 'my string'
takeWhile('my string' character => character !== ' ');
// returns 'mystring'
toCamelCase
Convert to camel case
Arguments
string
(string): The string to convert to camel case.
Returns
(string): Returns a camel cased string.
Examples
toCamelCase('my string');
// returns 'myString'
toCamelCase('my string');
// returns 'my string'
toKebabCase
Convert to kebab case
Arguments
string
(string): The string to convert to kebab case.
Returns
(string): Returns a kebab cased string.
Examples
endsWith('my string');
// returns 'my-string'
endsWith('myString');
// returns 'my-string'
endsWith('my_string');
// returns 'my-string'
toLowerCase
Convert to lower case
Arguments
string
(string): The string to convert to lower case.
Returns
(string): Returns a lower cased string.
Examples
toLowerCase('My String');
// returns 'my string'
toSnakeCase
Convert to snake case
Arguments
string
(string): The string to convert to snake case.
Returns
(string): Returns a snake cased string.
Examples
endsWith('my string');
// returns 'my_string'
endsWith('myString');
// returns 'my_string'
endsWith('my-string');
// returns 'my_string'
toSpaceCase
Convert to space case
Arguments
string
(string): The string to convert to space case.
Returns
(string): Returns a space cased string.
Examples
endsWith('myString');
// returns 'my string'
endsWith('myString');
// returns 'my string'
endsWith('my-string');
// returns 'my string'
toUpperCase
Convert to upper case
Arguments
string
(string): The string to convert to upper case.
Returns
(string): Returns a upper cased string.
Examples
toUpperCase('My String');
// returns 'MY STRING'
trim
Remove a pattern from the beginning and end of a string
Arguments
string
(string): The string to remove the pattern from.[pattern=' ']
(string): The pattern to remove.
Returns
(string): Returns a string with the pattern omitted from the beginning and end.
Examples
toLowerCase('my string');
// returns 'my string'
toLowerCase('Xmy stringX', 'X');
// returns 'my string'
trimLeft
Remove a pattern from the beginning of a string
Arguments
string
(string): The string to remove the pattern from.[pattern=' ']
(string): The pattern to remove.
Returns
(string): Returns a string with the pattern omitted from the beginning.
Examples
toLowerCase('my string');
// returns 'my string'
toLowerCase('Xmy stringX', 'X');
// returns 'my stringX'
trimRight
Remove a pattern from the end of a string
Arguments
string
(string): The string to remove the pattern from.[pattern=' ']
(string): The pattern to remove.
Returns
(string): Returns a string with the pattern omitted from the end.
Examples
toLowerCase('my string');
// returns 'my string'
toLowerCase('Xmy stringX', 'X');
// returns 'Xmy string'
split
Group sections of a string, using a delimiter, into an array
Arguments
string
(string): The string to convert to an array[pattern=' ']
(string): The number of times to repeat
Returns
(Array): Returns array of substrings
Examples
repeat('my string');
// returns ['my', 'string']
repeat('AA_BB_CC', '_');
// returns ['AA', 'BB', 'CC']
toChars
Convert to an array of characters
Arguments
string
(string): The string to convert to an array.
Returns
(): Returns an array of characters from the string.
Examples
toLowerCase('my string');
// returns ['m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g']
toCodePoints
Convert to an array of code points
Arguments
string
(string): The string to convert to an array.
Returns
(): Returns an array of code points from the string.
Examples
toLowerCase('😀 awesome');
// returns [55357, 56832, 32, 97, 119, 101, 115, 111, 109, 101]
toLines
Create an array of lines
Arguments
string
(string): The string to convert to lines.[delimiter='\n']
(string): The pattern to split lines on.
Returns
(): Returns a kebab cased string.
Examples
toLines('first line\n second line');
// returns ['first line', 'second line']
toWords
Convert to an array of words
Arguments
string
(string): The string to convert to an array.
Returns
(): Returns an array of words from the string
Examples
toLowerCase('My sentence is great');
// returns ['my', 'sentence', 'is', 'great']
chain
Creates a fluent interface to chain stringy operations together.
Arguments
string
(string): The string to chain operations from.
Returns
(Chain): Returns Chain
Examples
chain('my string')
.value();
// returns 'my string'
chain('my string')
.toCamelCase()
.capitalize()
.repeat(2)
.value();
// returns 'MyStringMyString'