• Stars
    star
    3,372
  • Rank 12,731 (Top 0.3 %)
  • Language
    JavaScript
  • Created almost 14 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

String manipulation helpers for javascript

The stable release documentation can be found here https://epeli.github.io/underscore.string/

Underscore.string Build Status

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.

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!");
// => hello-world

s("   epeli  ").trim().capitalize().value();
// => "Epeli"

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'
]);
// => ['.vimrc', '.zshrc']

Download

API

Individual functions

numberFormat(number, [ decimals=0, decimalSeparator='.', orderSeparator=',']) => string

Formats the numbers.

numberFormat(1000, 2);
// => "1,000.00"

numberFormat(123456789.123, 5, ".", ",");
// => "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, [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");
// => "Foo Bar"

capitalize("FOO Bar", true);
// => "Foo bar"

decapitalize(string) => string

Converts first letter of the string to lowercase.

decapitalize("Foo Bar");
// => "foo Bar"

chop(string, step) => array

chop("whitespace", 3);
// => ["whi", "tes", "pac", "e"]

clean(string) => string

Trim and replace multiple spaces with a single space.

clean(" foo    bar   ");
// => "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");
// => "aakkonen"

chars(string) => array

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"

include(string, substring) => boolean

Tests if string contains a substring.

include("foobar", "ob");
// => true

count(string, substring) => number

Returns number of occurrences of substring in string.

count("Hello world", "l");
// => 3

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>");
// => "&lt;div&gt;Blah blah blah&lt;/div&gt;"

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("&lt;div&gt;Blah&nbsp;blah blah&lt;/div&gt;");
// => "<div>Blah blah blah</div>"

insert(string, index, substring) => string

insert("Hellworld", 4, "o ");
// => "Hello world"

replaceAll(string, find, replace, [ignorecase=false]) => string

replaceAll("foo", "o", "a");
// => "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"]

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 })
// => "Hello\nWorld"

wrap("Hello World", { width:6, seperator:'.', trailingSpaces: true })
// => "Hello .World "

wrap("Hello World", { width:5, seperator:'.', cut:true, trailingSpaces: true })
// => "Hello. Worl.d    "

wrap("Hello World", { width:5, seperator:'.', preserveSpaces: true })
// => "Hello .World"

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");
// => "Hello\n  World"

dedent("\t\tHello\n\t\t\t\tWorld");
// => "Hello\n\t\tWorld"

dedent("    Hello\n    World", "  "); // Dedent by 2 spaces
// => "  Hello\n  World"

reverse(string) => string

Return reversed string:

reverse("foobar");
// => "raboof"

splice(string, index, howmany, substring) => string

Like an array splice.

splice("https://[email protected]/edtsech/underscore.strings", 30, 7, "epeli");
// => "https://[email protected]/epeli/underscore.strings"

startsWith(string, starts, [position]) => boolean

This method checks whether the string begins with starts at position (default: 0).

startsWith("image.gif", "image");
// => true

startsWith(".vimrc", "vim", 1);
// => true

endsWith(string, ends, [position]) => boolean

This method checks whether the string ends with ends at position (default: string.length).

endsWith("image.gif", "gif");
// => true

endsWith("image.old.gif", "old", 9);
// => 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"

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");
// => "mozTransform"

camelize("-moz-transform");
// => "MozTransform"

camelize("_moz_transform");
// => "MozTransform"

camelize("Moz-transform");
// => "MozTransform"

camelize("-moz-transform", true);
// => "mozTransform"

classify(string) => string

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.

trim("  foobar   ");
// => "foobar"

trim("_-foobar-_", "_-");
// => "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);
// => "Hello..."

truncate("Hello", 10);
// => "Hello"

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);
// => "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.

pad("1", 8);
// => "       1"

pad("1", 8, "0");
// => "00000001"

pad("1", 8, "0", "right");
// => "10000000"

pad("1", 8, "0", "both");
// => "00001000"

pad("1", 8, "bleepblorp", "both");
// => "bbbb1bbb"

lpad(str, length, [padStr]) => string

left-pad a string. Alias for pad(str, length, padStr, "left")

lpad("1", 8, "0");
// => "00000001"

rpad(str, length, [padStr]) => string

right-pad a string. Alias for pad(str, length, padStr, "right")

rpad("1", 8, "0");
// => "10000000"

lrpad(str, length, [padStr]) => string

left/right-pad a string. Alias for pad(str, length, padStr, "both")

lrpad("1", 8, '0');
// => "00001000"

toNumber(string, [decimals]) => number

Parse string to number. Returns NaN if string can't be parsed to number.

toNumber("2.556");
// => 3

toNumber("2.556", 1);
// => 2.6

toNumber("999.999", -1);
// => 990

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", "_");
// => "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", "_");
// => "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", "_");
// => "This";

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", "_");
// => "This_is_a_test";

stripTags(string) => string

Removes all html tags from string.

stripTags("a <a href=\"#\">link</a>");
// => "a link"

stripTags("a <a href=\"#\">link</a><script>alert(\"hello world!\")</script>");
// => "a linkalert("hello world!")"

toSentence(array, [delimiter, lastDelimiter]) => string

Join an array into a human readable sentence.

toSentence(["jQuery", "Mootools", "Prototype"]);
// => "jQuery, Mootools and Prototype";

toSentence(["jQuery", "Mootools", "Prototype"], ", ", " unt ");
// => "jQuery, Mootools unt Prototype";

toSentenceSerial(array, [delimiter, lastDelimiter]) => string

The same as toSentence, but adjusts delimeters to use Serial comma.

toSentenceSerial(["jQuery", "Mootools"]);
// => "jQuery and Mootools"

toSentenceSerial(["jQuery", "Mootools", "Prototype"]);
// => "jQuery, Mootools, and Prototype"

toSentenceSerial(["jQuery", "Mootools", "Prototype"], ", ", " unt ");
// => "jQuery, Mootools, unt Prototype"

repeat(string, count, [separator]) => string

Repeats a string count times.

repeat("foo", 3);
// => "foofoofoo"

repeat("foo", 3, "bar");
// => "foobarfoobarfoo"

surround(string, wrap) => string

Surround a string with another string.

surround("foo", "ab");
// => "abfooab"

quote(string, quoteChar) or q(string, quoteChar) => string

Quotes a string. quoteChar defaults to ".

quote("foo", '"');
// => '"foo"';

unquote(string, quoteChar) => string

Unquotes a string. quoteChar defaults to ".

unquote('"foo"');
// => "foo"

unquote("'foo'", "'");
// => "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");
// => "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.

Just past it to Array#sort.

["foo20", "foo5"].sort(naturalCmp);
// => ["foo5", "foo20"]

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");
// => true

toBoolean("FALSE");
// => false

toBoolean("random");
// => undefined

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"]);
// => true

toBoolean("true only at start", [/^true/]);
// => 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;
});
// => "Hello world"

map(12345, function(x) {
  return x;
});
// => "12345"

map("Hello world", function(x) {
  if (x === 'o') x = 'O';
  return x;
});
// => "HellO wOrld"

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();
// => "Foo"

When calling a method which does not return a string the resulting value is immediately returned

s(" foobar ").trim().startsWith("foo");
// => true

chain.tap(function) => chain

Tap into the chain with a custom function

s("foo").tap(function(value){
  return value + "bar";
}).value();
// => "foobar"

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 [email protected]

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.

More Repositories

1

react-zorm

🪱 Zorm - Type-safe <form> for React using Zod
TypeScript
714
star
2

node-hbsfy

Handlebars precompiler plugin for Browserify
JavaScript
257
star
3

immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
TypeScript
225
star
4

slimux

SLIME inspired tmux integration plugin for Vim
Vim Script
216
star
5

piler

Deprecated Asset Manager for Node.js
CoffeeScript
148
star
6

redux-hooks

âš“ React Hooks implementation for Redux
TypeScript
96
star
7

node-promisepipe

Safely pipe node.js streams while capturing all errors to a single promise
JavaScript
80
star
8

requirejs-hbs

Simple Handlebars loader plugin for RequireJS
JavaScript
79
star
9

lean-redux

Redux state like local component state
JavaScript
78
star
10

angry-caching-proxy

Make package downloads lightning fast!
JavaScript
77
star
11

browserify-externalize

Create external Browserify bundles for lazy asynchronous loading
JavaScript
73
star
12

postcss-ts-classnames

PostCSS plugin to generate TypeScript types from your CSS class names.
TypeScript
70
star
13

jslibs

List of Javascript libraries
59
star
14

backbone.viewmaster

Few tested opinions on how to handle deeply nested views in Backbone.js focusing on modularity.
JavaScript
52
star
15

babel-plugin-ts-optchain

Babel plugin for transpiling legacy browser support to ts-optchain by removing Proxy usage.
TypeScript
30
star
16

trpc-cloudflare-worker

TypeScript
29
star
17

carcounter

Asynchronous module loading example with Browserify
JavaScript
24
star
18

redux-render-prop

Redux with render props. Typescript friendly.
TypeScript
22
star
19

source-map-peek

Peek into original source via source maps from the command line when devtools fail.
JavaScript
21
star
20

TextareaServer

Server for opening external text editors from Chrome
CoffeeScript
19
star
21

deno_karabiner

Write Complex Modifications for Karabiner-Elements using Typescript and Deno.
TypeScript
17
star
22

babel-plugin-display-name-custom

display name inference for your custom react component creators
JavaScript
15
star
23

geekslides

Remote controllable Node.js introduction slides
JavaScript
15
star
24

npm-release

Github action for npm (pre)releases
TypeScript
15
star
25

sauna.reload

Development & Issues MOVED to https://github.com/collective/sauna.reload
Python
13
star
26

TextareaConnect

Edit textareas in Google Chrome using any external editor!
CoffeeScript
13
star
27

node-clim

Console.Log IMproved for Node.js
JavaScript
12
star
28

react-simple

simple style only components for the web & native
JavaScript
9
star
29

typescript-redux-todoapp

Type-safe Boilerplate-free Redux Example
TypeScript
9
star
30

jquery.panfullsize

jQuery plugin for panning large images
JavaScript
8
star
31

neovim-config

Vim Script
8
star
32

Projectwatch

Watch file changes and run multiple css/js pre-processors from one watcher
JavaScript
8
star
33

multip

Tiny multi process init for containers written in Rust 🦀
Rust
8
star
34

ts-box

Put exceptions into boxes 📦
TypeScript
8
star
35

vimconfig

Vim and other dotfiles
Vim Script
7
star
36

node-tftp

Streaming TFTP Server for node.js
JavaScript
7
star
37

react-makefile

Makefile boilerplate for React.js
JavaScript
6
star
38

yalr

YALR - Yet Another Live Reload
JavaScript
6
star
39

vscode-unsaved

Makes you actually notice when you have unsaved files
TypeScript
5
star
40

reallyexpress

Really Express extends Express with extraordinary features given by Node.js. Work in progress.
CoffeeScript
5
star
41

shell-cheatsheet

4
star
42

tmpshare

Easily share temporary files over http
JavaScript
4
star
43

ircshare

Simple service for sharing your pictures on IRC
JavaScript
3
star
44

ircshare-android

Share pictures easily in IRC from Android phone
Java
3
star
45

qdomain

Promises from domains
JavaScript
3
star
46

browserify-cs-example

Browserify v2 with CoffeeScript Source Maps
CoffeeScript
3
star
47

flysight-subtitles

Convert FlySight data files (.csv) to SubRip (.srt) subtitles.
JavaScript
3
star
48

node-handlebars-runtime

Handlebars runtime only
JavaScript
2
star
49

node-lights

Instanssi Light Server Simulator
CoffeeScript
2
star
50

revisioncask

Version control management made simple
Python
2
star
51

flips.io

JavaScript
2
star
52

subssh

Small framework for creating SSH public key based shell accounts.
Python
2
star
53

toggl-paster

I need to paste Toggl time entries into things so I made a thing.
TypeScript
2
star
54

utils

Epeli's Javascript / Typescript utilities
TypeScript
2
star
55

MAILNETD

Meta Asynchronous Irc Linked Network Email Transport Daemon
Python
1
star
56

rt

Instant tab-complete for npm scripts and Jakefiles
Rust
1
star
57

lunarvim-config

Lua
1
star
58

awesome

My Personal Awesome WM config
Lua
1
star
59

wp-graphql-todoapp

TypeScript
1
star
60

stylify

JavaScript
1
star
61

subssh_buildout

buildout for subssh development purposes
Python
1
star
62

LightManager

lightmanager tool written for instanssi.org party
Java
1
star
63

rollsum

Just experiments with rolling checksums. Nothing to see here.
C
1
star
64

sh-thunk

Generate promise returning thunks from shell strings.
JavaScript
1
star
65

konf

TypeScript
1
star
66

HAL2012

Home hack using Node.js and Raspberry Pi
CoffeeScript
1
star
67

IRCPost

Simple IRC bot with HTTP POST API
CoffeeScript
1
star
68

blog

JavaScript
1
star
69

aswyg-editor

ASWYG Editor - Also See What You Get Editor
JavaScript
1
star