The stable release documentation can be found here https://epeli.github.io/underscore.string/
Underscore.string
Javascript lacks complete string manipulation operations.
This is an attempt to fill that gap. List of build-in methods can be found
for example from Dive Into JavaScript.
Originally started as an Underscore.js extension but is a full standalone
library nowadays.
Upgrading from 2.x to 3.x? Please read the changelog.
Usage
For Node.js, Browserify and Webpack
Install from npm
npm install underscore.string
Require individual functions
var slugify = require("underscore.string/slugify");
slugify("Hello world!");
or load the full library to enable chaining
var s = require("underscore.string");
s(" epeli ").trim().capitalize().value();
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.
In Meteor
From your Meteor project folder
meteor add underscorestring:underscore.string
and you'll be able to access the library with the s global from both the server and the client.
s.slugify("Hello world!");
s(" epeli ").trim().capitalize().value();
Others
The dist/underscore.string.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.
Underscore.js/Lo-Dash integration
It is still possible use as Underscore.js/Lo-Dash extension
_.mixin(s.exports());
But it's not recommended since include
, contains
, reverse
and join
are dropped because they collide with the functions already defined by Underscore.js.
Lo-Dash-FP/Ramda integration
If you want to use underscore.string with ramdajs or Lo-Dash-FP you can use underscore.string.fp.
npm install underscore.string.fp
var S = require('underscore.string.fp');
var filter = require('lodash-fp').filter;
var filter = require('ramda').filter;
filter(S.startsWith('.'), [
'.vimrc',
'foo.md',
'.zshrc'
]);
Download
API
Individual functions
numberFormat(number, [ decimals=0, decimalSeparator='.', orderSeparator=',']) => string
Formats the numbers.
numberFormat(1000, 2);
numberFormat(123456789.123, 5, ".", ",");
levenshtein(string1, string2) => number
Calculates [Levenshtein distance][ld] between two strings.
[ld]: http://en.wikipedia.org/wiki/Levenshtein_distance
levenshtein("kitten", "kittah");
capitalize(string, [lowercaseRest=false]) => string
Converts first letter of the string to uppercase. If true
is passed as second argument the rest
of the string will be converted to lower case.
capitalize("foo Bar");
capitalize("FOO Bar", true);
decapitalize(string) => string
Converts first letter of the string to lowercase.
decapitalize("Foo Bar");
chop(string, step) => array
chop("whitespace", 3);
clean(string) => string
Trim and replace multiple spaces with a single space.
clean(" foo bar ");
cleanDiacritics(string) => string
Replace diacritic characters with closest ASCII equivalents. Check the
source for supported characters. Pull requests welcome for missing
characters!
cleanDiacritics("ääkkönen");
chars("Hello");
swapCase(string) => string
Returns a copy of the string in which all the case-based characters have had their case swapped.
swapCase("hELLO");
include(string, substring) => boolean
Tests if string contains a substring.
include("foobar", "ob");
count(string, substring) => number
Returns number of occurrences of substring in string.
count("Hello world", "l");
escapeHTML(string) => string
Converts HTML special characters to their entity equivalents.
This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos.
escapeHTML("<div>Blah blah blah</div>");
unescapeHTML(string) => string
Converts entity characters to HTML equivalents.
This function supports cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos, nbsp.
unescapeHTML("<div>Blah blah blah</div>");
insert(string, index, substring) => string
insert("Hellworld", 4, "o ");
replaceAll(string, find, replace, [ignorecase=false]) => string
replaceAll("foo", "o", "a");
isBlank(string) => boolean
isBlank("");
isBlank("\n");
isBlank(" ");
isBlank("a");
join(separator, ...strings) => string
Joins strings together with given separator
join(" ", "foo", "bar");
lines(str) => array
Split lines to an array
lines("Hello\nWorld");
wrap(str, options) => string
Splits a line str
(default '') into several lines of size options.width
(default 75) using a options.seperator
(default '\n'). If options.trailingSpaces
is true, make each line at least width
long using trailing spaces. If options.cut
is true, create new lines in the middle of words. If options.preserveSpaces
is true, preserve the space that should be there at the end of a line (only works if options.cut is false).
wrap("Hello World", { width:5 })
wrap("Hello World", { width:6, seperator:'.', trailingSpaces: true })
wrap("Hello World", { width:5, seperator:'.', cut:true, trailingSpaces: true })
wrap("Hello World", { width:5, seperator:'.', preserveSpaces: true })
dedent(str, [pattern]) => string
Dedent unnecessary indentation or dedent by a pattern.
Credits go to @sindresorhus.
This implementation is similar to https://github.com/sindresorhus/strip-indent
dedent(" Hello\n World");
dedent("\t\tHello\n\t\t\t\tWorld");
dedent(" Hello\n World", " ");
reverse(string) => string
Return reversed string:
reverse("foobar");
splice(string, index, howmany, substring) => string
Like an array splice.
splice("https://edtsech@bitbucket.org/edtsech/underscore.strings", 30, 7, "epeli");
startsWith(string, starts, [position]) => boolean
This method checks whether the string begins with starts
at position
(default: 0).
startsWith("image.gif", "image");
startsWith(".vimrc", "vim", 1);
endsWith(string, ends, [position]) => boolean
This method checks whether the string ends with ends
at position
(default: string.length).
endsWith("image.gif", "gif");
endsWith("image.old.gif", "old", 9);
pred(string) => string
Returns the predecessor to str.
pred("b");
pred("B");
succ(string) => string
Returns the successor to str.
succ("a");
succ("A");
titleize(string) => string
titleize("my name is epeli");
camelize(string, [decapitalize=false]) => string
Converts underscored or dasherized string to a camelized one. Begins with
a lower case letter unless it starts with an underscore, dash or an upper case letter.
camelize("moz-transform");
camelize("-moz-transform");
camelize("_moz_transform");
camelize("Moz-transform");
camelize("-moz-transform", true);
classify(string) => string
Converts string to camelized class name. First letter is always upper case
classify("some_class_name");
underscored(string) => string
Converts a camelized or dasherized string into an underscored one
underscored("MozTransform");
dasherize(string) => string
Converts a underscored or camelized string into an dasherized one
dasherize("MozTransform");
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 ");
trim(string, [characters]) => string
Trims defined characters from begining and ending of the string.
Defaults to whitespace characters.
trim(" foobar ");
trim("_-foobar-_", "_-");
ltrim(string, [characters]) => string
Left trim. Similar to trim, but only for left side.
rtrim(string, [characters]) => string
Right trim. Similar to trim, but only for right side.
truncate(string, length, [truncateString = '...']) => string
truncate("Hello world", 5);
truncate("Hello", 10);
prune(string, length, pruneString) => string
Elegant version of truncate. Makes sure the pruned string does not exceed the
original length. Avoid half-chopped words when truncating.
prune("Hello, world", 5);
prune("Hello, world", 8);
prune("Hello, world", 5, " (read a lot more)");
prune("Hello, cruel world", 15);
prune("Hello", 10);
words(str, delimiter=/\s+/) => array
Split string by delimiter (String or RegExp), /\s+/ by default.
words(" I love you ");
words("I_love_you", "_");
words("I-love-you", /-/);
words(" ")
sprintf(string format, ...arguments) => string
C like string formatting. Makes use of the sprintf-js package.
This function will be removed in the next major release, use the sprintf-js package instead.
sprintf("%.1f", 1.17);
pad(str, length, [padStr, type]) => string
pads the str
with characters until the total string length is equal to the passed length
parameter. By default, pads on the left with the space char (" "
). padStr
is truncated to a single character if necessary.
pad("1", 8);
pad("1", 8, "0");
pad("1", 8, "0", "right");
pad("1", 8, "0", "both");
pad("1", 8, "bleepblorp", "both");
lpad(str, length, [padStr]) => string
left-pad a string. Alias for pad(str, length, padStr, "left")
lpad("1", 8, "0");
rpad(str, length, [padStr]) => string
right-pad a string. Alias for pad(str, length, padStr, "right")
rpad("1", 8, "0");
lrpad(str, length, [padStr]) => string
left/right-pad a string. Alias for pad(str, length, padStr, "both")
lrpad("1", 8, '0');
toNumber(string, [decimals]) => number
Parse string to number. Returns NaN if string can't be parsed to number.
toNumber("2.556");
toNumber("2.556", 1);
toNumber("999.999", -1);
strRight(string, pattern) => string
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
strRight("This_is_a_test_string", "_");
strRightBack(string, pattern) => string
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
strRightBack("This_is_a_test_string", "_");
strLeft(string, pattern) => string
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
strLeft("This_is_a_test_string", "_");
strLeftBack(string, pattern) => string
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
strLeftBack("This_is_a_test_string", "_");
stripTags(string) => string
Removes all html tags from string.
stripTags("a <a href=\"#\">link</a>");
stripTags("a <a href=\"#\">link</a><script>alert(\"hello world!\")</script>");
toSentence(array, [delimiter, lastDelimiter]) => string
Join an array into a human readable sentence.
toSentence(["jQuery", "Mootools", "Prototype"]);
toSentence(["jQuery", "Mootools", "Prototype"], ", ", " unt ");
toSentenceSerial(array, [delimiter, lastDelimiter]) => string
The same as toSentence
, but adjusts delimeters to use Serial comma.
toSentenceSerial(["jQuery", "Mootools"]);
toSentenceSerial(["jQuery", "Mootools", "Prototype"]);
toSentenceSerial(["jQuery", "Mootools", "Prototype"], ", ", " unt ");
repeat(string, count, [separator]) => string
Repeats a string count times.
repeat("foo", 3);
repeat("foo", 3, "bar");
surround(string, wrap) => string
Surround a string with another string.
surround("foo", "ab");
quote(string, quoteChar) or q(string, quoteChar) => string
Quotes a string. quoteChar
defaults to "
.
quote("foo", '"');
unquote(string, quoteChar) => string
Unquotes a string. quoteChar
defaults to "
.
unquote('"foo"');
unquote("'foo'", "'");
slugify(string) => string
Transform text into an ascii slug which can be used in safely in URLs. Replaces whitespaces, accentuated, and special characters with a dash. Limited set of non-ascii characters are transformed to similar versions in the ascii character set such as ä
to a
.
slugify("Un éléphant à l\'orée du bois");
Caution: this function is charset dependent
naturalCmp(string1, string2) => number
Naturally sort strings like humans would do. None numbers are compared by their ASCII values. Note: this means "a" > "A". Use .toLowerCase
if this isn't to be desired.
Just past it to Array#sort
.
["foo20", "foo5"].sort(naturalCmp);
toBoolean(string) => boolean
Turn strings that can be commonly considered as booleas to real booleans. Such as "true", "false", "1" and "0". This function is case insensitive.
toBoolean("true");
toBoolean("FALSE");
toBoolean("random");
It can be customized by giving arrays of truth and falsy value matcher as parameters. Matchers can be also RegExp objects.
toBoolean("truthy", ["truthy"], ["falsy"]);
toBoolean("true only at start", [/^true/]);
map(string, function) => string
Creates a new string with the results of calling a provided function on every character of the given string.
map("Hello world", function(x) {
return x;
});
map(12345, function(x) {
return x;
});
map("Hello world", function(x) {
if (x === 'o') x = 'O';
return x;
});
Library functions
If you require the full library you can use chaining and aliases
s(string) => chain
Start a chain. Returns an immutable chain object with the string functions as
methods which return a new chain object instead of the plain string value.
The chain object includes also following native Javascript string methods:
chain.value()
Return the string value from the chain
s(" foo ").trim().capitalize().value();
When calling a method which does not return a string the resulting value is
immediately returned
s(" foobar ").trim().startsWith("foo");
chain.tap(function) => chain
Tap into the chain with a custom function
s("foo").tap(function(value){
return value + "bar";
}).value();
Aliases
strip = trim
lstrip = ltrim
rstrip = rtrim
center = lrpad
rjust = lpad
ljust = rpad
contains = include
q = quote
toBool = toBoolean
camelcase = camelize
Maintainers
This library is maintained by
Licence
The MIT License
Copyright (c) 2011 Esa-Matti Suuronen esa-matti@suuronen.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.