PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/index.html

https://github.com/gplocke/underscore
HTML | 1424 lines | 1272 code | 151 blank | 1 comment | 0 complexity | 2fa18939f17d175c4c3c2f9ea34af383 MD5 | raw file

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

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