Gathering detailed insights and metrics for underscore.string.fp
Gathering detailed insights and metrics for underscore.string.fp
Gathering detailed insights and metrics for underscore.string.fp
Gathering detailed insights and metrics for underscore.string.fp
npm install underscore.string.fp
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
20 Stars
10 Commits
1 Forks
3 Watching
1 Branches
2 Contributors
Updated on 07 Feb 2023
JavaScript (97.69%)
HTML (2.31%)
Cumulative downloads
Total Downloads
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
This is a wrapper for underscore.string to use it as a FP-library or with a library like lodash-fp or Ramda
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.
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.
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']
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']
Formats the numbers.
1numberFormat(1000); 2// => "1,000"
Formats the numbers.
1numberFormatDecimal(2)(1000); 2// => "1,000.00"
Formats the numbers.
1numberFormatSeparator(".", "'", 5, 123456789.123); 2// => "123'456'789.12300"
Calculates [Levenshtein distance][ld] between two strings. [ld]: http://en.wikipedia.org/wiki/Levenshtein_distance
1levenshtein("kitten")("kittah"); 2// => 2
Converts first letter of the string to uppercase.
1capitalize()("foo Bar"); 2// => "Foo Bar" 3 4capitalize("foo Bar"); 5// => "Foo Bar"
Converts first letter of the string to lowercase.
1decapitalize()("Foo Bar"); 2// => "foo Bar" 3 4decapitalize("Foo Bar"); 5// => "foo Bar"
1chop(3)("whitespace"); 2// => ["whi", "tes", "pac", "e"]
Trim and replace multiple spaces with a single space.
1clean()(" foo bar "); 2// => "foo bar" 3 4clean(" foo bar "); 5// => "foo bar"
1chars()("Hello"); 2// => ["H", "e", "l", "l", "o"] 3 4chars("Hello"); 5// => ["H", "e", "l", "l", "o"]
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"
Tests if string contains a substring.
1include("ob")("foobar"); 2// => true
1count("l")("Hello world"); 2// => 3
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>"
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>"
1insert("world", 6)("Hello "); 2// => "Hello world"
1replaceAll("a")("o")("foo"); 2// => "faa"
1replaceAll("a")("o")("fOo"); 2// => "faa"
1isBlank(""); // => true 2isBlank("\n"); // => true 3isBlank(" "); // => true 4isBlank("a"); // => false
Joins strings together with given separator
1join(" ")(["foo", "bar"]); 2// => "foo bar"
Split lines to an array
1lines()("Hello\nWorld"); 2// => ["Hello", "World"] 3 4lines("Hello\nWorld"); 5// => ["Hello", "World"]
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"
Dedent by a pattern.
1dedentPattern(" ")(" Hello\n World"); // Dedent by 2 spaces 2// => " Hello\n World"
Return reversed string:
1reverse()("foobar"); 2// => "raboof" 3 4reverse("foobar"); 5// => "raboof"
Like an array splice.
1splice("epeli")(7)(30)("https://edtsech@bitbucket.org/edtsech/underscore.strings"); 2// => "https://edtsech@bitbucket.org/epeli/underscore.strings"
This method checks whether the string begins with starts
.
1startsWith("image")("image.gif"); 2// => true
This method checks whether the string begins with starts
at position
.
1startsWithAt(1)("vim")(".vimrc"); 2// => true
This method checks whether the string ends with ends
.
1endsWith("gif")("image.gif"); 2// => true
This method checks whether the string ends with ends
at position
.
1endsWithAt(9)("old")("image.old.gif"); 2// => true
Returns the predecessor to str.
1pred()("b"); 2// => "a" 3 4pred("B"); 5// => "A"
Returns the successor to str.
1succ()("a"); 2// => "b" 3 4succ("A"); 5// => "B"
1titleize()("my name is epeli"); 2// => "My Name Is Epeli" 3 4titleize("my name is epeli"); 5// => "My Name Is Epeli"
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"
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"
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"
Converts a camelized or dasherized string into an underscored one
1underscored()("MozTransform"); 2// => "moz_transform" 3 4underscored("MozTransform"); 5// => "moz_transform"
Converts a underscored or camelized string into an dasherized one
1dasherize()("MozTransform"); 2// => "-moz-transform" 3 4dasherize("MozTransform"); 5// => "-moz-transform"
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"
Trims defined characters from begining and ending of the string.
1trim(' ')(" foobar "); 2// => "foobar" 3 4trim("_-foobar-_", "_-"); 5// => "foobar"
Left trim. Similar to trim, but only for left side.
Right trim. Similar to trim, but only for right side.
1truncate('...')(5)("Hello world"); 2// => "Hello..." 3 4truncate('...')(10)("Hello"); 5// => "Hello"
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"
Split string by /\s+/.
1words()(" I love you "); 2// => ["I", "love", "you"] 3 4words(" ") 5// => []
Split string by delimiter (String or RegExp).
1words("_")("I_love_you"); 2// => ["I", "love", "you"] 3 4words(/-/)("I-love-you"); 5// => ["I", "love", "you"]
C like string formatting. Credits goes to Alexandru Marasteanu. For more detailed documentation, see the original page.
1sprintf(1.17)("%.1f"); 2// => "1.2"
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"
left-pad a string. Alias for pad(str, length, padStr, "left")
1lpad("0")(8)("1"); 2// => "00000001"
right-pad a string. Alias for pad(str, length, padStr, "right")
1rpad("0")(8)("1"); 2// => "10000000"
left/right-pad a string. Alias for pad(str, length, padStr, "both")
1lrpad('0')(8)("1"); 2// => "00001000"
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
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"
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"
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";
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";
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!")"
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";
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"
Repeats a string count times.
1repeat('')(3)("foo"); 2// => "foofoofoo" 3 4repeat("bar")(3)("foo"); 5// => "foobarfoobarfoo"
Surround a string with another string.
1surround("ab")("foo"); 2// => "abfooab"
Quotes a string. quoteChar
.
1quote('"')("foo"); 2// => '"foo"';
Unquotes a string. quoteChar
.
1unquote('"')('"foo"'); 2// => "foo" 3 4unquote("'")("'foo'"); 5// => "foo"
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
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"]
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
If you require the full library you can use composing and aliases
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!"
1strip = trim 2lstrip = ltrim 3rstrip = rtrim 4center = lrpad 5rjust = lpad 6ljust = rpad 7contains = include 8q = quote 9toBool = toBoolean 10camelcase = camelize
This library is maintained by
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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