Underscore.string.fp
This is a wrapper for underscore.string to use it as a FP-library or with Ramda/lodash-fp
Install / Use
/learn @stoeffel/Underscore.string.fpREADME
Underscore.string.fp 
This is a wrapper for underscore.string to use it as a FP-library or with a library like lodash-fp or Ramda
Usage
In Node.js and Browserify
Install from npm
npm install underscore.string.fp
Require individual functions
var include = require("underscore.string.fp/include");
var includeHello = include('Hello');
includeHello("Hello world!");
// => true
includeHello("Nope");
// => false
or load the full library to compose functions
var rockit = S(
S.titleize(),
S.insert('rocks', 9),
S.trim('_')
);
// S.titleize(S.insert('rocks', 9, S.trim('_', "__stoeffel !__")));
rockit("__stoeffel !__");
// => "Stoeffel Rocks!"
but especially when using with Browserify the individual function approach is recommended because using it you only add those functions to your bundle you use.
Others
The dist/underscore.string.fp.js file is an UMD build. You can load it using
an AMD loader such as RequireJS or just stick it to a web page and access
the library from the S global.
Ramda integration
You can use it with ramda.
R.map(S.camelize(), [
'Moo boo',
'Foo bar'
]);
// => ['mooBoo', 'fooBoo']
R.filter(S.startsWith('.'), [
'.vimrc',
'foo.md',
'.zshrc'
]);
// => ['.vimrc', '.zshrc']
lodash-fp integration
You can use it with lodash-fp.
_.map(S.camelize(), [
'Moo boo',
'Foo bar'
]);
// => ['mooBoo', 'fooBoo']
_.filter(S.startsWith('.'), [
'.vimrc',
'foo.md',
'.zshrc'
]);
// => ['.vimrc', '.zshrc']
Download
- Development version Uncompressed with Comments
- Production version Minified
API
Individual functions
numberFormat(number) => string
Formats the numbers.
numberFormat(1000);
// => "1,000"
numberFormatDecimal(decimals, number) => string
Formats the numbers.
numberFormatDecimal(2)(1000);
// => "1,000.00"
numberFormatSeparator(decimalSeparator, orderSeparator, decimal, number) => string
Formats the numbers.
numberFormatSeparator(".", "'", 5, 123456789.123);
// => "123'456'789.12300"
levenshtein(string1, string2) => number
Calculates [Levenshtein distance][ld] between two strings. [ld]: http://en.wikipedia.org/wiki/Levenshtein_distance
levenshtein("kitten")("kittah");
// => 2
capitalize(string) => string
Converts first letter of the string to uppercase.
capitalize()("foo Bar");
// => "Foo Bar"
capitalize("foo Bar");
// => "Foo Bar"
decapitalize(string) => string
Converts first letter of the string to lowercase.
decapitalize()("Foo Bar");
// => "foo Bar"
decapitalize("Foo Bar");
// => "foo Bar"
chop(step, string) => array
chop(3)("whitespace");
// => ["whi", "tes", "pac", "e"]
clean(string) => string
Trim and replace multiple spaces with a single space.
clean()(" foo bar ");
// => "foo bar"
clean(" foo bar ");
// => "foo bar"
chars(string) => array
chars()("Hello");
// => ["H", "e", "l", "l", "o"]
chars("Hello");
// => ["H", "e", "l", "l", "o"]
swapCase(string) => string
Returns a copy of the string in which all the case-based characters have had their case swapped.
swapCase()("hELLO");
// => "Hello"
swapCase("hELLO");
// => "Hello"
include(substring, string) => boolean
Tests if string contains a substring.
include("ob")("foobar");
// => true
count(substring, string) => number
count("l")("Hello world");
// => 3
escapeHTML(string) => string
Converts HTML special characters to their entity equivalents.
escapeHTML()("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>"
escapeHTML("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>"
unescapeHTML(string) => string
Converts entity characters to HTML equivalents.
unescapeHTML()("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>"
unescapeHTML("<div>Blah blah blah</div>");
// => "<div>Blah blah blah</div>"
insert(substring, index, string) => string
insert("world", 6)("Hello ");
// => "Hello world"
replaceAll(replace, find, string) => string
replaceAll("a")("o")("foo");
// => "faa"
replaceAllIgnoreCase(replace, find, string) => string
replaceAll("a")("o")("fOo");
// => "faa"
isBlank(string) => boolean
isBlank(""); // => true
isBlank("\n"); // => true
isBlank(" "); // => true
isBlank("a"); // => false
join(separator, strings) => string
Joins strings together with given separator
join(" ")(["foo", "bar"]);
// => "foo bar"
lines(str) => array
Split lines to an array
lines()("Hello\nWorld");
// => ["Hello", "World"]
lines("Hello\nWorld");
// => ["Hello", "World"]
dedent(str) => string
Dedent unnecessary indentation
Credits go to @sindresorhus. This implementation is similar to https://github.com/sindresorhus/strip-indent
dedent()(" Hello\n World");
// => "Hello\n World"
dedent("\t\tHello\n\t\t\t\tWorld");
// => "Hello\n\t\tWorld"
dedentPattern(pattern, str) => string
Dedent by a pattern.
dedentPattern(" ")(" Hello\n World"); // Dedent by 2 spaces
// => " Hello\n World"
reverse(string) => string
Return reversed string:
reverse()("foobar");
// => "raboof"
reverse("foobar");
// => "raboof"
splice(substring, howmany, index, string) => string
Like an array splice.
splice("epeli")(7)(30)("https://edtsech@bitbucket.org/edtsech/underscore.strings");
// => "https://edtsech@bitbucket.org/epeli/underscore.strings"
startsWith(starts, string) => boolean
This method checks whether the string begins with starts.
startsWith("image")("image.gif");
// => true
startsWithAt(starts, position, string) => boolean
This method checks whether the string begins with starts at position.
startsWithAt(1)("vim")(".vimrc");
// => true
endsWith(ends, string) => boolean
This method checks whether the string ends with ends.
endsWith("gif")("image.gif");
// => true
endsWithAt(position, ends, string) => boolean
This method checks whether the string ends with ends at position.
endsWithAt(9)("old")("image.old.gif");
// => true
pred(string) => string
Returns the predecessor to str.
pred()("b");
// => "a"
pred("B");
// => "A"
succ(string) => string
Returns the successor to str.
succ()("a");
// => "b"
succ("A");
// => "B"
titleize(string) => string
titleize()("my name is epeli");
// => "My Name Is Epeli"
titleize("my name is epeli");
// => "My Name Is Epeli"
camelize(string) => string
Converts underscored or dasherized string to a camelized one.
camelize("moz-transform");
// => "mozTransform"
camelize("-moz-transform");
// => "mozTransform"
camelize("_moz_transform");
// => "mozTransform"
camelize("Moz-transform");
// => "mozTransform"
pascalize(string) => string
Converts underscored or dasherized string to a pascalized one. Begins with a lower case letter unless it starts with an underscore, dash or an upper case letter.
pascalize("moz-transform");
// => "mozTransform"
pascalize("-moz-transform");
// => "MozTransform"
pascalize("_moz_transform");
// => "MozTransform"
pascalize("Moz-transform");
// => "MozTransform"
classify(string) => string
Converts string to camelized class name. First letter is always upper case
classify()("some_class_name");
// => "SomeClassName"
classify("some_class_name");
// => "SomeClassName"
underscored(string) => string
Converts a camelized or dasherized string into an underscored one
underscored()("MozTransform");
// => "moz_transform"
underscored("MozTransform");
// => "moz_transform"
dasherize(string) => string
Converts a underscored or camelized string into an dasherized one
dasherize()("MozTransform");
// => "-moz-transform"
dasherize("MozTransform");
// => "-moz-transform"
humanize(string) => string
Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'.
humanize()(" capitalize dash-CamelCase_underscore trim ");
// => "Capitalize dash camel case underscore trim"
humanize(" capitalize dash-CamelCase_underscore trim ");
// => "Capitalize dash camel case underscore trim"
trim(characters, string) => string
Trims defined characters from begining and ending of the string.
trim(' ')(" foobar ");
// => "foobar"
trim("_-foobar-_", "_-");
// => "foobar"
ltrim(characters, string) => string
Left trim. Similar to trim, but only for left side.
rtrim(characters, string) => string
Right trim. Similar to trim, but only for right side.
truncate(truncateString, length, string) => string
``
