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!");// => hello-world
or load the full library to enable chaining
var s = require("underscore.string");s(" epeli ").trim().capitalize().value();// => "Epeli"
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.
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.
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).
titleize("my name is epeli");// => "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.
Converts string to camelized class name. First letter is always upper case
classify("some_class_name");// => "SomeClassName"
underscored(string) => string
Converts a camelized or dasherized string into an underscored one
underscored("MozTransform");// => "moz_transform"
dasherize(string) => string
Converts a underscored or camelized string into an dasherized one
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"
trim(string, [characters]) => string
Trims defined characters from begining and ending of the string.
Defaults to whitespace characters.
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);// => "Hello..."prune("Hello, world", 8);// => "Hello..."prune("Hello, world", 5, " (read a lot more)");// => "Hello, world" (as adding "(read a lot more)" would be longer than the original string)prune("Hello, cruel world", 15);// => "Hello, cruel..."prune("Hello", 10);// => "Hello"
words(str, delimiter=/\s+/) => array
Split string by delimiter (String or RegExp), /\s+/ by default.
words(" I love you ");// => ["I", "love", "you"]words("I_love_you", "_");// => ["I", "love", "you"]words("I-love-you", /-/);// => ["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);// => "1.2"
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.
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.
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.
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.
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.
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");// => "un-elephant-a-l-oree-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.
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:
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.