_.camelCase([string=''])

Converts string to camel case.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the camel cased string.

Example

  1. _.camelCase('Foo Bar');
  2. // => 'fooBar'
  3. _.camelCase('--foo-bar--');
  4. // => 'fooBar'
  5. _.camelCase('__FOO_BAR__');
  6. // => 'fooBar'

_.capitalize([string=''])

Converts the first character of string to upper case and the remaining to lower case.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to capitalize.

Returns

(string): Returns the capitalized string.

Example

  1. _.capitalize('FRED');
  2. // => '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

  1. [string=''] (string): The string to deburr.

Returns

(string): Returns the deburred string.

Example

  1. _.deburr('déjà vu');
  2. // => 'deja vu'

_.endsWith([string=''], [target], [position=string.length])

Checks if string ends with the given target string.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to inspect.
  2. [target] (string): The string to search for.
  3. [position=string.length] (number): The position to search up to.

Returns

(boolean): Returns true if string ends with target, else false.

Example

  1. _.endsWith('abc', 'c');
  2. // => true
  3. _.endsWith('abc', 'b');
  4. // => false
  5. _.endsWith('abc', 'b', 2);
  6. // => 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

  1. [string=''] (string): The string to escape.

Returns

(string): Returns the escaped string.

Example

  1. _.escape('fred, barney, & pebbles');
  2. // => 'fred, barney, &amp; pebbles'

_.escapeRegExp([string=''])

Escapes the RegExp special characters “^”, “$”, “\”, “.”, “*”, “+”, “?”, “(“, “)”, “[“, “]”, “{“, “}”, and “|” in string.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to escape.

Returns

(string): Returns the escaped string.

Example

  1. _.escapeRegExp('[lodash](https://lodash.com/)');
  2. // => '\[lodash\]\(https://lodash\.com/\)'

_.kebabCase([string=''])

Converts string to kebab case.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the kebab cased string.

Example

  1. _.kebabCase('Foo Bar');
  2. // => 'foo-bar'
  3. _.kebabCase('fooBar');
  4. // => 'foo-bar'
  5. _.kebabCase('__FOO_BAR__');
  6. // => 'foo-bar'

_.lowerCase([string=''])

Converts string, as space separated words, to lower case.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the lower cased string.

Example

  1. _.lowerCase('--Foo-Bar--');
  2. // => 'foo bar'
  3. _.lowerCase('fooBar');
  4. // => 'foo bar'
  5. _.lowerCase('__FOO_BAR__');
  6. // => 'foo bar'

_.lowerFirst([string=''])

Converts the first character of string to lower case.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the converted string.

Example

  1. _.lowerFirst('Fred');
  2. // => 'fred'
  3. _.lowerFirst('FRED');
  4. // => '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

  1. [string=''] (string): The string to pad.
  2. [length=0] (number): The padding length.
  3. [chars=' '] (string): The string used as padding.

Returns

(string): Returns the padded string.

Example

  1. _.pad('abc', 8);
  2. // => ' abc '
  3. _.pad('abc', 8, '_-');
  4. // => '_-abc_-_'
  5. _.pad('abc', 3);
  6. // => '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

  1. [string=''] (string): The string to pad.
  2. [length=0] (number): The padding length.
  3. [chars=' '] (string): The string used as padding.

Returns

(string): Returns the padded string.

Example

  1. _.padEnd('abc', 6);
  2. // => 'abc '
  3. _.padEnd('abc', 6, '_-');
  4. // => 'abc_-_'
  5. _.padEnd('abc', 3);
  6. // => '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

  1. [string=''] (string): The string to pad.
  2. [length=0] (number): The padding length.
  3. [chars=' '] (string): The string used as padding.

Returns

(string): Returns the padded string.

Example

  1. _.padStart('abc', 6);
  2. // => ' abc'
  3. _.padStart('abc', 6, '_-');
  4. // => '_-_abc'
  5. _.padStart('abc', 3);
  6. // => '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

  1. string (string): The string to convert.
  2. [radix=10] (number): The radix to interpret value by.

Returns

(number): Returns the converted integer.

Example

  1. _.parseInt('08');
  2. // => 8
  3. _.map(['6', '08', '10'], _.parseInt);
  4. // => [6, 8, 10]

_.repeat([string=''], [n=1])

Repeats the given string n times.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to repeat.
  2. [n=1] (number): The number of times to repeat the string.

Returns

(string): Returns the repeated string.

Example

  1. _.repeat('*', 3);
  2. // => '***'
  3. _.repeat('abc', 2);
  4. // => 'abcabc'
  5. _.repeat('abc', 0);
  6. // => ''

_.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

  1. [string=''] (string): The string to modify.
  2. pattern (RegExp|string): The pattern to replace.
  3. replacement (Function|string): The match replacement.

Returns

(string): Returns the modified string.

Example

  1. _.replace('Hi Fred', 'Fred', 'Barney');
  2. // => 'Hi Barney'

_.snakeCase([string=''])

Converts string to snake case.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the snake cased string.

Example

  1. _.snakeCase('Foo Bar');
  2. // => 'foo_bar'
  3. _.snakeCase('fooBar');
  4. // => 'foo_bar'
  5. _.snakeCase('--FOO-BAR--');
  6. // => 'foo_bar'

_.split([string=''], separator, [limit])

Splits string by separator.

Note: This method is based on String#split.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to split.
  2. separator (RegExp|string): The separator pattern to split by.
  3. [limit] (number): The length to truncate results to.

Returns

(Array): Returns the string segments.

Example

  1. _.split('a-b-c', '-', 2);
  2. // => ['a', 'b']

_.startCase([string=''])

Converts string to start case.

Since

3.1.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the start cased string.

Example

  1. _.startCase('--foo-bar--');
  2. // => 'Foo Bar'
  3. _.startCase('fooBar');
  4. // => 'Foo Bar'
  5. _.startCase('__FOO_BAR__');
  6. // => 'FOO BAR'

_.startsWith([string=''], [target], [position=0])

Checks if string starts with the given target string.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to inspect.
  2. [target] (string): The string to search for.
  3. [position=0] (number): The position to search from.

Returns

(boolean): Returns true if string starts with target, else false.

Example

  1. _.startsWith('abc', 'a');
  2. // => true
  3. _.startsWith('abc', 'b');
  4. // => false
  5. _.startsWith('abc', 'b', 1);
  6. // => 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

  1. [string=''] (string): The template string.
  2. [options={}] (Object): The options object.
  3. [options.escape=_.templateSettings.escape] (RegExp): The HTML “escape” delimiter.
  4. [options.evaluate=_.templateSettings.evaluate] (RegExp): The “evaluate” delimiter.
  5. [options.imports=_.templateSettings.imports] (Object): An object to import into the template as free variables.
  6. [options.interpolate=_.templateSettings.interpolate] (RegExp): The “interpolate” delimiter.
  7. [options.sourceURL='lodash.templateSources[n]'] (string): The sourceURL of the compiled template.
  8. [options.variable='obj'] (string): The data object variable name.

Returns

(Function): Returns the compiled template function.

Example

  1. // Use the "interpolate" delimiter to create a compiled template.
  2. var compiled = _.template('hello <%= user %>!');
  3. compiled({ 'user': 'fred' });
  4. // => 'hello fred!'
  5. // Use the HTML "escape" delimiter to escape data property values.
  6. var compiled = _.template('<b><%- value %></b>');
  7. compiled({ 'value': '<script>' });
  8. // => '<b>&lt;script&gt;</b>'
  9. // Use the "evaluate" delimiter to execute JavaScript and generate HTML.
  10. var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  11. compiled({ 'users': ['fred', 'barney'] });
  12. // => '<li>fred</li><li>barney</li>'
  13. // Use the internal `print` function in "evaluate" delimiters.
  14. var compiled = _.template('<% print("hello " + user); %>!');
  15. compiled({ 'user': 'barney' });
  16. // => 'hello barney!'
  17. // Use the ES template literal delimiter as an "interpolate" delimiter.
  18. // Disable support by replacing the "interpolate" delimiter.
  19. var compiled = _.template('hello ${ user }!');
  20. compiled({ 'user': 'pebbles' });
  21. // => 'hello pebbles!'
  22. // Use backslashes to treat delimiters as plain text.
  23. var compiled = _.template('<%= "\\<%- value %\\>" %>');
  24. compiled({ 'value': 'ignored' });
  25. // => '<%- value %>'
  26. // Use the `imports` option to import `jQuery` as `jq`.
  27. var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  28. var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  29. compiled({ 'users': ['fred', 'barney'] });
  30. // => '<li>fred</li><li>barney</li>'
  31. // Use the `sourceURL` option to specify a custom sourceURL for the template.
  32. var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  33. compiled(data);
  34. // => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
  35. // Use the `variable` option to ensure a with-statement isn't used in the compiled template.
  36. var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  37. compiled.source;
  38. // => function(data) {
  39. // var __t, __p = '';
  40. // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  41. // return __p;
  42. // }
  43. // Use custom template delimiters.
  44. _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  45. var compiled = _.template('hello {{ user }}!');
  46. compiled({ 'user': 'mustache' });
  47. // => 'hello mustache!'
  48. // Use the `source` property to inline compiled templates for meaningful
  49. // line numbers in error messages and stack traces.
  50. fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
  51. var JST = {\
  52. "main": ' + _.template(mainText).source + '\
  53. };\
  54. ');

_.toLower([string=''])

Converts string, as a whole, to lower case just like String#toLowerCase.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the lower cased string.

Example

  1. _.toLower('--Foo-Bar--');
  2. // => '--foo-bar--'
  3. _.toLower('fooBar');
  4. // => 'foobar'
  5. _.toLower('__FOO_BAR__');
  6. // => '__foo_bar__'

_.toUpper([string=''])

Converts string, as a whole, to upper case just like String#toUpperCase.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the upper cased string.

Example

  1. _.toUpper('--foo-bar--');
  2. // => '--FOO-BAR--'
  3. _.toUpper('fooBar');
  4. // => 'FOOBAR'
  5. _.toUpper('__foo_bar__');
  6. // => '__FOO_BAR__'

_.trim([string=''], [chars=whitespace])

Removes leading and trailing whitespace or specified characters from string.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to trim.
  2. [chars=whitespace] (string): The characters to trim.

Returns

(string): Returns the trimmed string.

Example

  1. _.trim(' abc ');
  2. // => 'abc'
  3. _.trim('-_-abc-_-', '_-');
  4. // => 'abc'
  5. _.map([' foo ', ' bar '], _.trim);
  6. // => ['foo', 'bar']

_.trimEnd([string=''], [chars=whitespace])

Removes trailing whitespace or specified characters from string.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to trim.
  2. [chars=whitespace] (string): The characters to trim.

Returns

(string): Returns the trimmed string.

Example

  1. _.trimEnd(' abc ');
  2. // => ' abc'
  3. _.trimEnd('-_-abc-_-', '_-');
  4. // => '-_-abc'

_.trimStart([string=''], [chars=whitespace])

Removes leading whitespace or specified characters from string.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to trim.
  2. [chars=whitespace] (string): The characters to trim.

Returns

(string): Returns the trimmed string.

Example

  1. _.trimStart(' abc ');
  2. // => 'abc '
  3. _.trimStart('-_-abc-_-', '_-');
  4. // => '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

  1. [string=''] (string): The string to truncate.
  2. [options={}] (Object): The options object.
  3. [options.length=30] (number): The maximum string length.
  4. [options.omission='...'] (string): The string to indicate text is omitted.
  5. [options.separator] (RegExp|string): The separator pattern to truncate to.

Returns

(string): Returns the truncated string.

Example

  1. _.truncate('hi-diddly-ho there, neighborino');
  2. // => 'hi-diddly-ho there, neighbo...'
  3. _.truncate('hi-diddly-ho there, neighborino', {
  4. 'length': 24,
  5. 'separator': ' '
  6. });
  7. // => 'hi-diddly-ho there,...'
  8. _.truncate('hi-diddly-ho there, neighborino', {
  9. 'length': 24,
  10. 'separator': /,? +/
  11. });
  12. // => 'hi-diddly-ho there...'
  13. _.truncate('hi-diddly-ho there, neighborino', {
  14. 'omission': ' [...]'
  15. });
  16. // => 'hi-diddly-ho there, neig [...]'

_.unescape([string=''])

The inverse of _.escape; this method converts the HTML entities &amp;, &lt;, &gt;, &quot;, and &#39; 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

  1. [string=''] (string): The string to unescape.

Returns

(string): Returns the unescaped string.

Example

  1. _.unescape('fred, barney, &amp; pebbles');
  2. // => 'fred, barney, & pebbles'

_.upperCase([string=''])

Converts string, as space separated words, to upper case.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the upper cased string.

Example

  1. _.upperCase('--foo-bar');
  2. // => 'FOO BAR'
  3. _.upperCase('fooBar');
  4. // => 'FOO BAR'
  5. _.upperCase('__foo_bar__');
  6. // => 'FOO BAR'

_.upperFirst([string=''])

Converts the first character of string to upper case.

Since

4.0.0

Arguments

  1. [string=''] (string): The string to convert.

Returns

(string): Returns the converted string.

Example

  1. _.upperFirst('fred');
  2. // => 'Fred'
  3. _.upperFirst('FRED');
  4. // => 'FRED'

_.words([string=''], [pattern])

Splits string into an array of its words.

Since

3.0.0

Arguments

  1. [string=''] (string): The string to inspect.
  2. [pattern] (RegExp|string): The pattern to match words.

Returns

(Array): Returns the words of string.

Example

  1. _.words('fred, barney, & pebbles');
  2. // => ['fred', 'barney', 'pebbles']
  3. _.words('fred, barney, & pebbles', /[^, ]+/g);
  4. // => ['fred', 'barney', '&', 'pebbles']