Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
underscore.string
Advanced tools
The underscore.string npm package provides utility functions for string manipulation, including functions for trimming, padding, converting case, and more. It extends the functionality of Underscore.js, a JavaScript library that provides functional programming helpers, to work with strings.
String Trimming
Removes whitespace from both ends of a string.
" Hello world! ".trim(); // 'Hello world!'
Case Conversion
Converts dash or underscore delimited strings into camelCase.
_.str.camelize('moz-transform'); // 'mozTransform'
String Padding
Pads the string with characters until the total string length is equal to the passed length parameter.
_.str.pad('1', 8, '0'); // '00000001'
String Truncation
Truncates the string securely, not cutting words in half, and appending an ellipsis if the string was truncated.
_.str.prune('Hello, world', 5); // 'Hello...'
Number Formatting
Formats a number with grouped thousands and a specific number of decimal places.
_.str.numberFormat(1000, 2); // '1,000.00'
Lodash is a popular utility library that offers similar string manipulation functions, along with a wide range of other utilities for arrays, objects, and more. It is often considered a more comprehensive alternative to underscore.string.
The 'string' npm package provides an additional set of string manipulation tools. It includes methods for creating slugs, translating to different cases, and more. It is similar to underscore.string but with a different API design and additional features.
The 'he' package is an HTML entity encoder/decoder. While it is more specialized than underscore.string, it offers robust handling of HTML entities which might be useful in conjunction with string manipulation.
Idea: Esa-Matti Suuronen (esa-matti aet suuronen dot org)
Authors: Esa-Matti Suuronen, Edward Tsech
Javascript lacks complete string manipulation operations. This an attempt to fill that cap. List of buildin methods can be found for example from Dive Into JavaScript.
As name states this an extension for Underscore.js, but it can be used independently from _s-global variable. But with Underscore.js you can use Object-Oriented style and chaining:
_(" epeli ").chain().trim().capitalize().value()
=> "Epeli"
npm package
npm install underscore.string
Standalone usage:
var _s = require('undescore.string');
Integrate with Underscore.js:
var _ = require('underscore');
_.mixin(require('underscore.string'));
capitalize _.capitalize(string)
Converts first letter of the string to uppercase.
_.capitalize("epeli")
=> "Epeli"
chop _.chop(string, step)
_.chop('whitespace', 3)
=> ['whi','tes','pac','e']
clean _.clean(str)
Compress some whitespaces to one.
_.clean(" foo bar ")
=> 'foo bar'
chars _.chars(str)
_.chars('Hello')
=> ['H','e','l','l','o']
includes _.includes(string, substring)
Tests if string contains a substring.
_.includes("foobar", "ob")
=> true
count _.count(string, substring)
_('Hello world').count('l')
=> 3
escapeHTML _.escapeHTML(string)
Converts HTML special characters to their entity equivalents.
_('<div>Blah blah blah</div>').escapeHTML();
=> '<div>Blah blah blah</div>'
unescapeHTML _.unescapeHTML(string)
Converts entity characters to HTML equivalents.
_('<div>Blah blah blah</div>').unescapeHTML();
=> '<div>Blah blah blah</div>'
insert _.insert(string, index, substing)
_('Hello ').insert(6, 'world')
=> 'Hello world'
join _.join(separator, *strings)
Joins strings together with given separator
_.join(" ", "foo", "bar")
=> "foo bar"
lines _.lines(str)
_.lines("Hello\nWorld")
=> ["Hello", "World"]
reverse
This functions has been removed, because this function override underscore.js 'reverse'. But now you can do that:
_("foobar").chars().reverse().join('')
splice _.splice(string, index, howmany, substring)
Like a array splice.
_('https://edtsech@bitbucket.org/edtsech/underscore.strings').splice(30, 7, 'epeli')
=> 'https://edtsech@bitbucket.org/epeli/underscore.strings'
startsWith _.startsWith(string, starts)
This method checks whether string starts with starts.
_("image.gif").startsWith("image")
=> true
endsWith _.endsWith(string, ends)
This method checks whether string ends with ends.
_("image.gif").endsWith("gif")
=> true
succ _.succ(str)
Returns the successor to str.
_('a').succ()
=> 'b'
_('A').succ()
=> 'B'
supplant
Supplant function was removed, use Underscore.js template function.
strip alias for trim
lstrip alias for ltrim
rstrip alias for rtrim
titleize _.titleize(string)
_('my name is epeli').titleize()
=> 'My Name Is Epeli'
camelize _.camelize(string)
Converts underscored or dasherized string to a camelized one
_('-moz-transform').camelize()
=> 'MozTransform'
underscored _.underscored(string)
Converts a camelized or dasherized string into an underscored one
_(MozTransform).underscored()
=> 'moz_transform'
dasherize _.dasherize(string)
Converts a underscored or camelized string into an dasherized one
_('MozTransform').dasherize()
=> '-moz-transform'
trim _.trim(string, [characters])
trims defined characters from begining and ending of the string. Defaults to whitespace characters.
_.trim(" foobar ")
=> "foobar"
_.trim("_-foobar-_", "_-")
=> "foobar"
ltrim _.ltrim(string, [characters])
Left trim. Similar to trim, but only for left side.
rtrim _.rtrim(string, [characters])
Left trim. Similar to trim, but only for right side.
truncate _.truncate(string, length, truncateString)
_('Hello world').truncate(5)
=> 'Hello...'
words _.words(str, delimiter=" ")
Split string by delimiter (String or RegExp), ' ' by default.
_.words("I love you")
=> ["I","love","you"]
_.words("I_love_you", "_")
=> ["I","love","you"]
_.words("I-love-you", /-/)
=> ["I","love","you"]
sprintf _.sprintf(string format, *arguments)
C like string formatting. Credits goes to Alexandru Marasteanu. For more detailed documentation, see the original page.
_.sprintf("%.1f", 1.17)
"1.2"
pad _.pad(str, length, [padStr, type])
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 _.lpad(str, length, [padStr])
left-pad a string. Alias for pad(str, length, padStr, 'left')
_.lpad("1", 8, '0')
-> "00000001";
rpad _.rpad(str, length, [padStr])
right-pad a string. Alias for pad(str, length, padStr, 'right')
_.rpad("1", 8, '0')
-> "10000000";
lrpad _.lrpad(str, length, [padStr])
left/right-pad a string. Alias for pad(str, length, padStr, 'both')
_.lrpad("1", 8, '0')
-> "00001000";
center alias for lrpad
ljust alias for lpad
rjust alias for rpad
toNumber _.toNumber(string, [decimals])
Parse string to number. Return 0 if string can't be parsed to number.
_('2.556').toNumber()
=> 3
_('2.556').toNumber(1)
=> 2.6
strRight _.strRight(string, pattern)
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.
_('This_is_a_test_string').strRight('_')
=> "is_a_test_string";
strRightBack _.strRightBack(string, pattern)
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.
_('This_is_a_test_string').strRightBack('_')
=> "string";
strLeft _.strLeft(string, pattern)
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.
_('This_is_a_test_string').strLeft('_')
=> "This";
strLeftBack _.strLeftBack(string, pattern)
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.
_('This_is_a_test_string').strLeftBack('_')
=> "This_is_a_test";
Any suggestions or bug reports are welcome. Just email me or more preferably open an issue.
Otherwise changes will be rejected.
The MIT License
Copyright (c) 2011 Eduard Tsech edtsech@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.
FAQs
String manipulation extensions for Underscore.js javascript library.
The npm package underscore.string receives a total of 1,741,327 weekly downloads. As such, underscore.string popularity was classified as popular.
We found that underscore.string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.