Installations
npm install underscore.string.fp
Developer
stoeffel
Developer Guide
Module System
CommonJS
Min. Node Version
*
Typescript Support
No
Node Version
0.12.2
NPM Version
2.7.4
Statistics
20 Stars
10 Commits
1 Forks
3 Watching
1 Branches
2 Contributors
Updated on 07 Feb 2023
Languages
JavaScript (97.69%)
HTML (2.31%)
Total Downloads
Cumulative downloads
Total Downloads
1,887,701
Last day
-17.3%
91
Compared to previous day
Last week
-66.2%
448
Compared to previous week
Last month
78.7%
3,501
Compared to previous month
Last year
-36.4%
33,527
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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
1var include = require("underscore.string.fp/include"); 2 3var includeHello = include('Hello'); 4 5includeHello("Hello world!"); 6// => true 7 8includeHello("Nope"); 9// => false
or load the full library to compose functions
1var rockit = S( 2 S.titleize(), 3 S.insert('rocks', 9), 4 S.trim('_') 5); 6 7// S.titleize(S.insert('rocks', 9, S.trim('_', "__stoeffel !__"))); 8rockit("__stoeffel !__"); 9// => "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.
1R.map(S.camelize(), [ 2 'Moo boo', 3 'Foo bar' 4]); 5// => ['mooBoo', 'fooBoo']
1R.filter(S.startsWith('.'), [ 2 '.vimrc', 3 'foo.md', 4 '.zshrc' 5]); 6// => ['.vimrc', '.zshrc']
lodash-fp integration
You can use it with lodash-fp.
1_.map(S.camelize(), [ 2 'Moo boo', 3 'Foo bar' 4]); 5// => ['mooBoo', 'fooBoo']
1_.filter(S.startsWith('.'), [ 2 '.vimrc', 3 'foo.md', 4 '.zshrc' 5]); 6// => ['.vimrc', '.zshrc']
Download
- Development version Uncompressed with Comments
- Production version Minified
API
Individual functions
numberFormat(number) => string
Formats the numbers.
1numberFormat(1000); 2// => "1,000"
numberFormatDecimal(decimals, number) => string
Formats the numbers.
1numberFormatDecimal(2)(1000); 2// => "1,000.00"
numberFormatSeparator(decimalSeparator, orderSeparator, decimal, number) => string
Formats the numbers.
1numberFormatSeparator(".", "'", 5, 123456789.123); 2// => "123'456'789.12300"
levenshtein(string1, string2) => number
Calculates [Levenshtein distance][ld] between two strings. [ld]: http://en.wikipedia.org/wiki/Levenshtein_distance
1levenshtein("kitten")("kittah"); 2// => 2
capitalize(string) => string
Converts first letter of the string to uppercase.
1capitalize()("foo Bar"); 2// => "Foo Bar" 3 4capitalize("foo Bar"); 5// => "Foo Bar"
decapitalize(string) => string
Converts first letter of the string to lowercase.
1decapitalize()("Foo Bar"); 2// => "foo Bar" 3 4decapitalize("Foo Bar"); 5// => "foo Bar"
chop(step, string) => array
1chop(3)("whitespace"); 2// => ["whi", "tes", "pac", "e"]
clean(string) => string
Trim and replace multiple spaces with a single space.
1clean()(" foo bar "); 2// => "foo bar" 3 4clean(" foo bar "); 5// => "foo bar"
chars(string) => array
1chars()("Hello"); 2// => ["H", "e", "l", "l", "o"] 3 4chars("Hello"); 5// => ["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.
1swapCase()("hELLO"); 2// => "Hello" 3 4swapCase("hELLO"); 5// => "Hello"
include(substring, string) => boolean
Tests if string contains a substring.
1include("ob")("foobar"); 2// => true
count(substring, string) => number
1count("l")("Hello world"); 2// => 3
escapeHTML(string) => string
Converts HTML special characters to their entity equivalents.
1escapeHTML()("<div>Blah blah blah</div>"); 2// => "<div>Blah blah blah</div>" 3 4escapeHTML("<div>Blah blah blah</div>"); 5// => "<div>Blah blah blah</div>"
unescapeHTML(string) => string
Converts entity characters to HTML equivalents.
1unescapeHTML()("<div>Blah blah blah</div>"); 2// => "<div>Blah blah blah</div>" 3 4unescapeHTML("<div>Blah blah blah</div>"); 5// => "<div>Blah blah blah</div>"
insert(substring, index, string) => string
1insert("world", 6)("Hello "); 2// => "Hello world"
replaceAll(replace, find, string) => string
1replaceAll("a")("o")("foo"); 2// => "faa"
replaceAllIgnoreCase(replace, find, string) => string
1replaceAll("a")("o")("fOo"); 2// => "faa"
isBlank(string) => boolean
1isBlank(""); // => true 2isBlank("\n"); // => true 3isBlank(" "); // => true 4isBlank("a"); // => false
join(separator, strings) => string
Joins strings together with given separator
1join(" ")(["foo", "bar"]); 2// => "foo bar"
lines(str) => array
Split lines to an array
1lines()("Hello\nWorld"); 2// => ["Hello", "World"] 3 4lines("Hello\nWorld"); 5// => ["Hello", "World"]
dedent(str) => string
Dedent unnecessary indentation
Credits go to @sindresorhus. This implementation is similar to https://github.com/sindresorhus/strip-indent
1dedent()(" Hello\n World"); 2// => "Hello\n World" 3 4dedent("\t\tHello\n\t\t\t\tWorld"); 5// => "Hello\n\t\tWorld"
dedentPattern(pattern, str) => string
Dedent by a pattern.
1dedentPattern(" ")(" Hello\n World"); // Dedent by 2 spaces 2// => " Hello\n World"
reverse(string) => string
Return reversed string:
1reverse()("foobar"); 2// => "raboof" 3 4reverse("foobar"); 5// => "raboof"
splice(substring, howmany, index, string) => string
Like an array splice.
1splice("epeli")(7)(30)("https://edtsech@bitbucket.org/edtsech/underscore.strings"); 2// => "https://edtsech@bitbucket.org/epeli/underscore.strings"
startsWith(starts, string) => boolean
This method checks whether the string begins with starts
.
1startsWith("image")("image.gif"); 2// => true
startsWithAt(starts, position, string) => boolean
This method checks whether the string begins with starts
at position
.
1startsWithAt(1)("vim")(".vimrc"); 2// => true
endsWith(ends, string) => boolean
This method checks whether the string ends with ends
.
1endsWith("gif")("image.gif"); 2// => true
endsWithAt(position, ends, string) => boolean
This method checks whether the string ends with ends
at position
.
1endsWithAt(9)("old")("image.old.gif"); 2// => true
pred(string) => string
Returns the predecessor to str.
1pred()("b"); 2// => "a" 3 4pred("B"); 5// => "A"
succ(string) => string
Returns the successor to str.
1succ()("a"); 2// => "b" 3 4succ("A"); 5// => "B"
titleize(string) => string
1titleize()("my name is epeli"); 2// => "My Name Is Epeli" 3 4titleize("my name is epeli"); 5// => "My Name Is Epeli"
camelize(string) => string
Converts underscored or dasherized string to a camelized one.
1camelize("moz-transform"); 2// => "mozTransform" 3 4camelize("-moz-transform"); 5// => "mozTransform" 6 7camelize("_moz_transform"); 8// => "mozTransform" 9 10camelize("Moz-transform"); 11// => "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.
1pascalize("moz-transform"); 2// => "mozTransform" 3 4pascalize("-moz-transform"); 5// => "MozTransform" 6 7pascalize("_moz_transform"); 8// => "MozTransform" 9 10pascalize("Moz-transform"); 11// => "MozTransform"
classify(string) => string
Converts string to camelized class name. First letter is always upper case
1classify()("some_class_name"); 2// => "SomeClassName" 3 4classify("some_class_name"); 5// => "SomeClassName"
underscored(string) => string
Converts a camelized or dasherized string into an underscored one
1underscored()("MozTransform"); 2// => "moz_transform" 3 4underscored("MozTransform"); 5// => "moz_transform"
dasherize(string) => string
Converts a underscored or camelized string into an dasherized one
1dasherize()("MozTransform"); 2// => "-moz-transform" 3 4dasherize("MozTransform"); 5// => "-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'.
1humanize()(" capitalize dash-CamelCase_underscore trim "); 2// => "Capitalize dash camel case underscore trim" 3 4humanize(" capitalize dash-CamelCase_underscore trim "); 5// => "Capitalize dash camel case underscore trim"
trim(characters, string) => string
Trims defined characters from begining and ending of the string.
1trim(' ')(" foobar "); 2// => "foobar" 3 4trim("_-foobar-_", "_-"); 5// => "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
1truncate('...')(5)("Hello world"); 2// => "Hello..." 3 4truncate('...')(10)("Hello"); 5// => "Hello"
prune(pruneString, length, string) => string
Elegant version of truncate. Makes sure the pruned string does not exceed the original length. Avoid half-chopped words when truncating.
1prune('...')(5)("Hello, world"); 2// => "Hello..." 3 4prune('...')(8)("Hello, world"); 5// => "Hello..." 6 7prune(" (read a lot more)")(5)("Hello, world"); 8// => "Hello, world" (as adding "(read a lot more)" would be longer than the original string) 9 10prune( '...')(1)("Hello, cruel world"); 11// => "Hello, cruel..." 12 13prune( '...')(1)("Hello"); 14// => "Hello"
words(str) => array
Split string by /\s+/.
1words()(" I love you "); 2// => ["I", "love", "you"] 3 4words(" ") 5// => []
wordsDelim(delimiter=/\s+/, str) => array
Split string by delimiter (String or RegExp).
1words("_")("I_love_you"); 2// => ["I", "love", "you"] 3 4words(/-/)("I-love-you"); 5// => ["I", "love", "you"]
sprintf(arguments, string format) => string
C like string formatting. Credits goes to Alexandru Marasteanu. For more detailed documentation, see the original page.
1sprintf(1.17)("%.1f"); 2// => "1.2"
pad(padStr, length, str) => 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.
1pad(" ")(8)("1"); 2// => " 1" 3 4pad("0")(8)("1"); 5// => "00000001"
lpad(padStr, length, str) => string
left-pad a string. Alias for pad(str, length, padStr, "left")
1lpad("0")(8)("1"); 2// => "00000001"
rpad(padStr, length, str) => string
right-pad a string. Alias for pad(str, length, padStr, "right")
1rpad("0")(8)("1"); 2// => "10000000"
lrpad(padStr, length, str) => string
left/right-pad a string. Alias for pad(str, length, padStr, "both")
1lrpad('0')(8)("1"); 2// => "00001000"
toNumber(decimals, string) => number
Parse string to number. Returns NaN if string can't be parsed to number.
1toNumber(0)("2.556"); 2// => 3 3 4toNumber(1)("2.556"); 5// => 2.6 6 7toNumber(-1)("999.999"); 8// => 990
strRight(pattern, string) => 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.
1strRight("_")("This_is_a_test_string"); 2// => "is_a_test_string"
strRightBack(pattern, string) => 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.
1strRightBack("_")("This_is_a_test_string"); 2// => "string"
strLeft(pattern, string) => 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.
1strLeft("_")("This_is_a_test_string"); 2// => "This";
strLeftBack(pattern, string) => 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.
1strLeftBack("_")("This_is_a_test_string"); 2// => "This_is_a_test";
stripTags(string) => string
Removes all html tags from string.
1stripTags()("a <a href="#">link</a>"); 2// => "a link" 3 4stripTags("a <a href="#">link</a><script>alert("hello world!")</script>"); 5// => "a linkalert("hello world!")"
toSentence(lastDelimiter, delimiter, array) => string
Join an array into a human readable sentence.
1toSentence(" and ")(", ")((["jQuery", "Mootools", "Prototype"]); 2// => "jQuery, Mootools and Prototype"; 3 4toSentence(" unt ")(", ")((["jQuery", "Mootools", "Prototype"]); 5// => "jQuery, Mootools unt Prototype";
toSentenceSerial(lastDelimiter, delimiter, array) => string
The same as toSentence
, but adjusts delimeters to use Serial comma.
1toSentenceSerial(' and ')(', ')(["jQuery", "Mootools"]); 2// => "jQuery and Mootools" 3 4toSentenceSerial(' and ')(', ')(["jQuery", "Mootools", "Prototype"]); 5// => "jQuery, Mootools, and Prototype" 6 7toSentenceSerial(' unt ')(', ')(["jQuery", "Mootools", "Prototype"]); 8// => "jQuery, Mootools, unt Prototype"
repeat(separator, count, string) => string
Repeats a string count times.
1repeat('')(3)("foo"); 2// => "foofoofoo" 3 4repeat("bar")(3)("foo"); 5// => "foobarfoobarfoo"
surround(wrap, string) => string
Surround a string with another string.
1surround("ab")("foo"); 2// => "abfooab"
quote(quoteChar, string) or q(quoteChar, string) => string
Quotes a string. quoteChar
.
1quote('"')("foo"); 2// => '"foo"';
unquote(quoteChar, string) => string
Unquotes a string. quoteChar
.
1unquote('"')('"foo"'); 2// => "foo" 3 4unquote("'")("'foo'"); 5// => "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
.
1slugify()("Un éléphant à l\'orée du bois"); 2// => "un-elephant-a-l-oree-du-bois" 3 4slugify("Un éléphant à l\'orée du bois"); 5// => "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
.
1["foo20", "foo5"].sort(naturalCmp); 2// => ["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.
1toBoolean("true"); 2// => true 3 4toBoolean("FALSE"); 5// => false 6 7toBoolean("random"); 8// => undefined
Library functions
If you require the full library you can use composing and aliases
s(functions) => function
Compose new functions.
1var rockit = S( 2 S.titleize(), 3 S.insert('rocks', 9), 4 S.trim('_') 5); 6 7// S.titleize(S.insert('rocks', 9, S.trim('_', "__stoeffel !__"))); 8rockit("__stoeffel !__"); 9// => "Stoeffel Rocks!"
Aliases
1strip = trim 2lstrip = ltrim 3rstrip = rtrim 4center = lrpad 5rjust = lpad 6ljust = rpad 7contains = include 8q = quote 9toBool = toBoolean 10camelcase = camelize
Maintainers
This library is maintained by
- Christoph Hermann – @stoeffel
Licence
The MIT License
Copyright (c) 2015 Christoph Hermann schtoeffel@gmail.com
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 1/10 approved changesets -- score normalized to 1
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Score
2.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More