PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/index.html

https://github.com/josher19/underscore.grep
HTML | 1320 lines | 1183 code | 137 blank | 0 comment | 0 complexity | f0cae9eb02459090ac2515c58e2af017 MD5 | raw 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. <script src="underscore.js"></script>
  8. <style>
  9. body {
  10. font-size: 16px;
  11. line-height: 24px;
  12. background: #f0f0e5;
  13. color: #252519;
  14. font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
  15. }
  16. div.container {
  17. width: 720px;
  18. margin: 50px 0 50px 50px;
  19. }
  20. p {
  21. width: 550px;
  22. }
  23. #documentation p {
  24. margin-bottom: 4px;
  25. }
  26. a, a:visited {
  27. padding: 0 2px;
  28. text-decoration: none;
  29. background: #dadaba;
  30. color: #252519;
  31. }
  32. a:active, a:hover {
  33. color: #000;
  34. background: #f0c095;
  35. }
  36. h1, h2, h3, h4, h5, h6 {
  37. margin-top: 40px;
  38. }
  39. b.header {
  40. font-size: 18px;
  41. }
  42. span.alias {
  43. font-size: 14px;
  44. font-style: italic;
  45. margin-left: 20px;
  46. }
  47. table, tr, td {
  48. margin: 0; padding: 0;
  49. }
  50. td {
  51. padding: 2px 12px 2px 0;
  52. }
  53. code, pre, tt {
  54. font-family: Monaco, Consolas, "Lucida Console", monospace;
  55. font-size: 12px;
  56. line-height: 18px;
  57. color: #555529;
  58. }
  59. code {
  60. margin-left: 20px;
  61. }
  62. pre {
  63. font-size: 12px;
  64. padding: 2px 0 2px 12px;
  65. border-left: 6px solid #aaaa99;
  66. margin: 0px 0 30px;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div class="container">
  72. <h1>Underscore.js</h1>
  73. <p>
  74. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  75. utility-belt library for JavaScript that provides a lot of the
  76. functional programming support that you would expect in
  77. <a href="http://prototypejs.org/api">Prototype.js</a>
  78. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  79. but without extending any of the built-in JavaScript objects. It's the
  80. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
  81. </p>
  82. <p>
  83. Underscore provides 60-odd functions that support both the usual
  84. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  85. as well as more specialized helpers: function binding, javascript
  86. templating, deep equality testing, and so on. It delegates to built-in
  87. functions, if present, so modern browsers will use the
  88. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  89. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  90. </p>
  91. <p>
  92. A complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
  93. is included for your perusal.
  94. </p>
  95. <p>
  96. The unabridged source code is
  97. <a href="http://github.com/documentcloud/underscore/">available on GitHub</a>.
  98. </p>
  99. <p>
  100. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  101. </p>
  102. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  103. <p>
  104. <table>
  105. <tr>
  106. <td><a href="underscore.js">Development Version (0.5.8)</a></td>
  107. <td><i>22kb, Uncompressed with Comments</i></td>
  108. </tr>
  109. <tr>
  110. <td><a href="underscore-min.js">Production Version (0.5.8)</a></td>
  111. <td><i>3kb, Packed and Gzipped</i></td>
  112. </tr>
  113. </table>
  114. </p>
  115. <h2 id="styles">Object-Oriented and Functional Styles</h2>
  116. <p>
  117. You can use Underscore in either an object-oriented or a functional style,
  118. depending on your preference. The following two lines of code are
  119. identical ways to double a list of numbers.
  120. </p>
  121. <pre>
  122. _.map([1, 2, 3], function(n){ return n * 2; });
  123. _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
  124. <p>
  125. Using the object-oriented style allows you to chain together methods. Calling
  126. <tt>chain</tt> on a wrapped object will cause all future method calls to
  127. return wrapped objects as well. When you've finished the computation,
  128. use <tt>value</tt> to retrieve the final value. Here's an example of chaining
  129. together a <b>map/flatten/reduce</b>, in order to get the word count of
  130. every word in a song.
  131. </p>
  132. <pre>
  133. var lyrics = [
  134. {line : 1, words : "I'm a lumberjack and I'm okay"},
  135. {line : 2, words : "I sleep all night and I work all day"},
  136. {line : 3, words : "He's a lumberjack and he's okay"},
  137. {line : 4, words : "He sleeps all night and he works all day"}
  138. ];
  139. _(lyrics).chain()
  140. .map(function(line) { return line.words.split(' '); })
  141. .flatten()
  142. .reduce({}, function(counts, word) {
  143. counts[word] = (counts[word] || 0) + 1;
  144. return counts;
  145. }).value();
  146. =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
  147. <p>
  148. In addition, the
  149. <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array prototype's methods</a>
  150. are proxied through the chained Underscore object, so you can slip a
  151. <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
  152. modify the array.
  153. </p>
  154. <h2>Table of Contents</h2>
  155. <p>
  156. <b>Collections</b>
  157. <br />
  158. <span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
  159. <a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
  160. <a href="#detect">detect</a>, <a href="#select">select</a>,
  161. <a href="#reject">reject</a>, <a href="#all">all</a>,
  162. <a href="#any">any</a>, <a href="#include">include</a>,
  163. <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
  164. <a href="#max">max</a>, <a href="#min">min</a>,
  165. <a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
  166. <a href="#toArray">toArray</a>, <a href="#size">size</a>,
  167. <a href="#grep">grep</a>
  168. </span></p>
  169. <p>
  170. <b>Arrays</b>
  171. <br />
  172. <span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
  173. <a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
  174. <a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a>,
  175. <a href="#lastIndexOf">lastIndexOf</a>, <a href="#range">range</a></span>
  176. </p>
  177. <p>
  178. <b>Functions</b>
  179. <br />
  180. <span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
  181. <a href="#defer">defer</a>, <a href="#wrap">wrap</a>, <a href="#compose">compose</a></span>
  182. </p>
  183. <p>
  184. <b>Objects</b>
  185. <br />
  186. <span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
  187. <a href="#functions">functions</a>, <a href="#extend">extend</a>, <a href="#clone">clone</a>, <a href="#tap">tap</a>,
  188. <a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
  189. <a href="#isArray">isArray</a>, <a href="#isArguments">isArguments</a>, <a href="#isFunction">isFunction</a>, <a href="#isString">isString</a>,
  190. <a href="#isNumber">isNumber</a>, <a href="#isDate">isDate</a>, <a href="#isRegExp">isRegExp</a>
  191. <a href="#isNaN">isNaN</a>, <a href="#isNull">isNull</a>,
  192. <a href="#isUndefined">isUndefined</a>
  193. </span>
  194. </p>
  195. <p>
  196. <b>Utility</b>
  197. <br />
  198. <span class="methods"><a href="#noConflict">noConflict</a>,
  199. <a href="#identity">identity</a>, <a href="#breakLoop">breakLoop</a>,
  200. <a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
  201. </p>
  202. <p>
  203. <b>Chaining</b>
  204. <br />
  205. <span class="methods"><a href="#chain">chain</a>, <a href="#value">value</a>, <a href="#item">item</a>
  206. </span>
  207. </p>
  208. <div id="documentation">
  209. <h2>Collection Functions (Arrays or Objects)</h2>
  210. <p id="each">
  211. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  212. <span class="alias">Alias: <b>forEach</b></span>
  213. <br />
  214. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  215. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  216. passed. Each invocation of <b>iterator</b> is called with three arguments:
  217. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  218. arguments will be <tt>(value, key, list)</tt>. Use <a href="#breakLoop"><tt>breakLoop</tt></a>
  219. to break out of the iteration. Delegates to the native
  220. <b>forEach</b> function if it exists.
  221. </p>
  222. <pre>
  223. _.each([1, 2, 3], function(num){ alert(num); });
  224. =&gt; alerts each number in turn...</pre>
  225. <p id="map">
  226. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  227. <br />
  228. Produces a new array of values by mapping each value in <b>list</b>
  229. through a transformation function (<b>iterator</b>). If the native
  230. <b>map</b> method exists, it will be used instead.
  231. </p>
  232. <pre>
  233. _.map([1, 2, 3], function(num){ return num * 3 });
  234. =&gt; [3, 6, 9]</pre>
  235. <p id="reduce">
  236. <b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
  237. <span class="alias">Aliases: <b>inject, foldl</b></span>
  238. <br />
  239. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  240. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  241. of the reduction, and each successive step of it should be returned by
  242. <b>iterator</b>.
  243. </p>
  244. <pre>
  245. var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
  246. =&gt; 6
  247. </pre>
  248. <p id="reduceRight">
  249. <b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code>
  250. <span class="alias">Alias: <b>foldr</b></span>
  251. <br />
  252. The right-associative version of <b>reduce</b>. Delegates to the
  253. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  254. is not as useful in JavaScript as it would be in a language with lazy
  255. evaluation.
  256. </p>
  257. <pre>
  258. var list = [[0, 1], [2, 3], [4, 5]];
  259. var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); });
  260. =&gt; [4, 5, 2, 3, 0, 1]
  261. </pre>
  262. <p id="detect">
  263. <b class="header">detect</b><code>_.detect(list, iterator, [context])</code>
  264. <br />
  265. Looks through each value in the <b>list</b>, returning the first one that
  266. passes a truth test (<b>iterator</b>). The function returns as
  267. soon as it finds an acceptable element, and doesn't traverse the
  268. entire list.
  269. </p>
  270. <pre>
  271. var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  272. =&gt; 2
  273. </pre>
  274. <p id="select">
  275. <b class="header">select</b><code>_.select(list, iterator, [context])</code>
  276. <span class="alias">Alias: <b>filter</b></span>
  277. <br />
  278. Looks through each value in the <b>list</b>, returning an array of all
  279. the values that pass a truth test (<b>iterator</b>). Delegates to the
  280. native <b>filter</b> method, if it exists.
  281. </p>
  282. <pre>
  283. var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  284. =&gt; [2, 4, 6]
  285. </pre>
  286. <p id="reject">
  287. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  288. <br />
  289. Returns the values in <b>list</b> without the elements that the truth
  290. test (<b>iterator</b>) passes. The opposite of <b>select</b>.
  291. </p>
  292. <pre>
  293. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  294. =&gt; [1, 3, 5]
  295. </pre>
  296. <p id="all">
  297. <b class="header">all</b><code>_.all(list, [iterator], [context])</code>
  298. <span class="alias">Alias: <b>every</b></span>
  299. <br />
  300. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  301. truth test. If an <b>iterator</b> is not provided, the truthy value of
  302. the element will be used instead. Delegates to the native method <b>every</b>, if
  303. present.
  304. </p>
  305. <pre>
  306. _.all([true, 1, null, 'yes']);
  307. =&gt; false
  308. </pre>
  309. <p id="any">
  310. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  311. <span class="alias">Alias: <b>some</b></span>
  312. <br />
  313. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  314. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  315. if a true element is found. Delegates to the native method <b>some</b>,
  316. if present.
  317. </p>
  318. <pre>
  319. _.any([null, 0, 'yes', false]);
  320. =&gt; true
  321. </pre>
  322. <p id="include">
  323. <b class="header">include</b><code>_.include(list, value)</code>
  324. <br />
  325. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
  326. <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
  327. is an Array.
  328. </p>
  329. <pre>
  330. _.include([1, 2, 3], 3);
  331. =&gt; true
  332. </pre>
  333. <p id="invoke">
  334. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  335. <br />
  336. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  337. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  338. method invocation.
  339. </p>
  340. <pre>
  341. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  342. =&gt; [[1, 5, 7], [1, 2, 3]]
  343. </pre>
  344. <p id="pluck">
  345. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  346. <br />
  347. An convenient version of what is perhaps the most common use-case for
  348. <b>map</b>: extracting a list of property values.
  349. </p>
  350. <pre>
  351. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  352. _.pluck(stooges, 'name');
  353. =&gt; ["moe", "larry", "curly"]
  354. </pre>
  355. <p id="max">
  356. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  357. <br />
  358. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  359. it will be used on each value to generate the criterion by which the
  360. value is ranked.
  361. </p>
  362. <pre>
  363. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  364. _.max(stooges, function(stooge){ return stooge.age; });
  365. =&gt; {name : 'curly', age : 60};
  366. </pre>
  367. <p id="min">
  368. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  369. <br />
  370. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  371. it will be used on each value to generate the criterion by which the
  372. value is ranked.
  373. </p>
  374. <pre>
  375. var numbers = [10, 5, 100, 2, 1000];
  376. _.min(numbers);
  377. =&gt; 2
  378. </pre>
  379. <p id="sortBy">
  380. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  381. <br />
  382. Returns a sorted <b>list</b>, ranked by the results of running each
  383. value through <b>iterator</b>.
  384. </p>
  385. <pre>
  386. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  387. =&gt; [5, 4, 6, 3, 1, 2]
  388. </pre>
  389. <p id="sortedIndex">
  390. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  391. <br />
  392. Uses a binary search to determine the index at which the <b>value</b>
  393. should be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  394. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  395. the sort ranking of each value.
  396. </p>
  397. <pre>
  398. _.sortedIndex([10, 20, 30, 40, 50], 35);
  399. =&gt; 3
  400. </pre>
  401. <p id="toArray">
  402. <b class="header">toArray</b><code>_.toArray(list)</code>
  403. <br />
  404. Converts the <b>list</b> (anything that can be iterated over), into a
  405. real Array. Useful for transmuting the <b>arguments</b> object.
  406. </p>
  407. <pre>
  408. (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
  409. =&gt; [1, 2, 3]
  410. </pre>
  411. <p id="size">
  412. <b class="header">size</b><code>_.size(list)</code>
  413. <br />
  414. Return the number of values in the <b>list</b>.
  415. </p>
  416. <pre>
  417. _.size({one : 1, two : 2, three : 3});
  418. =&gt; 3
  419. </pre>
  420. <p id="grep">
  421. <b class="header">grep</b><code>_.grep(obj, re, [iterator], [context])</code>
  422. <br />
  423. Returns all values that match regular expression <b>re</b> and then applies (optional) <b>iterator</b> in (optional) <b>context</b>.
  424. If <b>re</b> is not a RegExp, then it is converted to one, escaping any RegExp "special characters" such as {.( |}:)$ +?=^* !/[]\.
  425. </p>
  426. <pre>
  427. _.grep(_.range(0,31), /[03]$/)
  428. =&gt; [0,3,10,13,20,23,30]
  429. _.grep(_.range(0,31), /[03]$/, function(n) { return 2*n })
  430. =&gt; [0,6,20,26,40,46,60]
  431. </pre>
  432. <h2>Array Functions</h2>
  433. <p>
  434. <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
  435. </p>
  436. <p id="first">
  437. <b class="header">first</b><code>_.first(array, [n])</code>
  438. <span class="alias">Alias: <b>head</b></span>
  439. <br />
  440. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  441. return the first <b>n</b> elements of the array.
  442. </p>
  443. <pre>
  444. _.first([5, 4, 3, 2, 1]);
  445. =&gt; 5
  446. </pre>
  447. <p id="rest">
  448. <b class="header">rest</b><code>_.rest(array, [index])</code>
  449. <span class="alias">Alias: <b>tail</b></span>
  450. <br />
  451. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  452. to return the values of the array from that index onward.
  453. </p>
  454. <pre>
  455. _.rest([5, 4, 3, 2, 1]);
  456. =&gt; [4, 3, 2, 1]
  457. </pre>
  458. <p id="last">
  459. <b class="header">last</b><code>_.last(array)</code>
  460. <br />
  461. Returns the last element of an <b>array</b>.
  462. </p>
  463. <pre>
  464. _.last([5, 4, 3, 2, 1]);
  465. =&gt; 1
  466. </pre>
  467. <p id="compact">
  468. <b class="header">compact</b><code>_.compact(array)</code>
  469. <br />
  470. Returns a copy of the <b>array</b> with all falsy values removed.
  471. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  472. <i>undefined</i> and <i>NaN</i> are all falsy.
  473. </p>
  474. <pre>
  475. _.compact([0, 1, false, 2, '', 3]);
  476. =&gt; [1, 2, 3]
  477. </pre>
  478. <p id="flatten">
  479. <b class="header">flatten</b><code>_.flatten(array)</code>
  480. <br />
  481. Flattens a nested <b>array</b> (the nesting can be to any depth).
  482. </p>
  483. <pre>
  484. _.flatten([1, [2], [3, [[[4]]]]]);
  485. =&gt; [1, 2, 3, 4];
  486. </pre>
  487. <p id="without">
  488. <b class="header">without</b><code>_.without(array, [*values])</code>
  489. <br />
  490. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  491. removed. <i>===</i> is used for the equality test.
  492. </p>
  493. <pre>
  494. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  495. =&gt; [2, 3, 4]
  496. </pre>
  497. <p id="uniq">
  498. <b class="header">uniq</b><code>_.uniq(array, [isSorted])</code>
  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. </p>
  525. <pre>
  526. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  527. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  528. </pre>
  529. <p id="indexOf">
  530. <b class="header">indexOf</b><code>_.indexOf(array, value)</code>
  531. <br />
  532. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  533. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  534. <b>indexOf</b> function unless it's missing.
  535. </p>
  536. <pre>
  537. _.indexOf([1, 2, 3], 2);
  538. =&gt; 1
  539. </pre>
  540. <p id="lastIndexOf">
  541. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
  542. <br />
  543. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  544. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  545. function if possible.
  546. </p>
  547. <pre>
  548. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  549. =&gt; 4
  550. </pre>
  551. <p id="range">
  552. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  553. <br />
  554. A function to create flexibly-numbered lists of integers, handy for
  555. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  556. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  557. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  558. exclusive.
  559. </p>
  560. <pre>
  561. _.range(10);
  562. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  563. _.range(1, 11);
  564. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  565. _.range(0, 30, 5);
  566. =&gt; [0, 5, 10, 15, 20, 25]
  567. _.range(0, -10, -1);
  568. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  569. _.range(0);
  570. =&gt; []
  571. </pre>
  572. <h2>Function (uh, ahem) Functions</h2>
  573. <p id="bind">
  574. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  575. <br />
  576. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  577. the function is called, the value of <i>this</i> will be the <b>object</b>.
  578. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  579. also known as <b>currying</b>.
  580. </p>
  581. <pre>
  582. var func = function(greeting){ return greeting + ': ' + this.name };
  583. func = _.bind(func, {name : 'moe'}, 'hi');
  584. func();
  585. =&gt; 'hi: moe'
  586. </pre>
  587. <p id="bindAll">
  588. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  589. <br />
  590. Binds a number of methods on the <b>object</b>, specified by
  591. <b>methodNames</b>, to be run in the context of that object whenever they
  592. are invoked. Very handy for binding functions that are going to be used
  593. as event handlers, which would otherwise be invoked with a fairly useless
  594. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  595. function properties will be bound to it.
  596. </p>
  597. <pre>
  598. var buttonView = {
  599. label : 'underscore',
  600. onClick : function(){ alert('clicked: ' + this.label); },
  601. onHover : function(){ console.log('hovering: ' + this.label); }
  602. };
  603. _.bindAll(buttonView);
  604. jQuery('#underscore_button').bind('click', buttonView.onClick);
  605. =&gt; When the button is clicked, this.label will have the correct value...
  606. </pre>
  607. <p id="delay">
  608. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  609. <br />
  610. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  611. milliseconds. If you pass the optional <b>arguments</b>, they will be
  612. forwarded on to the <b>function</b> when it is invoked.
  613. </p>
  614. <pre>
  615. var log = _.bind(console.log, console);
  616. _.delay(log, 1000, 'logged later');
  617. =&gt; 'logged later' // Appears after one second.
  618. </pre>
  619. <p id="defer">
  620. <b class="header">defer</b><code>_.defer(function)</code>
  621. <br />
  622. Defers invoking the <b>function</b> until the current call stack has cleared,
  623. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  624. expensive computations or HTML rendering in chunks without blocking the UI thread
  625. from updating.
  626. </p>
  627. <pre>
  628. _.defer(function(){ alert('deferred'); });
  629. // Returns from the function before the alert runs.
  630. </pre>
  631. <p id="wrap">
  632. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  633. <br />
  634. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  635. passing it as the first argument. This allows the <b>wrapper</b> to
  636. execute code before and after the <b>function</b> runs, adjust the arguments,
  637. and execute it conditionally.
  638. </p>
  639. <pre>
  640. var hello = function(name) { return "hello: " + name; };
  641. hello = _.wrap(hello, function(func) {
  642. return "before, " + func("moe") + ", after";
  643. });
  644. hello();
  645. =&gt; 'before, hello: moe, after'
  646. </pre>
  647. <p id="compose">
  648. <b class="header">compose</b><code>_.compose(*functions)</code>
  649. <br />
  650. Returns the composition of a list of <b>functions</b>, where each function
  651. consumes the return value of the function that follows. In math terms,
  652. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  653. <i>f(g(h()))</i>.
  654. </p>
  655. <pre>
  656. var greet = function(name){ return "hi: " + name; };
  657. var exclaim = function(statement){ return statement + "!"; };
  658. var welcome = _.compose(greet, exclaim);
  659. welcome('moe');
  660. =&gt; 'hi: moe!'
  661. </pre>
  662. <h2>Object Functions</h2>
  663. <p id="keys">
  664. <b class="header">keys</b><code>_.keys(object)</code>
  665. <br />
  666. Retrieve all the names of the <b>object</b>'s properties.
  667. </p>
  668. <pre>
  669. _.keys({one : 1, two : 2, three : 3});
  670. =&gt; ["one", "two", "three"]
  671. </pre>
  672. <p id="values">
  673. <b class="header">values</b><code>_.values(object)</code>
  674. <br />
  675. Return all of the values of the <b>object</b>'s properties.
  676. </p>
  677. <pre>
  678. _.values({one : 1, two : 2, three : 3});
  679. =&gt; [1, 2, 3]
  680. </pre>
  681. <p id="functions">
  682. <b class="header">functions</b><code>_.functions(object)</code>
  683. <span class="alias">Alias: <b>methods</b></span>
  684. <br />
  685. Returns a sorted list of the names of every method in an object &mdash;
  686. that is to say, the name of every function property of the object.
  687. </p>
  688. <pre>
  689. _.functions(_);
  690. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  691. </pre>
  692. <p id="extend">
  693. <b class="header">extend</b><code>_.extend(destination, source)</code>
  694. <br />
  695. Copy all of the properties in the <b>source</b> object over to the
  696. <b>destination</b> object.
  697. </p>
  698. <pre>
  699. _.extend({name : 'moe'}, {age : 50});
  700. =&gt; {name : 'moe', age : 50}
  701. </pre>
  702. <p id="clone">
  703. <b class="header">clone</b><code>_.clone(object)</code>
  704. <br />
  705. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  706. or arrays will be copied by reference, not duplicated.
  707. </p>
  708. <pre>
  709. _.clone({name : 'moe'});
  710. =&gt; {name : 'moe'};
  711. </pre>
  712. <p id="tap">
  713. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  714. <br />
  715. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  716. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  717. </p>
  718. <pre>
  719. _([1,2,3,200]).chain().
  720. select(function(num) { return num % 2 == 0; }).
  721. tap(console.log).
  722. map(function(num) { return num * num }).
  723. value();
  724. =&gt; [2, 200]
  725. =&gt; [4, 40000]
  726. </pre>
  727. <p id="isEqual">
  728. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  729. <br />
  730. Performs an optimized deep comparison between the two objects, to determine
  731. if they should be considered equal.
  732. </p>
  733. <pre>
  734. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  735. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  736. moe == clone;
  737. =&gt; false
  738. _.isEqual(moe, clone);
  739. =&gt; true
  740. </pre>
  741. <p id="isEmpty">
  742. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  743. <br />
  744. Returns <i>true</i> if <b>object</b> contains no values.
  745. </p>
  746. <pre>
  747. _.isEmpty([1, 2, 3]);
  748. =&gt; false
  749. _.isEmpty({});
  750. =&gt; true
  751. </pre>
  752. <p id="isElement">
  753. <b class="header">isElement</b><code>_.isElement(object)</code>
  754. <br />
  755. Returns <i>true</i> if <b>object</b> is a DOM element.
  756. </p>
  757. <pre>
  758. _.isElement(jQuery('body')[0]);
  759. =&gt; true
  760. </pre>
  761. <p id="isArray">
  762. <b class="header">isArray</b><code>_.isArray(object)</code>
  763. <br />
  764. Returns <i>true</i> if <b>object</b> is an Array.
  765. </p>
  766. <pre>
  767. (function(){ return _.isArray(arguments); })();
  768. =&gt; false
  769. _.isArray([1,2,3]);
  770. =&gt; true
  771. </pre>
  772. <p id="isArguments">
  773. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  774. <br />
  775. Returns <i>true</i> if <b>object</b> is an Arguments object.
  776. </p>
  777. <pre>
  778. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  779. =&gt; true
  780. _.isArguments([1,2,3]);
  781. =&gt; false
  782. </pre>
  783. <p id="isFunction">
  784. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  785. <br />
  786. Returns <i>true</i> if <b>object</b> is a Function.
  787. </p>
  788. <pre>
  789. _.isFunction(alert);
  790. =&gt; true
  791. </pre>
  792. <p id="isString">
  793. <b class="header">isString</b><code>_.isString(object)</code>
  794. <br />
  795. Returns <i>true</i> if <b>object</b> is a String.
  796. </p>
  797. <pre>
  798. _.isFunction("moe");
  799. =&gt; true
  800. </pre>
  801. <p id="isNumber">
  802. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  803. <br />
  804. Returns <i>true</i> if <b>object</b> is a Number.
  805. </p>
  806. <pre>
  807. _.isNumber(8.4 * 5);
  808. =&gt; true
  809. </pre>
  810. <p id="isDate">
  811. <b class="header">isDate</b><code>_.isDate(object)</code>
  812. <br />
  813. Returns <i>true</i> if <b>object</b> is a Date.
  814. </p>
  815. <pre>
  816. _.isDate(new Date());
  817. =&gt; true
  818. </pre>
  819. <p id="isRegExp">
  820. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  821. <br />
  822. Returns <i>true</i> if <b>object</b> is a RegExp.
  823. </p>
  824. <pre>
  825. _.isRegExp(/moe/);
  826. =&gt; true
  827. </pre>
  828. <p id="isNaN">
  829. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  830. <br />
  831. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  832. the same as the native <b>isNaN</b> function, which will also return
  833. true if the variable is <i>undefined</i>.
  834. </p>
  835. <pre>
  836. _.isNaN(NaN);
  837. =&gt; true
  838. isNaN(undefined);
  839. =&gt; true
  840. _.isNaN(undefined);
  841. =&gt; false
  842. </pre>
  843. <p id="isNull">
  844. <b class="header">isNull</b><code>_.isNull(object)</code>
  845. <br />
  846. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  847. </p>
  848. <pre>
  849. _.isNull(null);
  850. =&gt; true
  851. _.isNull(undefined);
  852. =&gt; false
  853. </pre>
  854. <p id="isUndefined">
  855. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  856. <br />
  857. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  858. </p>
  859. <pre>
  860. _.isUndefined(window.missingVariable);
  861. =&gt; true
  862. </pre>
  863. <h2>Utility Functions</h2>
  864. <p id="noConflict">
  865. <b class="header">noConflict</b><code>_.noConflict()</code>
  866. <br />
  867. Give control of the "_" variable back to its previous owner. Returns
  868. a reference to the <b>Underscore</b> object.
  869. </p>
  870. <pre>
  871. var underscore = _.noConflict();</pre>
  872. <p id="identity">
  873. <b class="header">identity</b><code>_.identity(value)</code>
  874. <br />
  875. Returns the same value that is used as the argument. In math:
  876. <tt>f(x) = x</tt><br />
  877. This function looks useless, but is used throughout Underscore as
  878. a default iterator.
  879. </p>
  880. <pre>
  881. var moe = {name : 'moe'};
  882. moe === _.identity(moe);
  883. =&gt; true</pre>
  884. <p id="breakLoop">
  885. <b class="header">breakLoop</b><code>_.breakLoop()</code>
  886. <br />
  887. Breaks out of the current loop iteration. Similar to the <tt>break</tt>
  888. keyword in regular "for" loop, but works within an iterator function.
  889. Uses the native <tt>StopIteration</tt> object in JavaScript 1.7 compliant
  890. browsers.
  891. </p>
  892. <pre>
  893. var result = null;
  894. _.each([1, 2, 3], function(num) {
  895. if ((result = num) == 2) _.breakLoop();
  896. });
  897. result;
  898. =&gt; 2</pre>
  899. <p id="uniqueId">
  900. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  901. <br />
  902. Generate a globally-unique id for client-side models or DOM elements
  903. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  904. </p>
  905. <pre>
  906. _.uniqueId('contact_');
  907. =&gt; 'contact_104'</pre>
  908. <p id="template">
  909. <b class="header">template</b><code>_.template(templateString, [context])</code>
  910. <br />
  911. Compiles JavaScript templates into functions that can be evaluated
  912. for rendering. Useful for rendering complicated bits of HTML from JSON
  913. data sources. Template functions can both interpolate variables, using<br />
  914. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  915. <tt>&lt;% &hellip; %&gt;</tt>. When you evaluate a template function, pass in a
  916. <b>context</b> object that has properties corresponding to the template's free
  917. variables. If you're writing a one-off, you can pass the <b>context</b>
  918. object as the second parameter to <b>template</b> in order to render
  919. immediately instead of returning a template function.
  920. </p>
  921. <pre>
  922. var compiled = _.template("hello: &lt;%= name %&gt;");
  923. compiled({name : 'moe'});
  924. =&gt; "hello: moe"
  925. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  926. _.template(list, {people : ['moe', 'curly', 'larry']});
  927. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"</pre>
  928. <p>
  929. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  930. template settings to use different symbols to set off interpolated code.
  931. Define <b>start</b> and <b>end</b> tokens, and an <b>interpolate</b> regex
  932. to match expressions that should be evaluated and inserted. For example,
  933. to perform
  934. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  935. style templating:
  936. </p>
  937. <pre>
  938. _.templateSettings = {
  939. start : '{{',
  940. end : '}}',
  941. interpolate : /\{\{(.+?)\}\}/g
  942. };
  943. var template = _.template("Hello {{ name }}!");
  944. template({name : "Mustache"});
  945. =&gt; "Hello Mustache!"</pre>
  946. <h2>Chaining</h2>
  947. <p id="chain">
  948. <b class="header">chain</b><code>_(obj).chain()</code>
  949. <br />
  950. Returns a wrapped object. Calling methods on this object will continue
  951. to return wrapped objects until <tt>value</tt> is used. (
  952. <a href="#styles">A more realistic example.</a>)
  953. </p>
  954. <pre>
  955. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  956. var youngest = _(stooges).chain()
  957. .sortBy(function(stooge){ return stooge.age; })
  958. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  959. .first()
  960. .value();
  961. =&gt; "moe is 21"
  962. </pre>
  963. <p id="value">
  964. <b class="header">value</b><code>_(obj).value()</code>
  965. <br />
  966. Extracts the value of a wrapped object.
  967. </p>
  968. <pre>
  969. _([1, 2, 3]).value();
  970. =&gt; [1, 2, 3]
  971. </pre>
  972. <p id="item">
  973. <b class="header">item</b><code>_(obj).item(n)</code>
  974. <span class="alias">Alias: <b>get</b></span>
  975. <br />
  976. Get an item of a wrapped object. Equivalent to <b>_(obj).value()[n]</b> or <b>obj[n]</b>.
  977. </p>
  978. <pre>
  979. _([1, 2, 3]).item(1);
  980. =&gt; 2
  981. _({'one' : 1, 'two' : 2, 'three' : 3}).get('one')
  982. =&gt; 1
  983. </pre>
  984. <h2 id="duck_typing">Duck Typing</h2>
  985. <p>
  986. The <b>isType</b> (<tt>isArray</tt>, <tt>isFunction</tt>, <tt>isString</tt> ...) family of type-checking
  987. functions use property detection to do their work, which, although
  988. orders of magnitude faster than the alternative, isn't entirely safe when dealing
  989. with objects that are used as hashes, where arbitrary strings are being
  990. set for the keys. It's entirely possible for an object to masquerade as
  991. another type, if you're setting properties with names like "concat" and
  992. "charCodeAt". So be aware.
  993. </p>
  994. <h2>Links &amp; Suggested Reading</h2>
  995. <p>
  996. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  997. a Lua port of the functions that are applicable in both languages.
  998. Includes OOP-wrapping and chaining.
  999. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1000. available on GitHub.
  1001. </p>
  1002. <p>
  1003. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1004. </p>
  1005. <p>
  1006. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1007. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1008. </p>
  1009. <p>
  1010. Oliver Steele's
  1011. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1012. which includes comprehensive higher-order function support as well as string lambdas.
  1013. </p>
  1014. <p>
  1015. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1016. </p>
  1017. <h2>Change Log</h2>
  1018. <p>
  1019. <b class="header">0.5.8</b><br />
  1020. Fixed Underscore's collection functions to work on
  1021. <a href="https://developer.mozilla.org/En/DOM/NodeList">NodeLists</a> and
  1022. <a href="https://developer.mozilla.org/En/DOM/HTMLCollection">HTMLCollections</a>
  1023. once more, thanks to
  1024. <a href="http://github.com/jmtulloss">Justin Tulloss</a>.
  1025. </p>
  1026. <p>
  1027. <b class="header">0.5.7</b><br />
  1028. A safer implementation of <tt>_.isArguments</tt>, and a
  1029. faster <tt>_.isNumber</tt>,<br />thanks to
  1030. <a href="http://jedschmidt.com/">Jed Schmidt</a>.
  1031. </p>
  1032. <p>
  1033. <b class="header">0.5.6</b><br />
  1034. Customizable delimiters for <tt>_.template</tt>, contributed by
  1035. <a href="http://github.com/iamnoah">Noah Sloan</a>.
  1036. </p>
  1037. <p>
  1038. <b class="header">0.5.5</b><br />
  1039. Fix for a bug in MobileSafari's OOP-wrapper, with the arguments object.
  1040. </p>
  1041. <p>
  1042. <b class="header">0.5.4</b><br />
  1043. Fix for multiple single quotes within a template string for
  1044. <tt>_.template</tt>. See:
  1045. <a href="http://www.west-wind.com/Weblog/posts/509108.aspx">Rick Strahl's blog post</a>.
  1046. </p>
  1047. <p>
  1048. <b class="header">0.5.2</b><br />
  1049. New implementations of <tt>isArray</tt>, <tt>isDate</tt>, <tt>isFunction</tt>,
  1050. <tt>isNumber</tt>, <tt>isRegExp</tt>, and <tt>isString</tt>, thanks to
  1051. a suggestion from
  1052. <a href="http://www.broofa.com/">Robert Kieffer</a>.
  1053. Instead of doing <tt>Object#toString</tt>
  1054. comparisons, they now check for expected properties, which is less safe,
  1055. but more than an order of magnitude faster. Most other Underscore
  1056. functions saw minor speed improvements as a result.
  1057. <a href="http://dolzhenko.org/">Evgeniy Dolzhenko</a>
  1058. contributed <tt>_.tap</tt>,
  1059. <a href="http://ruby-doc.org/core-1.9/classes/Object.html#M000191">similar to Ruby 1.9's</a>,
  1060. which is handy for injecting side effects (like logging) into chained calls.
  1061. </p>
  1062. <p>
  1063. <b class="header">0.5.1</b><br />
  1064. Added an <tt>_.isArguments</tt> function. Lots of little safety checks
  1065. and optimizations contributed by
  1066. <a href="http://github.com/iamnoah/">Noah Sloan</a> and Andri Möll.
  1067. </p>
  1068. <p>
  1069. <b class="header">0.5.0</b><br />
  1070. <b>[API Changes]</b> <tt>_.bindAll</tt> now takes the context object as
  1071. its first parameter. If no method names are passed, all of the context
  1072. object's methods are bound to it, enabling chaining and easier binding.
  1073. <tt>_.functions</tt> now takes a single argument and returns the names
  1074. of its Function properties. Calling <tt>_.functions(_)</tt> will get you
  1075. the previous behavior.
  1076. Added <tt>_.isRegExp</tt> so that <tt>isEqual</tt> can now test for RegExp equality.
  1077. All of the "is" functions have been shrunk down into a single definition.
  1078. <a href="http://github.com/grayrest/">Karl Guertin</a> contributed patches.
  1079. </p>
  1080. <p>
  1081. <b class="header">0.4.7</b><br />
  1082. Added <tt>isDate</tt>, <tt>isNaN</tt>, and <tt>isNull</tt>, for completeness.
  1083. Optimizations for <tt>isEqual</tt> when checking equality between Arrays
  1084. or Dates. <tt>_.keys</tt> is now <small><i><b>25%&ndash;2X</b></i></small> faster (depending on your
  1085. browser) which speeds up the functions that rely on it, such as <tt>_.each</tt>.
  1086. </p>
  1087. <p>
  1088. <b class="header">0.4.6</b><br />
  1089. Added the <tt>range</tt> function, a port of the
  1090. <a href="http://docs.python.org/library/functions.html#range">Python
  1091. function of the same name</a>, for generating flexibly-numbered lists
  1092. of integers. Original patch contributed by
  1093. <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
  1094. </p>
  1095. <p>
  1096. <b class="header">0.4.5</b><br />
  1097. Added <tt>rest</tt> for Arrays and arguments objects, and aliased
  1098. <tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
  1099. thanks to <a href="http://github.com/lukesutton/">Luke Sutton</a>'s patches.
  1100. Added tests ensuring that all Underscore Array functions also work on
  1101. <i>arguments</i> objects.
  1102. </p>
  1103. <p>
  1104. <b class="header">0.4.4</b><br />
  1105. Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
  1106. <tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
  1107. </p>
  1108. <p>
  1109. <b class="header">0.4.3</b><br />
  1110. Started using the native <tt>StopIteration</tt> object in browsers that support it.
  1111. Fixed Underscore setup for CommonJS environments.
  1112. </p>
  1113. <p>
  1114. <b class="header">0.4.2</b><br />
  1115. Renamed the unwrapping function to <tt>value</tt>, for clarity.
  1116. </p>
  1117. <p>
  1118. <b class="header">0.4.1</b><br />
  1119. Chained Underscore objects now support the Array prototype methods, so
  1120. that you can perform the full range of operations on a wrapped array
  1121. without having to break your chain. Added a <tt>breakLoop</tt> method
  1122. to <b>break</b> in the middle of any Underscore iteration. Added an
  1123. <tt>isEmpty</tt> function that works on arrays and objects.
  1124. </p>
  1125. <p>
  1126. <b class="header">0.4.0</b><br />
  1127. All Underscore functions can now be called in an object-oriented style,
  1128. like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
  1129. <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
  1130. Wrapped objects can be chained through multiple
  1131. method invocations. A <a href="#functions"><tt>functions</tt></a> method
  1132. was added, providing a sorted list of all the functions in Underscore.
  1133. </p>
  1134. <p>
  1135. <b class="header">0.3.3</b><br />
  1136. Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
  1137. as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
  1138. </p>
  1139. <p>
  1140. <b class="header">0.3.2</b><br />
  1141. Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
  1142. interpreters with: <tt>load("underscore.js")</tt>.
  1143. Added <a href="#identity"><tt>identity</tt></a> as a utility function.
  1144. </p>
  1145. <p>
  1146. <b class="header">0.3.1</b><br />
  1147. All iterators are now passed in the original collection as their third
  1148. argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
  1149. objects is now called with <tt>(value, key, collection)</tt>, for details
  1150. see <a href="#each"><tt>_.each</tt></a>.
  1151. </p>
  1152. <p>
  1153. <b class="header">0.3.0</b><br />
  1154. Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
  1155. comprehensive optimizations, merged in
  1156. <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
  1157. <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
  1158. <a href="http://narwhaljs.org/">Narwhal</a> compliant.
  1159. </p>
  1160. <p>
  1161. <b class="header">0.2.0</b><br />
  1162. Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
  1163. <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
  1164. <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
  1165. </p>
  1166. <p>
  1167. <b class="header">0.1.1</b><br />
  1168. Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
  1169. other variables.
  1170. </p>
  1171. <p>
  1172. <b class="header">0.1.0</b><br />
  1173. Initial release of Underscore.js.
  1174. </p>
  1175. <p>
  1176. <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
  1177. <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
  1178. </a>
  1179. </p>
  1180. </div>
  1181. </div>
  1182. </body>
  1183. </html>