PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/underscore/index.html

http://github.com/brikis98/node-backbone-skeleton
HTML | 1560 lines | 1397 code | 162 blank | 1 comment | 0 complexity | f2e57e09c40c9c08377c041242d7f65d MD5 | raw file
Possible License(s): MIT

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

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