_.camelCase([string=’’])

Converts string to camel case.

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=’’])

Capitalizes the first character of string.

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 supplementary letters#Character_table) to basic latin letters and removing combining diacritical marks.

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.

Arguments

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

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.

Backticks are escaped because in Internet Explorer < 9, they can break out of attribute values or HTML comments. See #59, #102, #108, and #133 of the HTML5 Security Cheatsheet for more details.

When working with HTML you should always quote attribute values to reduce XSS vectors.

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.

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.

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'

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

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'

_.padLeft([string=’’], [length=0], [chars=’ ‘])

Pads string on the left side if it’s shorter than length. Padding characters are truncated if they exceed length.

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

_.padRight([string=’’], [length=0], [chars=’ ‘])

Pads string on the right side if it’s shorter than length. Padding characters are truncated if they exceed length.

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

_.parseInt(string, [radix])

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.

Arguments

  1. string (string): The string to convert.
  2. [radix] (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=0])

Repeats the given string n times.

Arguments

  1. [string=''] (string): The string to repeat.
  2. [n=0] (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. // => ''

_.snakeCase([string=’’])

Converts string to snake case.

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'

_.startCase([string=’’])

Converts string to start case.

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.

Arguments

  1. [string=''] (string): The string to search.
  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 provided 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.

Arguments

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

Returns

(Function): Returns the compiled template function.

Example

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

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

Removes leading and trailing whitespace or specified characters from string.

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']

_.trimLeft([string=’’], [chars=whitespace])

Removes leading whitespace or specified characters from string.

Arguments

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

Returns

(string): Returns the trimmed string.

Example

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

_.trimRight([string=’’], [chars=whitespace])

Removes trailing whitespace or specified characters from string.

Arguments

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

Returns

(string): Returns the trimmed string.

Example

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

_.trunc([string=’’], [options], [options.length=30], [options.omission=’…’], [options.separator])

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 “…”.

Arguments

  1. [string=''] (string): The string to truncate.
  2. [options] (Object|number): The options object or maximum string length.
  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. _.trunc('hi-diddly-ho there, neighborino');
  2. // => 'hi-diddly-ho there, neighbo...'
  3. _.trunc('hi-diddly-ho there, neighborino', 24);
  4. // => 'hi-diddly-ho there, n...'
  5. _.trunc('hi-diddly-ho there, neighborino', {
  6. 'length': 24,
  7. 'separator': ' '
  8. });
  9. // => 'hi-diddly-ho there,...'
  10. _.trunc('hi-diddly-ho there, neighborino', {
  11. 'length': 24,
  12. 'separator': /,? +/
  13. });
  14. // => 'hi-diddly-ho there...'
  15. _.trunc('hi-diddly-ho there, neighborino', {
  16. 'omission': ' [...]'
  17. });
  18. // => 'hi-diddly-ho there, neig [...]'

_.unescape([string=’’])

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

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'

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

Splits string into an array of its words.

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']