_.camelCase([string=''])
_.capitalize([string=''])
_.deburr([string=''])
_.endsWith([string=''], [target], [position=string.length])
_.escape([string=''])
_.escapeRegExp([string=''])
_.kebabCase([string=''])
_.lowerCase([string=''])
_.lowerFirst([string=''])
_.pad([string=''], [length=0], [chars=' '])
_.padEnd([string=''], [length=0], [chars=' '])
_.padStart([string=''], [length=0], [chars=' '])
_.parseInt(string, [radix=10])
_.repeat([string=''], [n=1])
_.replace([string=''], pattern, replacement)
_.snakeCase([string=''])
_.split([string=''], separator, [limit])
_.startCase([string=''])
_.startsWith([string=''], [target], [position=0])
_.template([string=''], [options={}])
_.toLower([string=''])
_.toUpper([string=''])
_.trim([string=''], [chars=whitespace])
_.trimEnd([string=''], [chars=whitespace])
_.trimStart([string=''], [chars=whitespace])
_.truncate([string=''], [options={}])
_.unescape([string=''])
_.upperCase([string=''])
_.upperFirst([string=''])
_.words([string=''], [pattern])
_.camelCase([string=''])
Converts string
to camel case.
Since
3.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the camel cased string.
Example
_.camelCase('Foo Bar');
// => 'fooBar'
_.camelCase('--foo-bar--');
// => 'fooBar'
_.camelCase('__FOO_BAR__');
// => 'fooBar'
_.capitalize([string=''])
Converts the first character of string
to upper case and the remaining
to lower case.
Since
3.0.0
Arguments
[string='']
(string): The string to capitalize.
Returns
(string): Returns the capitalized string.
Example
_.capitalize('FRED');
// => 'Fred'
_.deburr([string=''])
Deburrs string
by converting
Latin-1 Supplement#Character_table)
and Latin Extended-A
letters to basic Latin letters and removing
combining diacritical marks.
Since
3.0.0
Arguments
[string='']
(string): The string to deburr.
Returns
(string): Returns the deburred string.
Example
_.deburr('déjà vu');
// => 'deja vu'
_.endsWith([string=''], [target], [position=string.length])
Checks if string
ends with the given target string.
Since
3.0.0
Arguments
[string='']
(string): The string to inspect.[target]
(string): The string to search for.[position=string.length]
(number): The position to search up to.
Returns
(boolean): Returns true
if string
ends with target
, else false
.
Example
_.endsWith('abc', 'c');
// => true
_.endsWith('abc', 'b');
// => false
_.endsWith('abc', 'b', 2);
// => true
_.escape([string=''])
Converts the characters “&”, “<”, “>”, ‘“‘, and “‘“ in string
to their
corresponding HTML entities.
Note: No other characters are escaped. To escape additional
characters use a third-party library like he.
Though the “>” character is escaped for symmetry, characters like
“>” and “/“ don’t need escaping in HTML and have no special meaning
unless they’re part of a tag or unquoted attribute value. See
Mathias Bynens’s article
(under “semi-related fun fact”) for more details.
When working with HTML you should always
quote attribute values to reduce
XSS vectors.
Since
0.1.0
Arguments
[string='']
(string): The string to escape.
Returns
(string): Returns the escaped string.
Example
_.escape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
_.escapeRegExp([string=''])
Escapes the RegExp
special characters “^”, “$”, “\”, “.”, “*”, “+”,
“?”, “(“, “)”, “[“, “]”, “{“, “}”, and “|” in string
.
Since
3.0.0
Arguments
[string='']
(string): The string to escape.
Returns
(string): Returns the escaped string.
Example
_.escapeRegExp('[lodash](https://lodash.com/)');
// => '\[lodash\]\(https://lodash\.com/\)'
_.kebabCase([string=''])
Converts string
to
kebab case.
Since
3.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the kebab cased string.
Example
_.kebabCase('Foo Bar');
// => 'foo-bar'
_.kebabCase('fooBar');
// => 'foo-bar'
_.kebabCase('__FOO_BAR__');
// => 'foo-bar'
_.lowerCase([string=''])
Converts string
, as space separated words, to lower case.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the lower cased string.
Example
_.lowerCase('--Foo-Bar--');
// => 'foo bar'
_.lowerCase('fooBar');
// => 'foo bar'
_.lowerCase('__FOO_BAR__');
// => 'foo bar'
_.lowerFirst([string=''])
Converts the first character of string
to lower case.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the converted string.
Example
_.lowerFirst('Fred');
// => 'fred'
_.lowerFirst('FRED');
// => 'fRED'
_.pad([string=''], [length=0], [chars=' '])
Pads string
on the left and right sides if it’s shorter than length
.
Padding characters are truncated if they can’t be evenly divided by length
.
Since
3.0.0
Arguments
[string='']
(string): The string to pad.[length=0]
(number): The padding length.[chars=' ']
(string): The string used as padding.
Returns
(string): Returns the padded string.
Example
_.pad('abc', 8);
// => ' abc '
_.pad('abc', 8, '_-');
// => '_-abc_-_'
_.pad('abc', 3);
// => 'abc'
_.padEnd([string=''], [length=0], [chars=' '])
Pads string
on the right side if it’s shorter than length
. Padding
characters are truncated if they exceed length
.
Since
4.0.0
Arguments
[string='']
(string): The string to pad.[length=0]
(number): The padding length.[chars=' ']
(string): The string used as padding.
Returns
(string): Returns the padded string.
Example
_.padEnd('abc', 6);
// => 'abc '
_.padEnd('abc', 6, '_-');
// => 'abc_-_'
_.padEnd('abc', 3);
// => 'abc'
_.padStart([string=''], [length=0], [chars=' '])
Pads string
on the left side if it’s shorter than length
. Padding
characters are truncated if they exceed length
.
Since
4.0.0
Arguments
[string='']
(string): The string to pad.[length=0]
(number): The padding length.[chars=' ']
(string): The string used as padding.
Returns
(string): Returns the padded string.
Example
_.padStart('abc', 6);
// => ' abc'
_.padStart('abc', 6, '_-');
// => '_-_abc'
_.padStart('abc', 3);
// => 'abc'
_.parseInt(string, [radix=10])
Converts string
to an integer of the specified radix. If radix
is
undefined
or 0
, a radix
of 10
is used unless value
is a
hexadecimal, in which case a radix
of 16
is used.
Note: This method aligns with the
ES5 implementation of parseInt
.
Since
1.1.0
Arguments
string
(string): The string to convert.[radix=10]
(number): The radix to interpretvalue
by.
Returns
(number): Returns the converted integer.
Example
_.parseInt('08');
// => 8
_.map(['6', '08', '10'], _.parseInt);
// => [6, 8, 10]
_.repeat([string=''], [n=1])
Repeats the given string n
times.
Since
3.0.0
Arguments
[string='']
(string): The string to repeat.[n=1]
(number): The number of times to repeat the string.
Returns
(string): Returns the repeated string.
Example
_.repeat('*', 3);
// => '***'
_.repeat('abc', 2);
// => 'abcabc'
_.repeat('abc', 0);
// => ''
_.replace([string=''], pattern, replacement)
Replaces matches for pattern
in string
with replacement
.
Note: This method is based on
String#replace
.
Since
4.0.0
Arguments
[string='']
(string): The string to modify.pattern
(RegExp|string): The pattern to replace.replacement
(Function|string): The match replacement.
Returns
(string): Returns the modified string.
Example
_.replace('Hi Fred', 'Fred', 'Barney');
// => 'Hi Barney'
_.snakeCase([string=''])
Converts string
to
snake case.
Since
3.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the snake cased string.
Example
_.snakeCase('Foo Bar');
// => 'foo_bar'
_.snakeCase('fooBar');
// => 'foo_bar'
_.snakeCase('--FOO-BAR--');
// => 'foo_bar'
_.split([string=''], separator, [limit])
Splits string
by separator
.
Note: This method is based on
String#split
.
Since
4.0.0
Arguments
[string='']
(string): The string to split.separator
(RegExp|string): The separator pattern to split by.[limit]
(number): The length to truncate results to.
Returns
(Array): Returns the string segments.
Example
_.split('a-b-c', '-', 2);
// => ['a', 'b']
_.startCase([string=''])
Converts string
to
start case.
Since
3.1.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the start cased string.
Example
_.startCase('--foo-bar--');
// => 'Foo Bar'
_.startCase('fooBar');
// => 'Foo Bar'
_.startCase('__FOO_BAR__');
// => 'FOO BAR'
_.startsWith([string=''], [target], [position=0])
Checks if string
starts with the given target string.
Since
3.0.0
Arguments
[string='']
(string): The string to inspect.[target]
(string): The string to search for.[position=0]
(number): The position to search from.
Returns
(boolean): Returns true
if string
starts with target
, else false
.
Example
_.startsWith('abc', 'a');
// => true
_.startsWith('abc', 'b');
// => false
_.startsWith('abc', 'b', 1);
// => true
_.template([string=''], [options={}])
Creates a compiled template function that can interpolate data properties
in “interpolate” delimiters, HTML-escape interpolated data properties in
“escape” delimiters, and execute JavaScript in “evaluate” delimiters. Data
properties may be accessed as free variables in the template. If a setting
object is given, it takes precedence over _.templateSettings
values.
Note: In the development build _.template
utilizes
sourceURLs
for easier debugging.
For more information on precompiling templates see
lodash’s custom builds documentation.
For more information on Chrome extension sandboxes see
Chrome’s extensions documentation.
Since
0.1.0
Arguments
[string='']
(string): The template string.[options={}]
(Object): The options object.[options.escape=_.templateSettings.escape]
(RegExp): The HTML “escape” delimiter.[options.evaluate=_.templateSettings.evaluate]
(RegExp): The “evaluate” delimiter.[options.imports=_.templateSettings.imports]
(Object): An object to import into the template as free variables.[options.interpolate=_.templateSettings.interpolate]
(RegExp): The “interpolate” delimiter.[options.sourceURL='lodash.templateSources[n]']
(string): The sourceURL of the compiled template.[options.variable='obj']
(string): The data object variable name.
Returns
(Function): Returns the compiled template function.
Example
// Use the "interpolate" delimiter to create a compiled template.
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'
// Use the HTML "escape" delimiter to escape data property values.
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b><script></b>'
// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
// Use the internal `print` function in "evaluate" delimiters.
var compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'
// Use the ES template literal delimiter as an "interpolate" delimiter.
// Disable support by replacing the "interpolate" delimiter.
var compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'
// Use backslashes to treat delimiters as plain text.
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// => '<%- value %>'
// Use the `imports` option to import `jQuery` as `jq`.
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
// Use the `sourceURL` option to specify a custom sourceURL for the template.
var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);
// => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
// Use the `variable` option to ensure a with-statement isn't used in the compiled template.
var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;
// => function(data) {
// var __t, __p = '';
// __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
// return __p;
// }
// Use custom template delimiters.
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'
// Use the `source` property to inline compiled templates for meaningful
// line numbers in error messages and stack traces.
fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
var JST = {\
"main": ' + _.template(mainText).source + '\
};\
');
_.toLower([string=''])
Converts string
, as a whole, to lower case just like
String#toLowerCase.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the lower cased string.
Example
_.toLower('--Foo-Bar--');
// => '--foo-bar--'
_.toLower('fooBar');
// => 'foobar'
_.toLower('__FOO_BAR__');
// => '__foo_bar__'
_.toUpper([string=''])
Converts string
, as a whole, to upper case just like
String#toUpperCase.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the upper cased string.
Example
_.toUpper('--foo-bar--');
// => '--FOO-BAR--'
_.toUpper('fooBar');
// => 'FOOBAR'
_.toUpper('__foo_bar__');
// => '__FOO_BAR__'
_.trim([string=''], [chars=whitespace])
Removes leading and trailing whitespace or specified characters from string
.
Since
3.0.0
Arguments
[string='']
(string): The string to trim.[chars=whitespace]
(string): The characters to trim.
Returns
(string): Returns the trimmed string.
Example
_.trim(' abc ');
// => 'abc'
_.trim('-_-abc-_-', '_-');
// => 'abc'
_.map([' foo ', ' bar '], _.trim);
// => ['foo', 'bar']
_.trimEnd([string=''], [chars=whitespace])
Removes trailing whitespace or specified characters from string
.
Since
4.0.0
Arguments
[string='']
(string): The string to trim.[chars=whitespace]
(string): The characters to trim.
Returns
(string): Returns the trimmed string.
Example
_.trimEnd(' abc ');
// => ' abc'
_.trimEnd('-_-abc-_-', '_-');
// => '-_-abc'
_.trimStart([string=''], [chars=whitespace])
Removes leading whitespace or specified characters from string
.
Since
4.0.0
Arguments
[string='']
(string): The string to trim.[chars=whitespace]
(string): The characters to trim.
Returns
(string): Returns the trimmed string.
Example
_.trimStart(' abc ');
// => 'abc '
_.trimStart('-_-abc-_-', '_-');
// => 'abc-_-'
_.truncate([string=''], [options={}])
Truncates string
if it’s longer than the given maximum string length.
The last characters of the truncated string are replaced with the omission
string which defaults to “…”.
Since
4.0.0
Arguments
[string='']
(string): The string to truncate.[options={}]
(Object): The options object.[options.length=30]
(number): The maximum string length.[options.omission='...']
(string): The string to indicate text is omitted.[options.separator]
(RegExp|string): The separator pattern to truncate to.
Returns
(string): Returns the truncated string.
Example
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': ' '
});
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': /,? +/
});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', {
'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
_.unescape([string=''])
The inverse of _.escape
; this method converts the HTML entities
&
, <
, >
, "
, and '
in string
to
their corresponding characters.
Note: No other HTML entities are unescaped. To unescape additional
HTML entities use a third-party library like he.
Since
0.6.0
Arguments
[string='']
(string): The string to unescape.
Returns
(string): Returns the unescaped string.
Example
_.unescape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
_.upperCase([string=''])
Converts string
, as space separated words, to upper case.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the upper cased string.
Example
_.upperCase('--foo-bar');
// => 'FOO BAR'
_.upperCase('fooBar');
// => 'FOO BAR'
_.upperCase('__foo_bar__');
// => 'FOO BAR'
_.upperFirst([string=''])
Converts the first character of string
to upper case.
Since
4.0.0
Arguments
[string='']
(string): The string to convert.
Returns
(string): Returns the converted string.
Example
_.upperFirst('fred');
// => 'Fred'
_.upperFirst('FRED');
// => 'FRED'
_.words([string=''], [pattern])
Splits string
into an array of its words.
Since
3.0.0
Arguments
[string='']
(string): The string to inspect.[pattern]
(RegExp|string): The pattern to match words.
Returns
(Array): Returns the words of string
.
Example
_.words('fred, barney, & pebbles');
// => ['fred', 'barney', 'pebbles']
_.words('fred, barney, & pebbles', /[^, ]+/g);
// => ['fred', 'barney', '&', 'pebbles']