_.escape(string)

Converts the characters &, <, >, ", and ' in string to their corresponding HTML entities.

Arguments

  1. string (String): The string to escape.

Returns

(String): Returns the escaped string.

Example

  1. _.escape('Moe, Larry & Curly');
  2. // => 'Moe, Larry &amp; Curly'

_.identity(value)

This method returns the first argument passed to it.

Arguments

  1. value (Mixed): Any value.

Returns

(Mixed): Returns value.

Example

  1. var moe = { 'name': 'moe' };
  2. moe === _.identity(moe);
  3. // => true

_.mixin(object)

Adds functions properties of object to the lodash function and chainable wrapper.

Arguments

  1. object (Object): The object of function properties to add to lodash.

Example

  1. _.mixin({
  2. 'capitalize': function(string) {
  3. return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  4. }
  5. });
  6. _.capitalize('moe');
  7. // => 'Moe'
  8. _('moe').capitalize();
  9. // => 'Moe'

_.noConflict()

Reverts the ‘_’ variable to its previous value and returns a reference to the lodash function.

Returns

(Function): Returns the lodash function.

Example

  1. var lodash = _.noConflict();

_.parseInt(value [, radix])

Converts the given value into an integer of the specified radix. If radix is undefined or 0, a radix of 10 is used unless the value is a hexadecimal, in which case a radix of 16 is used.

Note: This method avoids differences in native ES3 and ES5 parseInt implementations. See http://es5.github.com/#E.

Arguments

  1. value (String): The value to parse.
  2. [radix] (Number): The radix used to interpret the value to parse.

Returns

(Number): Returns the new integer value.

Example

  1. _.parseInt('08');
  2. // => 8

_.random([min=0, max=1])

Produces a random number between min and max (inclusive). If only one argument is passed, a number between 0 and the given number will be returned.

Arguments

  1. [min=0] (Number): The minimum possible value.
  2. [max=1] (Number): The maximum possible value.

Returns

(Number): Returns a random number.

Example

  1. _.random(0, 5);
  2. // => a number between 0 and 5
  3. _.random(5);
  4. // => also a number between 0 and 5

_.result(object, property)

Resolves the value of property on object. If property is a function, it will be invoked with the this binding of object and its result returned, else the property value is returned. If object is falsey, then undefined is returned.

Arguments

  1. object (Object): The object to inspect.
  2. property (String): The property to get the value of.

Returns

(Mixed): Returns the resolved value.

Example

  1. var object = {
  2. 'cheese': 'crumpets',
  3. 'stuff': function() {
  4. return 'nonsense';
  5. }
  6. };
  7. _.result(object, 'cheese');
  8. // => 'crumpets'
  9. _.result(object, 'stuff');
  10. // => 'nonsense'

_.runInContext([context=window])

Create a new lodash function using the given context object.

Arguments

  1. [context=window] (Object): The context object.

Returns

(Function): Returns the lodash function.


_.template(text, data, options)

A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code.

Note: In the development build, _.template utilizes sourceURLs for easier debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl

For more information on precompiling templates see:
http://lodash.com/#custom-builds

For more information on Chrome extension sandboxes see:
http://developer.chrome.com/stable/extensions/sandboxingEval.html

Arguments

  1. text (String): The template text.
  2. data (Object): The data object used to populate the text.
  3. options (Object): The options object. escape - The “escape” delimiter regexp. evaluate - The “evaluate” delimiter regexp. interpolate - The “interpolate” delimiter regexp. sourceURL - The sourceURL of the template’s compiled source. variable - The data object variable name.

Returns

(Function, String): Returns a compiled function when no data object is given, else it returns the interpolated text.

Example

  1. // using a compiled template
  2. var compiled = _.template('hello <%= name %>');
  3. compiled({ 'name': 'moe' });
  4. // => 'hello moe'
  5. var list = '<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>';
  6. _.template(list, { 'people': ['moe', 'larry'] });
  7. // => '<li>moe</li><li>larry</li>'
  8. // using the "escape" delimiter to escape HTML in data property values
  9. _.template('<b><%- value %></b>', { 'value': '<script>' });
  10. // => '<b>&lt;script&gt;</b>'
  11. // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
  12. _.template('hello ${ name }', { 'name': 'curly' });
  13. // => 'hello curly'
  14. // using the internal `print` function in "evaluate" delimiters
  15. _.template('<% print("hello " + epithet); %>!', { 'epithet': 'stooge' });
  16. // => 'hello stooge!'
  17. // using custom template delimiters
  18. _.templateSettings = {
  19. 'interpolate': /{{([\s\S]+?)}}/g
  20. };
  21. _.template('hello {{ name }}!', { 'name': 'mustache' });
  22. // => 'hello mustache!'
  23. // using the `sourceURL` option to specify a custom sourceURL for the template
  24. var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
  25. compiled(data);
  26. // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  27. // using the `variable` option to ensure a with-statement isn't used in the compiled template
  28. var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
  29. compiled.source;
  30. // => function(data) {
  31. var __t, __p = '', __e = _.escape;
  32. __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
  33. return __p;
  34. }
  35. // using the `source` property to inline compiled templates for meaningful
  36. // line numbers in error messages and a stack trace
  37. fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  38. var JST = {\
  39. "main": ' + _.template(mainText).source + '\
  40. };\
  41. ');

_.times(n, callback [, thisArg])

Executes the callback function n times, returning an array of the results of each callback execution. The callback is bound to thisArg and invoked with one argument; (index).

Arguments

  1. n (Number): The number of times to execute the callback.
  2. callback (Function): The function called per iteration.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array): Returns a new array of the results of each callback execution.

Example

  1. var diceRolls = _.times(3, _.partial(_.random, 1, 6));
  2. // => [3, 6, 4]
  3. _.times(3, function(n) { mage.castSpell(n); });
  4. // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
  5. _.times(3, function(n) { this.cast(n); }, mage);
  6. // => also calls `mage.castSpell(n)` three times

_.unescape(string)

The inverse of _.escape, this method converts the HTML entities &amp;, &lt;, &gt;, &quot;, and &#39; in string to their corresponding characters.

Arguments

  1. string (String): The string to unescape.

Returns

(String): Returns the unescaped string.

Example

  1. _.unescape('Moe, Larry &amp; Curly');
  2. // => 'Moe, Larry & Curly'

_.uniqueId([prefix])

Generates a unique ID. If prefix is passed, the ID will be appended to it.

Arguments

  1. [prefix] (String): The value to prefix the ID with.

Returns

(String): Returns the unique ID.

Example

  1. _.uniqueId('contact_');
  2. // => 'contact_104'
  3. _.uniqueId();
  4. // => '105'