#String
DESCRIPTION
General
Installation
npm install eden-string
Usage
var string = require('eden-string');
Methods
addSlashes
string addSlashes('I can't wait.');
Returns a string with backslashes before characters that need to be escaped.
Parameters
- 'I can't wait.' - string with apostrophe(')
Returns
string
Example
Code
string().addSlashes('I can't wait.');
Outputs
'I can\\'t wait.'
base64Decode
string base64Decode('dGVzdA==');
Decodes a base 64 string
Parameters
- 'dGVzdA==' - encoded base 64 string
Returns
string
Example
Code
string().base64Decode('dGVzdA==');
Outputs
'test'
base64Encode
string base64Encode('test');
Encodes a base 64 string
Parameters
- 'test' - convert to base 64 string
Returns
string
Example
Code
string().base64Encode('test');
Outputs
'dGVzdA=='
camelize
string camelize('this-is-sparta', sparta);
Camelizes a string
Parameters
'this-is-sparta' - string with a dash (-) in between
sparta - string (prefix)
Returns
string
Example
Code
string().camelize('this-is-sparta','sparta');
string().camelize('this-is-sparta');
Outputs
'this-Is-'
'thisIsSparta'
charAt
string charAt('You cant find me', 7);
Returns the character at the specified index
Parameters
'You cant find me' - string
7 - number (specified index)
Returns
string
Example
Code
string().charAt('You cant find me', 7);
Outputs
't'
charCodeAt
string charCodeAt('You cant find me', 7);
Returns the Unicode of the character at the specified index
Parameters
'You cant find me' - string
7 - number (specified index)
Returns
string
Example
Code
string().charCodeAt('You cant find me', 7);
Outputs
116
concat
string concat('You cant find me', 'Yes I can');
Joins two or more strings
Parameters
- 'You cant find me', 'Yes I can' - 2 or more strings, separated by comma (,)
Returns
string
Example
Code
string().concat('You cant find me', 'Yes I can');
Outputs
'You cant find meYes I can'
dasherize
string dasherize('This Is Sparta');
Transforms a string with caps and space to a lower case dash string
Parameters
- 'This Is Sparta' - string to be transform with caps and space to a lower case dash string
Returns
string
Example
Code
string().dasherize('This Is Sparta');
Outputs
'this-is-sparta'
endsWith
bool endsWith('To be or not to be that is the question', 'question');
Returns true if string ends with given value
Parameters
'To be or not to be that is the question' - string
'question' - end word of the string that is the same with the given value
Returns
bool
Example
Code
string().endsWith('To be or not to be that is the question', 'question');
Outputs
true
equals
bool equals('brown fox', 'brown fox', true);
Returns true if given value equals string
Parameters
'brown fox' - string
'brown fox' - mixed (same string as the first parameter)
true - bool (strict mode)
Returns
bool
Example
Code
string().equals('brown fox', 'brown fox', true);
Outputs
true
has
bool has('thisIsSparta', 'isIs');
Returns true if specified value is found within the string
Parameters
'thisIsSparta' - string (with or without whitespace)
'isIs' - string (string within the string)
Returns
bool
Example
Code
string().has('thisIsSparta', 'isIs');
string().has('this is sparta', 's spa');
string().has('this is sparta', 'issparta');
Outputs
true
true
false
hashToQuery
string hashToQuery({test2:6});
Returns query string from the given object
Parameters
- {test2:6} - any kind of object
Returns
string
Example
Code
string().hashToQuery({test2:6});
string().hashToQuery({test3:9});
Outputs
'test2=6'
'test3=9'
hmac
string hmac('test', '1234', sha1', 'hex');
hmac encryption
Parameters
'test' - string
'1234' - string
'sha1' - string (sha1|md5|sha256)
'hex' - string (binary|hex|base64)
Returns
string
Example
Code
string().hmac('test', '1234', 'sha1', 'hex');
Outputs
'6455fdb217cfe086953a844dabac0491b05d91d2'
htmlEntities
string htmlEntities(Number, String, String);
Convert all applicable characters to HTML entities
Parameters
number
string
string
Returns
string
Example
Code
string().htmlEntities('4 > 6 & 5');
Outputs
'4 < 6 & 5'
htmlEntityDecode
string htmlEntityDecode(String, Number);
Convert all HTML entities to their applicable characters
Parameters
string
number
Returns
string
Example
Code
string().htmlEntityDecode('4 < 6 & 5');
Outputs
'4 < 6 & 5'
indexOf
number indexOf('You cant find me', 't');
Returns the position of the first found occurrence of a specified value in a string
Parameters
'You cant find me' - string
't' - string (specified value in a string)
Returns
number
Example
Code
string().indexOf('You cant find me', 't');
string().indexOf('You cant find me', 'in');
Outputs
7
10
isJson
bool isJson('{"test1":4}');
Returns true if string is JSON formatted
Parameters
- '{"test1":4}' - string (string in JSON format)
Returns
bool
Example
Code
string().isJson('{"test1":4}');
string().isJson('{test1:4}');
Outputs
true
false
isString
bool isString('I got you');
Returns true if given is a string
Parameters
- 'I got you' - mixed (2 or more strings / strings with numbers)
Returns
bool
Example
Code
string().isString('I got you');
string().isString('Hello 1234');
Outputs
true
true
isEmpty
bool isEmpty('');
Returns true if string is empty
Parameters
- ' ' - string (checks if the string is empty)
Returns
bool
Example
Code
string().isEmpty("");
string().isEmpty('Hi');
Outputs
true
false
jsonToHash
object jsonToHash(String);
Converts a JSON string to an object
Parameters
- string
Returns
object
Example
Code
string().jsonToHash('{"test1":4}').test1);
Outputs
4
lastIndexOf
number lastIndexOf('You cant find me', 'n');
Returns the position of the last found occurrence of a specified value in a string
Parameters
'You cant find me' - string
'n' - string (specified value to find the last occurrence)
Returns
number
Example
Code
string().lastIndexOf('You cant find me', 'n');
string().lastIndexOf('I got you', 'you');
Outputs
11
6
lcFirst
string lcFirst('THIS IS SPARTA');
Lowercase the first letter of the string
Parameters
- 'THIS IS SPARTA' - string (string must be all in uppercase)
Returns
string
Example
Code
string().lcFirst('THIS IS SPARTA');
Outputs
'tHIS IS SPARTA'
nl2br
string nl2br('THIS\nIS\nSPARTA');
Inserts HTML line breaks before all newlines in a string
Parameters
- 'THIS\nIS\nSPARTA' - string (place (\n) between the given string)
Returns
string
Example
Code
string().nl2br('THIS\nIS\nSPARTA');
Outputs
'THIS<br />IS<br />SPARTA'
match
array match('The rain in SPAIN stays mainly in the plain', /ain/g);
Searches for a match between a regular expression and a string, and returns the matches
Parameters
'The rain in SPAIN stays mainly in the plain' - string
/ain/g - RegExp (global searching for a match with string)
Returns
array
Example
Code
string().match('The rain in SPAIN stays mainly in the plain', /ain/g);
Outputs
'ain,ain,ain'
md5
string md5('test');
Calculate the md5 hash of a string
Parameters
- 'test' - any string to be calculated to md5 hash
Returns
string
Example
Code
string().md5('test');
Outputs
'098f6bcd4621d373cade4e832627b4f6'
pathToArray
string pathToArray('/some/path/to/file');
Converts a path into an array
Parameters
- '/some/path/to/file' - string (path)
Returns
string
Example
Code
string().pathToArray('/some/path/to/file');
string().pathtoArray('/some/path/to/file').length);
Outputs
['some','path','to','file']
4
pathToQuery
object pathToQuery('/some/path/to/file?test1=4');
Formats a path with query to an object
Parameters
- '/some/path/to/file?test1=4' - string (path)
Returns
object
Example
Code
string().pathToQuery('/some/path/to/file?test1=4');
string().pathToQuery('/some/path/to/file?test1=4').test1);
Outputs
{'test1':4}
4
queryToHash
object queryToHash('test1=4&test2=6');
Converts a query string to an object
Parameters
- 'test1=4&test2=6' - any string to be converted as query(object)
Returns
object
Example
Code
string().queryToHash('test1=4&test2=6');
string().queryToHash('test1=4&test2=6').test2);
Outputs
{'test1':4,'test2':6}
6
replace
string replace('This is Sparta', 'i', 'a');
Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
Parameters
'This is Sparta' - string
'i', 'a' - string|RegExp (search for a match between a substring an a string, replaces the matched substring with a new substring)
Returns
string
Example
Code
string().replace('This is Sparta', 'i', 'a');
Outputs
'Thas as Sparta'
startsWith
bool startsWith('Hello', 'He');
Returns true if string starts with given value
Parameters
'Hello' - string
'He' - string (same starting value with given value)
Returns
bool
Example
Code
string().startsWith('Hello', 'He');
Outputs
true
search
number search('You cant find me', 't');
Searches for a match between a regular expression and a string, and returns the position of the match
Parameters
'You cant find me' - string
't' - string|RegExp (specified character or a regular expression)
Returns
number
Example
Code
string().search('You cant find me', 't');
Outputs
7
sha1
string sha1('test');
Calculate the sha1 hash of a string
Parameters
- 'test' - string to be calculated to sha1 hash
Returns
string
Example
Code
string().sha1('test');
Outputs
'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'
size
number size('qwert');
Returns the size of the string
Parameters
- 'qwert' - string
Returns
number
Example
Code
string().size('qwert');
Outputs
5
slice
string slice('The rain in SPAIN stays mainly in the plain', 10, 15);
Extracts a part of a string and returns a new string
Parameters
'The rain in SPAIN stays mainly in the plain' - string
10 - number (specified start position)
15 - [number](specified end position)
Returns
string
Example
Code
string().slice('The rain in SPAIN stays mainly in the plain', 10, 15);
Outputs
'n SPA'
split
array split('The rain in SPAIN stays mainly in the plain', 'ain');
Splits a string into an array of substrings
Parameters
'The rain in SPAIN stays mainly in the plain' - any string to be splited into an array of substrings
'ain' - string
Returns
array
Example
Code
string().split('The rain in SPAIN stays mainly in the plain', 'ain');
string().split('The rain in SPAIN stays mainly in the plain', 'ain').length);
Outputs
['The r', 'in SPAIN stays m', 'ly in the pl','']
4
stripSlashes
string stripSlashes('I can\\'t wait');
Returns a string with backslashes stripped off.
Parameters
- 'I can\'t wait' - string (string with backlashes before an apostrophe('))
Returns
string
Example
Code
string().stripSlashes('I can\\'t wait');
Outputs
'I can't wait'
substr
string substr('The rain in SPAIN stays mainly in the plain', 10, 5);
Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
Parameters
'The rain in SPAIN stays mainly in the plain' - any string to be extract
10 - number (specified starting position)
5 - number (specified number of character to be extracted)
Returns
string
Example
Code
string().substr('The rain in SPAIN stays mainly in the plain', 10, 5);
Outputs
'n SPA'
substring
string substring('The rain in SPAIN stays mainly in the plain', 10, 15);
Extracts the characters from a string, between two specified indices
Parameters
'The rain in SPAIN stays mainly in the plain' - string
10 - number (specified index)
5 - number (specified index)
Returns
string
Example
Code
string().substring('The rain in SPAIN stays mainly in the plain', 10, 15);
Outputs
'n SPA'
stripTags
string stripTags(String, String);
Strips HTML out of string
Parameters
string
string
Returns
string
Example
Code
string().stripTags("THIS<br />IS<br />SPARTA");
Outputs
'THISISSPARTA'
summarize
string summarize('The rain in SPAIN stays mainly in the plain',4);
Summarizes a text
Parameters
'The rain in SPAIN stays mainly in the plain' - string
4 - int (number of words)
Returns
string
Example
Code
string().summarize('The rain in SPAIN stays mainly in the plain', 4);
Outputs
'The rain in SPAIN'
titlize
string titlize(String, String);
Titlizes a string
Parameters
string
string - prefix
Returns
string
Example
Code
string().titlize('this-is-sparta');
Outputs
'This Is Sparta'
toLowerCase
string toLowerCase('This Is Sparta');
Converts a string to lowercase letters
Parameters
- 'This Is Sparta' - any string to be converted to lowercase letters
Returns
string
Example
Code
string().toLowerCase('This Is Sparta');
Outputs
'this is sparta'
toHash
object toHash('test1=4&test2=6');
Converts either a JSON string or a query string to an object
Parameters
- 'test1=4&test2=6' - any string to be converted either a JSON string or a query string to an object
Returns
object
Example
Code
string().toHash('test1=4&test2=6');
string().toHash('{"test1":4}');
string().toHash('test1=4&test2=6').test2);
string().toHash('{"test1":4}').test1);
Outputs
{'test1':4,'test2':6}
{'test1':4}
6
4
toPath
string toPath('some/path//to/file/';
Formats a string into a proper path
Parameters
- 'some/path//to/file/' - string (path)
Returns
string
Example
Code
string().toPath('some/path//to/file/');
Outputs
'/some/path/to/file'
toUpperCase
string toUpperCase('This Is Sparta');
Converts a string to uppercase letters
Parameters
- 'This Is Sparta' - string to be converted to uppercase letters
Returns
string
Example
Code
string().toUpperCase('This Is Sparta');
Outputs
'THIS IS SPARTA'
trim
string trim(' This Is Sparta ');
Removes whitespace from both ends of a string
Parameters
- ' This Is Sparta ' - string (string with whitespaces from both ends)
Returns
string
Example
Code
string().trim(' This Is Sparta ');
Outputs
'This Is Sparta'
ucFirst
string ucFirst('this is sparta');
Capitalize the first letter of the string
Parameters
- 'this is sparta' - string (the first letter to be capitalized)
Returns
string
Example
Code
string().ucFirst('this is sparta');
Outputs
'This is sparta'
ucWords
string ucWords('this is sparta');
Uppercase the first character of each word in a string
Parameters
- 'this is sparta' - string (first character of each word in a string is to be uppercase)
Returns
string
Example
Code
string().ucWords('this is sparta');
Outputs
'This Is Sparta'
uid
string uid('test');
Returns a base 51 unique id
Parameters
- string
Returns
string
Example
Code
string().uid('test');
Outputs
'aZblbFaobzaQabwibhae'
uncamelize
string uncamelize('this Is Sparta', 'helloworld');
Uncamelizes a string
Parameters
'this Is Sparta'- string
'helloworl - string (prefix)
Returns
string
Example
Code
string().uncamelize('thisIsSparta');
Outputs
'this helloworldis helloworldsparta'
urlDecode
string urlDecode('my%20test.asp?name=st%C3%A5le&car=saab');
Decodes URI version
Parameters
- 'my%20test.asp?name=st%C3%A5le&car=saab' - string (to be decoded as URI version)
Returns
string
Example
Code
string().urlDecode('my%20test.asp?name=st%C3%A5le&car=saab');
Outputs
'my test.asp?name=ståle&car=saab'
urlEncode
string urlEncode('my test.asp?name=ståle&car=saab');
Returns the encoded URI version
Parameters
- 'my test.asp?name=ståle&car=saab' - string (to be encoded as URI version)
Returns
string
Example
Code
string().urlEncode('my test.asp?name=ståle&car=saab');
Outputs
'my%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab'
utf8Encode
string utf8Encode('Kevin van Zonneveld');
Encodes an ISO-8859-1 string to UTF-8
Parameters
- 'Kevin van Zonneveld' - string to be encoded to UTF-8
Returns
string
Example
Code
string().utf8Encode('Kevin van Zonneveld');
Outputs
'Kevin van Zonneveld'
uuid
string uuid('test');
Generates a version 4 or 5 UUID.
Parameters
- 'test' - mixed (to be generated to version 4 or 5 UUID)
Returns
string
Example
Code
string().uuid('test');
Outputs
'a94a8fe5-ccb1-5ba6-9c4c-0873d391e987'
