PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/nano/node_modules/underscore/index.html

http://github.com/gradus/cupcake-demo
HTML | 1777 lines | 1601 code | 175 blank | 1 comment | 0 complexity | 5dc80fb28d11932617fc2f653ff6fc12 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="chrome=1">
  6. <title>Underscore.js</title>
  7. <style>
  8. body {
  9. font-size: 16px;
  10. line-height: 24px;
  11. background: #f0f0e5;
  12. color: #252519;
  13. font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
  14. }
  15. div.container {
  16. width: 720px;
  17. margin: 50px 0 50px 50px;
  18. }
  19. p {
  20. width: 550px;
  21. }
  22. #documentation p {
  23. margin-bottom: 4px;
  24. }
  25. a, a:visited {
  26. padding: 0 2px;
  27. text-decoration: none;
  28. background: #dadaba;
  29. color: #252519;
  30. }
  31. a:active, a:hover {
  32. color: #000;
  33. background: #f0c095;
  34. }
  35. h1, h2, h3, h4, h5, h6 {
  36. margin-top: 40px;
  37. }
  38. b.header {
  39. font-size: 18px;
  40. }
  41. span.alias {
  42. font-size: 14px;
  43. font-style: italic;
  44. margin-left: 20px;
  45. }
  46. table, tr, td {
  47. margin: 0; padding: 0;
  48. }
  49. td {
  50. padding: 2px 12px 2px 0;
  51. }
  52. ul {
  53. list-style-type: circle;
  54. padding: 0 0 0 20px;
  55. }
  56. li {
  57. width: 500px;
  58. margin-bottom: 10px;
  59. }
  60. code, pre, tt {
  61. font-family: Monaco, Consolas, "Lucida Console", monospace;
  62. font-size: 12px;
  63. line-height: 18px;
  64. color: #555529;
  65. }
  66. code {
  67. margin-left: 20px;
  68. }
  69. pre {
  70. font-size: 12px;
  71. padding: 2px 0 2px 12px;
  72. border-left: 6px solid #aaaa99;
  73. margin: 0px 0 30px;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <div class="container">
  79. <h1>Underscore.js</h1>
  80. <p>
  81. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  82. utility-belt library for JavaScript that provides a lot of the
  83. functional programming support that you would expect in
  84. <a href="http://prototypejs.org/api">Prototype.js</a>
  85. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  86. but without extending any of the built-in JavaScript objects. It's the
  87. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
  88. </p>
  89. <p>
  90. Underscore provides 60-odd functions that support both the usual
  91. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  92. as well as more specialized helpers: function binding, javascript
  93. templating, deep equality testing, and so on. It delegates to built-in
  94. functions, if present, so modern browsers will use the
  95. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  96. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  97. </p>
  98. <p>
  99. A complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
  100. is included for your perusal.
  101. </p>
  102. <p>
  103. You may also read through the <a href="docs/underscore.html">annotated source code</a>.
  104. </p>
  105. <p>
  106. The project is
  107. <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
  108. You can report bugs and discuss features on the
  109. <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
  110. on Freenode in the <tt>#documentcloud</tt> channel,
  111. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  112. </p>
  113. <p>
  114. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  115. </p>
  116. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  117. <table>
  118. <tr>
  119. <td><a href="underscore.js">Development Version (1.2.3)</a></td>
  120. <td><i>34kb, Uncompressed with Comments</i></td>
  121. </tr>
  122. <tr>
  123. <td><a href="underscore-min.js">Production Version (1.2.3)</a></td>
  124. <td><i>&lt; 4kb, Minified and Gzipped</i></td>
  125. </tr>
  126. </table>
  127. <h2>Table of Contents</h2>
  128. <a href="#styles">Object-Oriented and Functional Styles</a>
  129. <p>
  130. <b>Collections</b>
  131. <br />
  132. <span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
  133. <a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
  134. <a href="#find">find</a>, <a href="#filter">filter</a>,
  135. <a href="#reject">reject</a>, <a href="#all">all</a>,
  136. <a href="#any">any</a>, <a href="#include">include</a>,
  137. <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
  138. <a href="#max">max</a>, <a href="#min">min</a>,
  139. <a href="#sortBy">sortBy</a>, <a href="#groupBy">groupBy</a>,
  140. <a href="#sortedIndex">sortedIndex</a>, <a href="#shuffle">shuffle</a>,
  141. <a href="#toArray">toArray</a>, <a href="#size">size</a></span>
  142. </p>
  143. <p>
  144. <b>Arrays</b>
  145. <br />
  146. <span class="methods"><a href="#first">first</a>, <a href="#initial">initial</a>, <a href="#last">last</a>, <a href="#rest">rest</a>,
  147. <a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>,
  148. <a href="#union">union</a>, <a href="#intersection">intersection</a>, <a href="#difference">difference</a>,
  149. <a href="#uniq">uniq</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a>,
  150. <a href="#lastIndexOf">lastIndexOf</a>, <a href="#range">range</a></span>
  151. </p>
  152. <p>
  153. <b>Functions</b>
  154. <br />
  155. <span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>,
  156. <a href="#memoize">memoize</a>, <a href="#delay">delay</a>, <a href="#defer">defer</a>,
  157. <a href="#throttle">throttle</a>, <a href="#debounce">debounce</a>,
  158. <a href="#once">once</a>, <a href="#after">after</a>, <a href="#wrap">wrap</a>, <a href="#compose">compose</a></span>
  159. </p>
  160. <p>
  161. <b>Objects</b>
  162. <br />
  163. <span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
  164. <a href="#functions">functions</a>, <a href="#extend">extend</a>, <a href="#defaults">defaults</a>, <a href="#clone">clone</a>, <a href="#tap">tap</a>,
  165. <a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
  166. <a href="#isArray">isArray</a>, <a href="#isArguments">isArguments</a>, <a href="#isFunction">isFunction</a>, <a href="#isString">isString</a>,
  167. <a href="#isNumber">isNumber</a>, <a href="#isBoolean">isBoolean</a>, <a href="#isDate">isDate</a>, <a href="#isRegExp">isRegExp</a>,
  168. <a href="#isNaN">isNaN</a>, <a href="#isNull">isNull</a>,
  169. <a href="#isUndefined">isUndefined</a>
  170. </span>
  171. </p>
  172. <p>
  173. <b>Utility</b>
  174. <br />
  175. <span class="methods"><a href="#noConflict">noConflict</a>,
  176. <a href="#identity">identity</a>, <a href="#times">times</a>,
  177. <a href="#mixin">mixin</a>, <a href="#uniqueId">uniqueId</a>,
  178. <a href="#escape">escape</a>, <a href="#template">template</a></span>
  179. </p>
  180. <p>
  181. <b>Chaining</b>
  182. <br />
  183. <span class="methods"><a href="#chain">chain</a>, <a href="#value">value</a>
  184. </p>
  185. <div id="documentation">
  186. <h2 id="styles">Object-Oriented and Functional Styles</h2>
  187. <p>
  188. You can use Underscore in either an object-oriented or a functional style,
  189. depending on your preference. The following two lines of code are
  190. identical ways to double a list of numbers.
  191. </p>
  192. <pre>
  193. _.map([1, 2, 3], function(n){ return n * 2; });
  194. _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
  195. <p>
  196. Using the object-oriented style allows you to chain together methods. Calling
  197. <tt>chain</tt> on a wrapped object will cause all future method calls to
  198. return wrapped objects as well. When you've finished the computation,
  199. use <tt>value</tt> to retrieve the final value. Here's an example of chaining
  200. together a <b>map/flatten/reduce</b>, in order to get the word count of
  201. every word in a song.
  202. </p>
  203. <pre>
  204. var lyrics = [
  205. {line : 1, words : "I'm a lumberjack and I'm okay"},
  206. {line : 2, words : "I sleep all night and I work all day"},
  207. {line : 3, words : "He's a lumberjack and he's okay"},
  208. {line : 4, words : "He sleeps all night and he works all day"}
  209. ];
  210. _(lyrics).chain()
  211. .map(function(line) { return line.words.split(' '); })
  212. .flatten()
  213. .reduce(function(counts, word) {
  214. counts[word] = (counts[word] || 0) + 1;
  215. return counts;
  216. }, {}).value();
  217. =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
  218. <p>
  219. In addition, the
  220. <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/prototype">Array prototype's methods</a>
  221. are proxied through the chained Underscore object, so you can slip a
  222. <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
  223. modify the array.
  224. </p>
  225. <h2>Collection Functions (Arrays or Objects)</h2>
  226. <p id="each">
  227. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  228. <span class="alias">Alias: <b>forEach</b></span>
  229. <br />
  230. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  231. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  232. passed. Each invocation of <b>iterator</b> is called with three arguments:
  233. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  234. arguments will be <tt>(value, key, list)</tt>. Delegates to the native
  235. <b>forEach</b> function if it exists.
  236. </p>
  237. <pre>
  238. _.each([1, 2, 3], function(num){ alert(num); });
  239. =&gt; alerts each number in turn...
  240. _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
  241. =&gt; alerts each number in turn...</pre>
  242. <p id="map">
  243. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  244. <br />
  245. Produces a new array of values by mapping each value in <b>list</b>
  246. through a transformation function (<b>iterator</b>). If the native <b>map</b> method
  247. exists, it will be used instead. If <b>list</b> is a JavaScript object,
  248. <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
  249. </p>
  250. <pre>
  251. _.map([1, 2, 3], function(num){ return num * 3; });
  252. =&gt; [3, 6, 9]
  253. _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
  254. =&gt; [3, 6, 9]</pre>
  255. <p id="reduce">
  256. <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
  257. <span class="alias">Aliases: <b>inject, foldl</b></span>
  258. <br />
  259. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  260. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  261. of the reduction, and each successive step of it should be returned by
  262. <b>iterator</b>.
  263. </p>
  264. <pre>
  265. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  266. =&gt; 6
  267. </pre>
  268. <p id="reduceRight">
  269. <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
  270. <span class="alias">Alias: <b>foldr</b></span>
  271. <br />
  272. The right-associative version of <b>reduce</b>. Delegates to the
  273. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  274. is not as useful in JavaScript as it would be in a language with lazy
  275. evaluation.
  276. </p>
  277. <pre>
  278. var list = [[0, 1], [2, 3], [4, 5]];
  279. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  280. =&gt; [4, 5, 2, 3, 0, 1]
  281. </pre>
  282. <p id="find">
  283. <b class="header">find</b><code>_.find(list, iterator, [context])</code>
  284. <span class="alias">Alias: <b>detect</b></span>
  285. <br />
  286. Looks through each value in the <b>list</b>, returning the first one that
  287. passes a truth test (<b>iterator</b>). The function returns as
  288. soon as it finds an acceptable element, and doesn't traverse the
  289. entire list.
  290. </p>
  291. <pre>
  292. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  293. =&gt; 2
  294. </pre>
  295. <p id="filter">
  296. <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
  297. <span class="alias">Alias: <b>select</b></span>
  298. <br />
  299. Looks through each value in the <b>list</b>, returning an array of all
  300. the values that pass a truth test (<b>iterator</b>). Delegates to the
  301. native <b>filter</b> method, if it exists.
  302. </p>
  303. <pre>
  304. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  305. =&gt; [2, 4, 6]
  306. </pre>
  307. <p id="reject">
  308. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  309. <br />
  310. Returns the values in <b>list</b> without the elements that the truth
  311. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
  312. </p>
  313. <pre>
  314. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  315. =&gt; [1, 3, 5]
  316. </pre>
  317. <p id="all">
  318. <b class="header">all</b><code>_.all(list, iterator, [context])</code>
  319. <span class="alias">Alias: <b>every</b></span>
  320. <br />
  321. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  322. truth test. Delegates to the native method <b>every</b>, if present.
  323. </p>
  324. <pre>
  325. _.all([true, 1, null, 'yes'], _.identity);
  326. =&gt; false
  327. </pre>
  328. <p id="any">
  329. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  330. <span class="alias">Alias: <b>some</b></span>
  331. <br />
  332. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  333. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  334. if a true element is found. Delegates to the native method <b>some</b>,
  335. if present.
  336. </p>
  337. <pre>
  338. _.any([null, 0, 'yes', false]);
  339. =&gt; true
  340. </pre>
  341. <p id="include">
  342. <b class="header">include</b><code>_.include(list, value)</code>
  343. <span class="alias">Alias: <b>contains</b></span>
  344. <br />
  345. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
  346. <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
  347. is an Array.
  348. </p>
  349. <pre>
  350. _.include([1, 2, 3], 3);
  351. =&gt; true
  352. </pre>
  353. <p id="invoke">
  354. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  355. <br />
  356. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  357. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  358. method invocation.
  359. </p>
  360. <pre>
  361. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  362. =&gt; [[1, 5, 7], [1, 2, 3]]
  363. </pre>
  364. <p id="pluck">
  365. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  366. <br />
  367. A convenient version of what is perhaps the most common use-case for
  368. <b>map</b>: extracting a list of property values.
  369. </p>
  370. <pre>
  371. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  372. _.pluck(stooges, 'name');
  373. =&gt; ["moe", "larry", "curly"]
  374. </pre>
  375. <p id="max">
  376. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  377. <br />
  378. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  379. it will be used on each value to generate the criterion by which the
  380. value is ranked.
  381. </p>
  382. <pre>
  383. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  384. _.max(stooges, function(stooge){ return stooge.age; });
  385. =&gt; {name : 'curly', age : 60};
  386. </pre>
  387. <p id="min">
  388. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  389. <br />
  390. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  391. it will be used on each value to generate the criterion by which the
  392. value is ranked.
  393. </p>
  394. <pre>
  395. var numbers = [10, 5, 100, 2, 1000];
  396. _.min(numbers);
  397. =&gt; 2
  398. </pre>
  399. <p id="sortBy">
  400. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  401. <br />
  402. Returns a sorted copy of <b>list</b>, ranked by the results of running
  403. each value through <b>iterator</b>.
  404. </p>
  405. <pre>
  406. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  407. =&gt; [5, 4, 6, 3, 1, 2]
  408. </pre>
  409. <p id="groupBy">
  410. <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
  411. <br />
  412. Splits a collection into sets, grouped by the result of running each
  413. value through <b>iterator</b>. If <b>iterator</b> is a string instead of
  414. a function, groups by the property named by <b>iterator</b> on each of
  415. the values.
  416. </p>
  417. <pre>
  418. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  419. =&gt; {1: [1.3], 2: [2.1, 2.4]}
  420. _.groupBy(['one', 'two', 'three'], 'length');
  421. =&gt; {3: ["one", "two"], 5: ["three"]}
  422. </pre>
  423. <p id="sortedIndex">
  424. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  425. <br />
  426. Uses a binary search to determine the index at which the <b>value</b>
  427. <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  428. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  429. the sort ranking of each value.
  430. </p>
  431. <pre>
  432. _.sortedIndex([10, 20, 30, 40, 50], 35);
  433. =&gt; 3
  434. </pre>
  435. <p id="shuffle">
  436. <b class="header">shuffle</b><code>_.shuffle(list)</code>
  437. <br />
  438. Returns a shuffled copy of the <b>list</b>, using a version of the
  439. <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
  440. </p>
  441. <pre>
  442. _.shuffle([1, 2, 3, 4, 5, 6]);
  443. =&gt; [4, 1, 6, 3, 5, 2]
  444. </pre>
  445. <p id="toArray">
  446. <b class="header">toArray</b><code>_.toArray(list)</code>
  447. <br />
  448. Converts the <b>list</b> (anything that can be iterated over), into a
  449. real Array. Useful for transmuting the <b>arguments</b> object.
  450. </p>
  451. <pre>
  452. (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
  453. =&gt; [1, 2, 3]
  454. </pre>
  455. <p id="size">
  456. <b class="header">size</b><code>_.size(list)</code>
  457. <br />
  458. Return the number of values in the <b>list</b>.
  459. </p>
  460. <pre>
  461. _.size({one : 1, two : 2, three : 3});
  462. =&gt; 3
  463. </pre>
  464. <h2>Array Functions</h2>
  465. <p>
  466. <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
  467. </p>
  468. <p id="first">
  469. <b class="header">first</b><code>_.first(array, [n])</code>
  470. <span class="alias">Alias: <b>head</b></span>
  471. <br />
  472. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  473. return the first <b>n</b> elements of the array.
  474. </p>
  475. <pre>
  476. _.first([5, 4, 3, 2, 1]);
  477. =&gt; 5
  478. </pre>
  479. <p id="initial">
  480. <b class="header">initial</b><code>_.initial(array, [n])</code>
  481. <br />
  482. Returns everything but the last entry of the array. Especially useful on
  483. the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
  484. from the result.
  485. </p>
  486. <pre>
  487. _.initial([5, 4, 3, 2, 1]);
  488. =&gt; [5, 4, 3, 2]
  489. </pre>
  490. <p id="last">
  491. <b class="header">last</b><code>_.last(array, [n])</code>
  492. <br />
  493. Returns the last element of an <b>array</b>. Passing <b>n</b> will return
  494. the last <b>n</b> elements of the array.
  495. </p>
  496. <pre>
  497. _.last([5, 4, 3, 2, 1]);
  498. =&gt; 1
  499. </pre>
  500. <p id="rest">
  501. <b class="header">rest</b><code>_.rest(array, [index])</code>
  502. <span class="alias">Alias: <b>tail</b></span>
  503. <br />
  504. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  505. to return the values of the array from that index onward.
  506. </p>
  507. <pre>
  508. _.rest([5, 4, 3, 2, 1]);
  509. =&gt; [4, 3, 2, 1]
  510. </pre>
  511. <p id="compact">
  512. <b class="header">compact</b><code>_.compact(array)</code>
  513. <br />
  514. Returns a copy of the <b>array</b> with all falsy values removed.
  515. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  516. <i>undefined</i> and <i>NaN</i> are all falsy.
  517. </p>
  518. <pre>
  519. _.compact([0, 1, false, 2, '', 3]);
  520. =&gt; [1, 2, 3]
  521. </pre>
  522. <p id="flatten">
  523. <b class="header">flatten</b><code>_.flatten(array)</code>
  524. <br />
  525. Flattens a nested <b>array</b> (the nesting can be to any depth).
  526. </p>
  527. <pre>
  528. _.flatten([1, [2], [3, [[[4]]]]]);
  529. =&gt; [1, 2, 3, 4];
  530. </pre>
  531. <p id="without">
  532. <b class="header">without</b><code>_.without(array, [*values])</code>
  533. <br />
  534. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  535. removed. <i>===</i> is used for the equality test.
  536. </p>
  537. <pre>
  538. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  539. =&gt; [2, 3, 4]
  540. </pre>
  541. <p id="union">
  542. <b class="header">union</b><code>_.union(*arrays)</code>
  543. <br />
  544. Computes the union of the passed-in <b>arrays</b>: the list of unique items,
  545. in order, that are present in one or more of the <b>arrays</b>.
  546. </p>
  547. <pre>
  548. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  549. =&gt; [1, 2, 3, 101, 10]
  550. </pre>
  551. <p id="intersection">
  552. <b class="header">intersection</b><code>_.intersection(*arrays)</code>
  553. <br />
  554. Computes the list of values that are the intersection of all the <b>arrays</b>.
  555. Each value in the result is present in each of the <b>arrays</b>.
  556. </p>
  557. <pre>
  558. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  559. =&gt; [1, 2]
  560. </pre>
  561. <p id="difference">
  562. <b class="header">difference</b><code>_.difference(array, *others)</code>
  563. <br />
  564. Similar to <b>without</b>, but returns the values from <b>array</b> that
  565. are not present in the <b>other</b> arrays.
  566. </p>
  567. <pre>
  568. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  569. =&gt; [1, 3, 4]
  570. </pre>
  571. <p id="uniq">
  572. <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
  573. <span class="alias">Alias: <b>unique</b></span>
  574. <br />
  575. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  576. object equality. If you know in advance that the <b>array</b> is sorted,
  577. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  578. If you want to compute unique items based on a transformation, pass an
  579. <b>iterator</b> function.
  580. </p>
  581. <pre>
  582. _.uniq([1, 2, 1, 3, 1, 4]);
  583. =&gt; [1, 2, 3, 4]
  584. </pre>
  585. <p id="zip">
  586. <b class="header">zip</b><code>_.zip(*arrays)</code>
  587. <br />
  588. Merges together the values of each of the <b>arrays</b> with the
  589. values at the corresponding position. Useful when you have separate
  590. data sources that are coordinated through matching array indexes.
  591. If you're working with a matrix of nested arrays, <b>zip.apply</b>
  592. can transpose the matrix in a similar fashion.
  593. </p>
  594. <pre>
  595. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  596. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  597. </pre>
  598. <p id="indexOf">
  599. <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
  600. <br />
  601. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  602. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  603. <b>indexOf</b> function unless it's missing. If you're working with a
  604. large array, and you know that the array is already sorted, pass <tt>true</tt>
  605. for <b>isSorted</b> to use a faster binary search.
  606. </p>
  607. <pre>
  608. _.indexOf([1, 2, 3], 2);
  609. =&gt; 1
  610. </pre>
  611. <p id="lastIndexOf">
  612. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
  613. <br />
  614. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  615. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  616. function if possible.
  617. </p>
  618. <pre>
  619. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  620. =&gt; 4
  621. </pre>
  622. <p id="range">
  623. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  624. <br />
  625. A function to create flexibly-numbered lists of integers, handy for
  626. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  627. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  628. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  629. exclusive.
  630. </p>
  631. <pre>
  632. _.range(10);
  633. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  634. _.range(1, 11);
  635. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  636. _.range(0, 30, 5);
  637. =&gt; [0, 5, 10, 15, 20, 25]
  638. _.range(0, -10, -1);
  639. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  640. _.range(0);
  641. =&gt; []
  642. </pre>
  643. <h2>Function (uh, ahem) Functions</h2>
  644. <p id="bind">
  645. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  646. <br />
  647. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  648. the function is called, the value of <i>this</i> will be the <b>object</b>.
  649. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  650. also known as <b>currying</b>.
  651. </p>
  652. <pre>
  653. var func = function(greeting){ return greeting + ': ' + this.name };
  654. func = _.bind(func, {name : 'moe'}, 'hi');
  655. func();
  656. =&gt; 'hi: moe'
  657. </pre>
  658. <p id="bindAll">
  659. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  660. <br />
  661. Binds a number of methods on the <b>object</b>, specified by
  662. <b>methodNames</b>, to be run in the context of that object whenever they
  663. are invoked. Very handy for binding functions that are going to be used
  664. as event handlers, which would otherwise be invoked with a fairly useless
  665. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  666. function properties will be bound to it.
  667. </p>
  668. <pre>
  669. var buttonView = {
  670. label : 'underscore',
  671. onClick : function(){ alert('clicked: ' + this.label); },
  672. onHover : function(){ console.log('hovering: ' + this.label); }
  673. };
  674. _.bindAll(buttonView);
  675. jQuery('#underscore_button').bind('click', buttonView.onClick);
  676. =&gt; When the button is clicked, this.label will have the correct value...
  677. </pre>
  678. <p id="memoize">
  679. <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
  680. <br />
  681. Memoizes a given <b>function</b> by caching the computed result. Useful
  682. for speeding up slow-running computations. If passed an optional
  683. <b>hashFunction</b>, it will be used to compute the hash key for storing
  684. the result, based on the arguments to the original function. The default
  685. <b>hashFunction</b> just uses the first argument to the memoized function
  686. as the key.
  687. </p>
  688. <pre>
  689. var fibonacci = _.memoize(function(n) {
  690. return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  691. });
  692. </pre>
  693. <p id="delay">
  694. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  695. <br />
  696. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  697. milliseconds. If you pass the optional <b>arguments</b>, they will be
  698. forwarded on to the <b>function</b> when it is invoked.
  699. </p>
  700. <pre>
  701. var log = _.bind(console.log, console);
  702. _.delay(log, 1000, 'logged later');
  703. =&gt; 'logged later' // Appears after one second.
  704. </pre>
  705. <p id="defer">
  706. <b class="header">defer</b><code>_.defer(function)</code>
  707. <br />
  708. Defers invoking the <b>function</b> until the current call stack has cleared,
  709. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  710. expensive computations or HTML rendering in chunks without blocking the UI thread
  711. from updating.
  712. </p>
  713. <pre>
  714. _.defer(function(){ alert('deferred'); });
  715. // Returns from the function before the alert runs.
  716. </pre>
  717. <p id="throttle">
  718. <b class="header">throttle</b><code>_.throttle(function, wait)</code>
  719. <br />
  720. Returns a throttled version of the function, that, when invoked repeatedly,
  721. will only actually call the wrapped function at most once per every <b>wait</b>
  722. milliseconds. Useful for rate-limiting events that occur faster than you
  723. can keep up with.
  724. </p>
  725. <pre>
  726. var throttled = _.throttle(updatePosition, 100);
  727. $(window).scroll(throttled);
  728. </pre>
  729. <p id="debounce">
  730. <b class="header">debounce</b><code>_.debounce(function, wait)</code>
  731. <br />
  732. Calling a debounced function will postpone its execution until after
  733. <b>wait</b> milliseconds have elapsed since the last time the function
  734. was invoked. Useful for implementing behavior that should only happen
  735. <i>after</i> the input has stopped arriving. For example: rendering a
  736. preview of a Markdown comment, recalculating a layout after the window
  737. has stopped being resized...
  738. </p>
  739. <pre>
  740. var lazyLayout = _.debounce(calculateLayout, 300);
  741. $(window).resize(lazyLayout);
  742. </pre>
  743. <p id="once">
  744. <b class="header">once</b><code>_.once(function)</code>
  745. <br />
  746. Creates a version of the function that can only be called one time.
  747. Repeated calls to the modified function will have no effect, returning
  748. the value from the original call. Useful for initialization functions,
  749. instead of having to set a boolean flag and then check it later.
  750. </p>
  751. <pre>
  752. var initialize = _.once(createApplication);
  753. initialize();
  754. initialize();
  755. // Application is only created once.
  756. </pre>
  757. <p id="after">
  758. <b class="header">after</b><code>_.after(count, function)</code>
  759. <br />
  760. Creates a version of the function that will only be run after first
  761. being called <b>count</b> times. Useful for grouping asynchronous responses,
  762. where you want to be sure that all the async calls have finished, before
  763. proceeding.
  764. </p>
  765. <pre>
  766. var renderNotes = _.after(notes.length, render);
  767. _.each(notes, function(note) {
  768. note.asyncSave({success: renderNotes});
  769. });
  770. // renderNotes is run once, after all notes have saved.
  771. </pre>
  772. <p id="wrap">
  773. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  774. <br />
  775. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  776. passing it as the first argument. This allows the <b>wrapper</b> to
  777. execute code before and after the <b>function</b> runs, adjust the arguments,
  778. and execute it conditionally.
  779. </p>
  780. <pre>
  781. var hello = function(name) { return "hello: " + name; };
  782. hello = _.wrap(hello, function(func) {
  783. return "before, " + func("moe") + ", after";
  784. });
  785. hello();
  786. =&gt; 'before, hello: moe, after'
  787. </pre>
  788. <p id="compose">
  789. <b class="header">compose</b><code>_.compose(*functions)</code>
  790. <br />
  791. Returns the composition of a list of <b>functions</b>, where each function
  792. consumes the return value of the function that follows. In math terms,
  793. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  794. <i>f(g(h()))</i>.
  795. </p>
  796. <pre>
  797. var greet = function(name){ return "hi: " + name; };
  798. var exclaim = function(statement){ return statement + "!"; };
  799. var welcome = _.compose(exclaim, greet);
  800. welcome('moe');
  801. =&gt; 'hi: moe!'
  802. </pre>
  803. <h2>Object Functions</h2>
  804. <p id="keys">
  805. <b class="header">keys</b><code>_.keys(object)</code>
  806. <br />
  807. Retrieve all the names of the <b>object</b>'s properties.
  808. </p>
  809. <pre>
  810. _.keys({one : 1, two : 2, three : 3});
  811. =&gt; ["one", "two", "three"]
  812. </pre>
  813. <p id="values">
  814. <b class="header">values</b><code>_.values(object)</code>
  815. <br />
  816. Return all of the values of the <b>object</b>'s properties.
  817. </p>
  818. <pre>
  819. _.values({one : 1, two : 2, three : 3});
  820. =&gt; [1, 2, 3]
  821. </pre>
  822. <p id="functions">
  823. <b class="header">functions</b><code>_.functions(object)</code>
  824. <span class="alias">Alias: <b>methods</b></span>
  825. <br />
  826. Returns a sorted list of the names of every method in an object &mdash;
  827. that is to say, the name of every function property of the object.
  828. </p>
  829. <pre>
  830. _.functions(_);
  831. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  832. </pre>
  833. <p id="extend">
  834. <b class="header">extend</b><code>_.extend(destination, *sources)</code>
  835. <br />
  836. Copy all of the properties in the <b>source</b> objects over to the
  837. <b>destination</b> object. It's in-order, so the last source will override
  838. properties of the same name in previous arguments.
  839. </p>
  840. <pre>
  841. _.extend({name : 'moe'}, {age : 50});
  842. =&gt; {name : 'moe', age : 50}
  843. </pre>
  844. <p id="defaults">
  845. <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
  846. <br />
  847. Fill in missing properties in <b>object</b> with default values from the
  848. <b>defaults</b> objects. As soon as the property is filled, further defaults
  849. will have no effect.
  850. </p>
  851. <pre>
  852. var iceCream = {flavor : "chocolate"};
  853. _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
  854. =&gt; {flavor : "chocolate", sprinkles : "lots"}
  855. </pre>
  856. <p id="clone">
  857. <b class="header">clone</b><code>_.clone(object)</code>
  858. <br />
  859. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  860. or arrays will be copied by reference, not duplicated.
  861. </p>
  862. <pre>
  863. _.clone({name : 'moe'});
  864. =&gt; {name : 'moe'};
  865. </pre>
  866. <p id="tap">
  867. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  868. <br />
  869. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  870. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  871. </p>
  872. <pre>
  873. _([1,2,3,200]).chain().
  874. filter(function(num) { return num % 2 == 0; }).
  875. tap(console.log).
  876. map(function(num) { return num * num }).
  877. value();
  878. =&gt; [2, 200]
  879. =&gt; [4, 40000]
  880. </pre>
  881. <p id="isEqual">
  882. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  883. <br />
  884. Performs an optimized deep comparison between the two objects, to determine
  885. if they should be considered equal.
  886. </p>
  887. <pre>
  888. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  889. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  890. moe == clone;
  891. =&gt; false
  892. _.isEqual(moe, clone);
  893. =&gt; true
  894. </pre>
  895. <p id="isEmpty">
  896. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  897. <br />
  898. Returns <i>true</i> if <b>object</b> contains no values.
  899. </p>
  900. <pre>
  901. _.isEmpty([1, 2, 3]);
  902. =&gt; false
  903. _.isEmpty({});
  904. =&gt; true
  905. </pre>
  906. <p id="isElement">
  907. <b class="header">isElement</b><code>_.isElement(object)</code>
  908. <br />
  909. Returns <i>true</i> if <b>object</b> is a DOM element.
  910. </p>
  911. <pre>
  912. _.isElement(jQuery('body')[0]);
  913. =&gt; true
  914. </pre>
  915. <p id="isArray">
  916. <b class="header">isArray</b><code>_.isArray(object)</code>
  917. <br />
  918. Returns <i>true</i> if <b>object</b> is an Array.
  919. </p>
  920. <pre>
  921. (function(){ return _.isArray(arguments); })();
  922. =&gt; false
  923. _.isArray([1,2,3]);
  924. =&gt; true
  925. </pre>
  926. <p id="isArguments">
  927. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  928. <br />
  929. Returns <i>true</i> if <b>object</b> is an Arguments object.
  930. </p>
  931. <pre>
  932. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  933. =&gt; true
  934. _.isArguments([1,2,3]);
  935. =&gt; false
  936. </pre>
  937. <p id="isFunction">
  938. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  939. <br />
  940. Returns <i>true</i> if <b>object</b> is a Function.
  941. </p>
  942. <pre>
  943. _.isFunction(alert);
  944. =&gt; true
  945. </pre>
  946. <p id="isString">
  947. <b class="header">isString</b><code>_.isString(object)</code>
  948. <br />
  949. Returns <i>true</i> if <b>object</b> is a String.
  950. </p>
  951. <pre>
  952. _.isString("moe");
  953. =&gt; true
  954. </pre>
  955. <p id="isNumber">
  956. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  957. <br />
  958. Returns <i>true</i> if <b>object</b> is a Number.
  959. </p>
  960. <pre>
  961. _.isNumber(8.4 * 5);
  962. =&gt; true
  963. </pre>
  964. <p id="isBoolean">
  965. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  966. <br />
  967. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  968. </p>
  969. <pre>
  970. _.isBoolean(null);
  971. =&gt; false
  972. </pre>
  973. <p id="isDate">
  974. <b class="header">isDate</b><code>_.isDate(object)</code>
  975. <br />
  976. Returns <i>true</i> if <b>object</b> is a Date.
  977. </p>
  978. <pre>
  979. _.isDate(new Date());
  980. =&gt; true
  981. </pre>
  982. <p id="isRegExp">
  983. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  984. <br />
  985. Returns <i>true</i> if <b>object</b> is a RegExp.
  986. </p>
  987. <pre>
  988. _.isRegExp(/moe/);
  989. =&gt; true
  990. </pre>
  991. <p id="isNaN">
  992. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  993. <br />
  994. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  995. the same as the native <b>isNaN</b> function, which will also return
  996. true if the variable is <i>undefined</i>.
  997. </p>
  998. <pre>
  999. _.isNaN(NaN);
  1000. =&gt; true
  1001. isNaN(undefined);
  1002. =&gt; true
  1003. _.isNaN(undefined);
  1004. =&gt; false
  1005. </pre>
  1006. <p id="isNull">
  1007. <b class="header">isNull</b><code>_.isNull(object)</code>
  1008. <br />
  1009. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  1010. </p>
  1011. <pre>
  1012. _.isNull(null);
  1013. =&gt; true
  1014. _.isNull(undefined);
  1015. =&gt; false
  1016. </pre>
  1017. <p id="isUndefined">
  1018. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  1019. <br />
  1020. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  1021. </p>
  1022. <pre>
  1023. _.isUndefined(window.missingVariable);
  1024. =&gt; true
  1025. </pre>
  1026. <h2>Utility Functions</h2>
  1027. <p id="noConflict">
  1028. <b class="header">noConflict</b><code>_.noConflict()</code>
  1029. <br />
  1030. Give control of the "_" variable back to its previous owner. Returns
  1031. a reference to the <b>Underscore</b> object.
  1032. </p>
  1033. <pre>
  1034. var underscore = _.noConflict();</pre>
  1035. <p id="identity">
  1036. <b class="header">identity</b><code>_.identity(value)</code>
  1037. <br />
  1038. Returns the same value that is used as the argument. In math:
  1039. <tt>f(x) = x</tt><br />
  1040. This function looks useless, but is used throughout Underscore as
  1041. a default iterator.
  1042. </p>
  1043. <pre>
  1044. var moe = {name : 'moe'};
  1045. moe === _.identity(moe);
  1046. =&gt; true</pre>
  1047. <p id="times">
  1048. <b class="header">times</b><code>_.times(n, iterator)</code>
  1049. <br />
  1050. Invokes the given iterator function <b>n</b> times.
  1051. </p>
  1052. <pre>
  1053. _(3).times(function(){ genie.grantWish(); });</pre>
  1054. <p id="mixin">
  1055. <b class="header">mixin</b><code>_.mixin(object)</code>
  1056. <br />
  1057. Allows you to extend Underscore with your own utility functions. Pass
  1058. a hash of <tt>{name: function}</tt> definitions to have your functions
  1059. added to the Underscore object, as well as the OOP wrapper.
  1060. </p>
  1061. <pre>
  1062. _.mixin({
  1063. capitalize : function(string) {
  1064. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  1065. }
  1066. });
  1067. _("fabio").capitalize();
  1068. =&gt; "Fabio"
  1069. </pre>
  1070. <p id="uniqueId">
  1071. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  1072. <br />
  1073. Generate a globally-unique id for client-side models or DOM elements
  1074. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  1075. </p>
  1076. <pre>
  1077. _.uniqueId('contact_');
  1078. =&gt; 'contact_104'</pre>
  1079. <p id="escape">
  1080. <b class="header">escape</b><code>_.escape(string)</code>
  1081. <br />
  1082. Escapes a string for insertion into HTML, replacing
  1083. <tt>&amp;</tt>, <tt>&lt;</tt>, <tt>&gt;</tt>, <tt>&quot;</tt>, <tt>&#x27;</tt>, and <tt>&#x2F;</tt> characters.
  1084. </p>
  1085. <pre>
  1086. _.escape('Curly, Larry &amp; Moe');
  1087. =&gt; "Curly, Larry &amp;amp; Moe"</pre>
  1088. <p id="template">
  1089. <b class="header">template</b><code>_.template(templateString, [context])</code>
  1090. <br />
  1091. Compiles JavaScript templates into functions that can be evaluated
  1092. for rendering. Useful for rendering complicated bits of HTML from JSON
  1093. data sources. Template functions can both interpolate variables, using<br />
  1094. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  1095. <tt>&lt;% &hellip; %&gt;</tt>. If you wish to interpolate a value, and have
  1096. it be HTML-escaped, use <tt>&lt;%- &hellip; %&gt;</tt> When you evaluate a template function, pass in a
  1097. <b>context</b> object that has properties corresponding to the template's free
  1098. variables. If you're writing a one-off, you can pass the <b>context</b>
  1099. object as the second parameter to <b>template</b> in order to render
  1100. immediately instead of returning a template function.
  1101. </p>
  1102. <pre>
  1103. var compiled = _.template("hello: &lt;%= name %&gt;");
  1104. compiled({name : 'moe'});
  1105. =&gt; "hello: moe"
  1106. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  1107. _.template(list, {people : ['moe', 'curly', 'larry']});
  1108. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
  1109. var template = _.template("&lt;b&gt;&lt;%- value %&gt;&lt;/b&gt;");
  1110. template({value : '&lt;script&gt;'});
  1111. =&gt; "&lt;b&gt;&amp;lt;script&amp;gt;&lt;/b&gt;"</pre>
  1112. <p>
  1113. You can also use <tt>print</tt> from within JavaScript code. This is
  1114. sometimes more convenient than using <tt>&lt;%= ... %&gt;</tt>.
  1115. </p>
  1116. <pre>
  1117. var compiled = _.template("&lt;% print('Hello ' + epithet); %&gt;");
  1118. compiled({epithet: "stooge"});
  1119. =&gt; "Hello stooge."</pre>
  1120. <p>
  1121. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  1122. template settings to use different symbols to set off interpolated code.
  1123. Define an <b>interpolate</b> regex, and an (optional) <b>evaluate</b> regex
  1124. to match expressions that should be inserted and evaluated, respectively.
  1125. If no <b>evaluate</b> regex is provided, your templates will only be
  1126. capable of interpolating values.
  1127. For example, to perform
  1128. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  1129. style templating:
  1130. </p>
  1131. <pre>
  1132. _.templateSettings = {
  1133. interpolate : /\{\{(.+?)\}\}/g
  1134. };
  1135. var template = _.template("Hello {{ name }}!");
  1136. template({name : "Mustache"});
  1137. =&gt; "Hello Mustache!"</pre>
  1138. <h2>Chaining</h2>
  1139. <p id="chain">
  1140. <b class="header">chain</b><code>_(obj).chain()</code>
  1141. <br />
  1142. Returns a wrapped object. Calling methods on this object will continue
  1143. to return wrapped objects until <tt>value</tt> is used. (
  1144. <a href="#styles">A more realistic example.</a>)
  1145. </p>
  1146. <pre>
  1147. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  1148. var youngest = _(stooges).chain()
  1149. .sortBy(function(stooge){ return stooge.age; })
  1150. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  1151. .first()
  1152. .value();
  1153. =&gt; "moe is 21"
  1154. </pre>
  1155. <p id="value">
  1156. <b class="header">value</b><code>_(obj).value()</code>
  1157. <br />
  1158. Extracts the value of a wrapped object.
  1159. </p>
  1160. <pre>
  1161. _([1, 2, 3]).value();
  1162. =&gt; [1, 2, 3]
  1163. </pre>
  1164. <h2>Links &amp; Suggested Reading</h2>
  1165. <p>
  1166. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  1167. a Lua port of the functions that are applicable in both languages.
  1168. Includes OOP-wrapping and chaining.
  1169. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1170. available on GitHub.
  1171. </p>
  1172. <p>
  1173. <a href="http://brianhaveri.github.com/Underscore.php/">Underscore.php</a>,
  1174. a PHP port of the functions that are applicable in both languages.
  1175. Includes OOP-wrapping and chaining.
  1176. The <a href="http://github.com/brianhaveri/Underscore.php">source</a> is
  1177. available on GitHub.
  1178. </p>
  1179. <p>
  1180. <a href="http://vti.github.com/underscore-perl/">Underscore-perl</a>,
  1181. a Perl port of many of the Underscore.js functions,
  1182. aimed at on Perl hashes and arrays, also
  1183. <a href="https://github.com/vti/underscore-perl/">available on GitHub</a>.
  1184. </p>
  1185. <p>
  1186. <a href="https://github.com/edtsech/underscore.string">Underscore.string</a>,
  1187. an Underscore extension that adds functions for string-manipulation:
  1188. <tt>trim</tt>, <tt>startsWith</tt>, <tt>contains</tt>, <tt>capitalize</tt>,
  1189. <tt>reverse</tt>, <tt>sprintf</tt>, and more.
  1190. </p>
  1191. <p>
  1192. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1193. </p>
  1194. <p>
  1195. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1196. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1197. </p>
  1198. <p>
  1199. Oliver Steele's
  1200. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1201. which includes comprehensive higher-order function support as well as string lambdas.
  1202. </p>
  1203. <p>
  1204. Michael Aufreiter's <a href="http://substance.io/#michael/data-js">Data.js</a>,
  1205. a data manipulation + persistence library for JavaScript.
  1206. </p>
  1207. <p>
  1208. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1209. </p>
  1210. <h2 id="changelog">Change Log</h2>
  1211. <p>
  1212. <b class="header">1.2.3</b> &mdash; <small><i>Dec. 7, 2011</i></small><br />
  1213. <ul>
  1214. <li>
  1215. Dynamic scope is now preserved for compiled <tt>_.template</tt> functions,
  1216. so you can use the value of <tt>this</tt> if you like.
  1217. </li>
  1218. <li>
  1219. Sparse array support of <tt>_.indexOf</tt>, <tt>_.lastIndexOf</tt>.
  1220. </li>
  1221. <li>
  1222. Both <tt>_.reduce</tt> and <tt>_.reduceRight</tt> can now be passed an
  1223. explicitly <tt>undefined</tt> value. (There's no reason why you'd
  1224. want to do this.)
  1225. </li>
  1226. </ul>
  1227. </p>
  1228. <p>
  1229. <b class="header">1.2.2</b> &mdash; <small><i>Nov. 14, 2011</i></small><br />
  1230. <ul>
  1231. <li>
  1232. Continued tweaks to <tt>_.isEqual</tt> semantics. Now JS primitives are
  1233. considered equivalent to their wrapped versions, and arrays are compared
  1234. by their numeric properties only <small>(#351)</small>.
  1235. </li>
  1236. <li>
  1237. <tt>_.escape</tt> no longer tries to be smart about not double-escaping
  1238. already-escaped HTML entities. Now it just escapes regardless <small>(#350)</small>.
  1239. </li>
  1240. <li>
  1241. In <tt>_.template</tt>, you may now leave semicolons out of evaluated
  1242. statements if you wish: <tt>&lt;% }) %&gt;</tt> <small>(#369)</small>.
  1243. </li>
  1244. <li>
  1245. <tt>_.after(callback, 0)</tt> will now trigger the callback immediately,
  1246. making "after" easier to use with asynchronous APIs <small>(#366)</small>.
  1247. </li>
  1248. </ul>
  1249. </p>
  1250. <p>
  1251. <b class="header">1.2.1</b> &mdash; <small><i>Oct. 24, 2011</i></small><br />
  1252. <ul>
  1253. <li>
  1254. Several important bug fixes for <tt>_.isEqual</tt>, which should now
  1255. do better on mutated Arrays, and on non-Array objects with
  1256. <tt>length</tt> properties. <small>(#329)</small>
  1257. </li>
  1258. <li>
  1259. <b>jrburke</b> contributed Underscore exporting for AMD module loaders,
  1260. and <b>tonylukasavage</b> for Appcelerator Titanium.
  1261. <small>(#335, #338)</small>
  1262. </li>
  1263. <li>
  1264. You can now <tt>_.groupBy(list, 'property')</tt> as a shortcut for
  1265. grouping values by a particular common property.
  1266. </li>
  1267. <li>
  1268. <tt>_.throttle</tt>'d functions now fire immediately upon invocation,
  1269. and are rate-limited thereafter <small>(#170, #266)</small>.
  1270. </li>
  1271. <li>
  1272. Most of the <tt>_.is[Type]</tt> checks no longer ducktype.
  1273. </li>
  1274. <li>
  1275. The <tt>_.bind</tt> function now also works on constructors, a-la
  1276. ES5 ... but you would never want to use <tt>_.bind</tt> on a
  1277. constructor function.
  1278. </li>
  1279. <li>
  1280. <tt>_.clone</tt> no longer wraps non-object types in Objects.
  1281. </li>
  1282. <li>
  1283. <tt>_.find</tt> and <tt>_.filter</tt> are now the preferred names for
  1284. <tt>_.detect</tt> and <tt>_.select</tt>.
  1285. </li>
  1286. </ul>
  1287. </p>
  1288. <p>
  1289. <b class="header">1.2.0</b> &mdash; <small><i>Oct. 5, 2011</i></small><br />
  1290. <ul>
  1291. <li>
  1292. The <tt>_.isEqual</tt> func

Large files files are truncated, but you can click here to view the full file