PageRenderTime 44ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/example/node/node_modules/underscore/index.html

http://github.com/ifandelse/postal.js
HTML | 2045 lines | 1833 code | 211 blank | 1 comment | 0 complexity | 7e4f2bd44e5d5e365cacb94b20593674 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  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: 14px;
  10. line-height: 22px;
  11. background: #f4f4f4 url(docs/images/background.png);
  12. color: #000;
  13. font-family: Helvetica Neue, Helvetica, Arial;
  14. }
  15. .interface {
  16. font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
  17. }
  18. div#sidebar {
  19. background: #fff;
  20. position: fixed;
  21. top: 0;
  22. left: 0;
  23. bottom: 0;
  24. width: 200px;
  25. overflow-y: auto;
  26. overflow-x: hidden;
  27. padding: 15px 0 30px 30px;
  28. border-right: 1px solid #bbb;
  29. box-shadow: 0 0 20px #ccc;
  30. -webkit-box-shadow: 0 0 20px #ccc;
  31. -moz-box-shadow: 0 0 20px #ccc;
  32. }
  33. a.toc_title, a.toc_title:visited {
  34. display: block;
  35. color: black;
  36. font-weight: bold;
  37. margin-top: 15px;
  38. }
  39. a.toc_title:hover {
  40. text-decoration: underline;
  41. }
  42. #sidebar .version {
  43. font-size: 10px;
  44. font-weight: normal;
  45. }
  46. ul.toc_section {
  47. font-size: 11px;
  48. line-height: 14px;
  49. margin: 5px 0 0 0;
  50. padding-left: 0px;
  51. list-style-type: none;
  52. font-family: Lucida Grande;
  53. }
  54. .toc_section li {
  55. cursor: pointer;
  56. margin: 0 0 3px 0;
  57. }
  58. .toc_section li a {
  59. text-decoration: none;
  60. color: black;
  61. }
  62. .toc_section li a:hover {
  63. text-decoration: underline;
  64. }
  65. div.container {
  66. width: 550px;
  67. margin: 40px 0 50px 260px;
  68. }
  69. div.warning {
  70. margin-top: 15px;
  71. font: bold 11px Arial;
  72. color: #770000;
  73. }
  74. p {
  75. margin: 20px 0;
  76. width: 550px;
  77. }
  78. a, a:visited {
  79. color: #444;
  80. }
  81. a:active, a:hover {
  82. color: #000;
  83. }
  84. h1, h2, h3, h4, h5, h6 {
  85. padding-top: 20px;
  86. }
  87. h2 {
  88. font-size: 20px;
  89. }
  90. b.header {
  91. font-size: 16px;
  92. line-height: 30px;
  93. }
  94. span.alias {
  95. font-size: 14px;
  96. font-style: italic;
  97. margin-left: 20px;
  98. }
  99. table, tr, td {
  100. margin: 0;
  101. padding: 0;
  102. }
  103. td {
  104. padding: 2px 12px 2px 0;
  105. }
  106. ul {
  107. list-style-type: circle;
  108. padding: 0 0 0 20px;
  109. }
  110. li {
  111. width: 500px;
  112. margin-bottom: 10px;
  113. }
  114. code, pre, tt {
  115. font-family: Monaco, Consolas, "Lucida Console", monospace;
  116. font-size: 12px;
  117. line-height: 18px;
  118. font-style: normal;
  119. }
  120. tt {
  121. padding: 0px 3px;
  122. background: #fff;
  123. border: 1px solid #ddd;
  124. zoom: 1;
  125. }
  126. code {
  127. margin-left: 20px;
  128. }
  129. pre {
  130. font-size: 12px;
  131. padding: 2px 0 2px 15px;
  132. border-left: 5px solid #bbb;
  133. margin: 0px 0 30px;
  134. }
  135. </style>
  136. </head>
  137. <body>
  138. <div id="sidebar" class="interface">
  139. <a class="toc_title" href="#">
  140. Underscore.js <span class="version">(1.3.1)</span>
  141. </a>
  142. <a class="toc_title" href="#">
  143. Introduction
  144. </a>
  145. <a class="toc_title" href="#collections">
  146. Collections
  147. </a>
  148. <ul class="toc_section">
  149. <li>- <a href="#each">each</a></li>
  150. <li>- <a href="#map">map</a></li>
  151. <li>- <a href="#reduce">reduce</a></li>
  152. <li>- <a href="#reduceRight">reduceRight</a></li>
  153. <li>- <a href="#find">find</a></li>
  154. <li>- <a href="#filter">filter</a></li>
  155. <li>- <a href="#reject">reject</a></li>
  156. <li>- <a href="#all">all</a></li>
  157. <li>- <a href="#any">any</a></li>
  158. <li>- <a href="#include">include</a></li>
  159. <li>- <a href="#invoke">invoke</a></li>
  160. <li>- <a href="#pluck">pluck</a></li>
  161. <li>- <a href="#max">max</a></li>
  162. <li>- <a href="#min">min</a></li>
  163. <li>- <a href="#sortBy">sortBy</a></li>
  164. <li>- <a href="#groupBy">groupBy</a></li>
  165. <li>- <a href="#sortedIndex">sortedIndex</a></li>
  166. <li>- <a href="#shuffle">shuffle</a></li>
  167. <li>- <a href="#toArray">toArray</a></li>
  168. <li>- <a href="#size">size</a></li>
  169. </ul>
  170. <a class="toc_title" href="#arrays">
  171. Arrays
  172. </a>
  173. <ul class="toc_section">
  174. <li>- <a href="#first">first</a></li>
  175. <li>- <a href="#initial">initial</a></li>
  176. <li>- <a href="#last">last</a></li>
  177. <li>- <a href="#rest">rest</a></li>
  178. <li>- <a href="#compact">compact</a></li>
  179. <li>- <a href="#flatten">flatten</a></li>
  180. <li>- <a href="#without">without</a></li>
  181. <li>- <a href="#union">union</a></li>
  182. <li>- <a href="#intersection">intersection</a></li>
  183. <li>- <a href="#difference">difference</a></li>
  184. <li>- <a href="#uniq">uniq</a></li>
  185. <li>- <a href="#zip">zip</a></li>
  186. <li>- <a href="#indexOf">indexOf</a></li>
  187. <li>- <a href="#lastIndexOf">lastIndexOf</a></li>
  188. <li>- <a href="#range">range</a></li>
  189. </ul>
  190. <a class="toc_title" href="#functions">
  191. Functions
  192. </a>
  193. <ul class="toc_section">
  194. <li>- <a href="#bind">bind</a></li>
  195. <li>- <a href="#bindAll">bindAll</a></li>
  196. <li>- <a href="#memoize">memoize</a></li>
  197. <li>- <a href="#delay">delay</a></li>
  198. <li>- <a href="#defer">defer</a></li>
  199. <li>- <a href="#throttle">throttle</a></li>
  200. <li>- <a href="#debounce">debounce</a></li>
  201. <li>- <a href="#once">once</a></li>
  202. <li>- <a href="#after">after</a></li>
  203. <li>- <a href="#wrap">wrap</a></li>
  204. <li>- <a href="#compose">compose</a></li>
  205. </ul>
  206. <a class="toc_title" href="#objects">
  207. Objects
  208. </a>
  209. <ul class="toc_section">
  210. <li>- <a href="#keys">keys</a></li>
  211. <li>- <a href="#values">values</a></li>
  212. <li>- <a href="#functions">functions</a></li>
  213. <li>- <a href="#extend">extend</a></li>
  214. <li>- <a href="#defaults">defaults</a></li>
  215. <li>- <a href="#clone">clone</a></li>
  216. <li>- <a href="#tap">tap</a></li>
  217. <li>- <a href="#has">has</a></li>
  218. <li>- <a href="#isEqual">isEqual</a></li>
  219. <li>- <a href="#isEmpty">isEmpty</a></li>
  220. <li>- <a href="#isElement">isElement</a></li>
  221. <li>- <a href="#isArray">isArray</a></li>
  222. <li>- <a href="#isArguments">isArguments</a></li>
  223. <li>- <a href="#isFunction">isFunction</a></li>
  224. <li>- <a href="#isString">isString</a></li>
  225. <li>- <a href="#isNumber">isNumber</a></li>
  226. <li>- <a href="#isBoolean">isBoolean</a></li>
  227. <li>- <a href="#isDate">isDate</a></li>
  228. <li>- <a href="#isRegExp">isRegExp</a></li>
  229. <li>- <a href="#isNaN">isNaN</a></li>
  230. <li>- <a href="#isNull">isNull</a></li>
  231. <li>- <a href="#isUndefined">isUndefined</a></li>
  232. </ul>
  233. <a class="toc_title" href="#utility">
  234. Utility
  235. </a>
  236. <ul class="toc_section">
  237. <li>- <a href="#noConflict">noConflict</a></li>
  238. <li>- <a href="#identity">identity</a></li>
  239. <li>- <a href="#times">times</a></li>
  240. <li>- <a href="#mixin">mixin</a></li>
  241. <li>- <a href="#uniqueId">uniqueId</a></li>
  242. <li>- <a href="#escape">escape</a></li>
  243. <li>- <a href="#template">template</a></li>
  244. </ul>
  245. <a class="toc_title" href="#chaining">
  246. Chaining
  247. </a>
  248. <ul class="toc_section">
  249. <li>- <a href="#chain">chain</a></li>
  250. <li>- <a href="#value">value</a></li>
  251. </ul>
  252. <a class="toc_title" href="#links">
  253. Links
  254. </a>
  255. <a class="toc_title" href="#changelog">
  256. Change Log
  257. </a>
  258. </div>
  259. <div class="container">
  260. <p id="introduction">
  261. <img style="width: 396px; height: 69px;" src="docs/images/underscore.png" alt="Underscore.js"/>
  262. </p>
  263. <p>
  264. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  265. utility-belt library for JavaScript that provides a lot of the
  266. functional programming support that you would expect in
  267. <a href="http://prototypejs.org/api">Prototype.js</a>
  268. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  269. but without extending any of the built-in JavaScript objects. It's the
  270. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
  271. and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
  272. </p>
  273. <p>
  274. Underscore provides 60-odd functions that support both the usual
  275. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  276. as well as more specialized helpers: function binding, javascript
  277. templating, deep equality testing, and so on. It delegates to built-in
  278. functions, if present, so modern browsers will use the
  279. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  280. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  281. </p>
  282. <p>
  283. A complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
  284. is included for your perusal.
  285. </p>
  286. <p>
  287. You may also read through the <a href="docs/underscore.html">annotated source code</a>.
  288. </p>
  289. <p>
  290. The project is
  291. <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
  292. You can report bugs and discuss features on the
  293. <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
  294. on Freenode in the <tt>#documentcloud</tt> channel,
  295. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  296. </p>
  297. <p>
  298. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  299. </p>
  300. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  301. <table>
  302. <tr>
  303. <td><a href="underscore.js">Development Version (1.3.1)</a></td>
  304. <td><i>34kb, Uncompressed with Comments</i></td>
  305. </tr>
  306. <tr>
  307. <td><a href="underscore-min.js">Production Version (1.3.1)</a></td>
  308. <td><i>&lt; 4kb, Minified and Gzipped</i></td>
  309. </tr>
  310. </table>
  311. <div class="warning">Upgrade warning: version 1.3.0 removes AMD (RequireJS) support.</div>
  312. <div id="documentation">
  313. <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
  314. <p id="each">
  315. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  316. <span class="alias">Alias: <b>forEach</b></span>
  317. <br/>
  318. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  319. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  320. passed. Each invocation of <b>iterator</b> is called with three arguments:
  321. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  322. arguments will be <tt>(value, key, list)</tt>. Delegates to the native
  323. <b>forEach</b> function if it exists.
  324. </p>
  325. <pre>
  326. _.each([1, 2, 3], function(num){ alert(num); });
  327. =&gt; alerts each number in turn...
  328. _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
  329. =&gt; alerts each number in turn...</pre>
  330. <p id="map">
  331. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  332. <span class="alias">Alias: <b>collect</b></span>
  333. <br/>
  334. Produces a new array of values by mapping each value in <b>list</b>
  335. through a transformation function (<b>iterator</b>). If the native <b>map</b> method
  336. exists, it will be used instead. If <b>list</b> is a JavaScript object,
  337. <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
  338. </p>
  339. <pre>
  340. _.map([1, 2, 3], function(num){ return num * 3; });
  341. =&gt; [3, 6, 9]
  342. _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
  343. =&gt; [3, 6, 9]</pre>
  344. <p id="reduce">
  345. <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
  346. <span class="alias">Aliases: <b>inject, foldl</b></span>
  347. <br/>
  348. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  349. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  350. of the reduction, and each successive step of it should be returned by
  351. <b>iterator</b>.
  352. </p>
  353. <pre>
  354. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  355. =&gt; 6
  356. </pre>
  357. <p id="reduceRight">
  358. <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
  359. <span class="alias">Alias: <b>foldr</b></span>
  360. <br/>
  361. The right-associative version of <b>reduce</b>. Delegates to the
  362. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  363. is not as useful in JavaScript as it would be in a language with lazy
  364. evaluation.
  365. </p>
  366. <pre>
  367. var list = [[0, 1], [2, 3], [4, 5]];
  368. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  369. =&gt; [4, 5, 2, 3, 0, 1]
  370. </pre>
  371. <p id="find">
  372. <b class="header">find</b><code>_.find(list, iterator, [context])</code>
  373. <span class="alias">Alias: <b>detect</b></span>
  374. <br/>
  375. Looks through each value in the <b>list</b>, returning the first one that
  376. passes a truth test (<b>iterator</b>). The function returns as
  377. soon as it finds an acceptable element, and doesn't traverse the
  378. entire list.
  379. </p>
  380. <pre>
  381. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  382. =&gt; 2
  383. </pre>
  384. <p id="filter">
  385. <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
  386. <span class="alias">Alias: <b>select</b></span>
  387. <br/>
  388. Looks through each value in the <b>list</b>, returning an array of all
  389. the values that pass a truth test (<b>iterator</b>). Delegates to the
  390. native <b>filter</b> method, if it exists.
  391. </p>
  392. <pre>
  393. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  394. =&gt; [2, 4, 6]
  395. </pre>
  396. <p id="reject">
  397. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  398. <br/>
  399. Returns the values in <b>list</b> without the elements that the truth
  400. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
  401. </p>
  402. <pre>
  403. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  404. =&gt; [1, 3, 5]
  405. </pre>
  406. <p id="all">
  407. <b class="header">all</b><code>_.all(list, iterator, [context])</code>
  408. <span class="alias">Alias: <b>every</b></span>
  409. <br/>
  410. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  411. truth test. Delegates to the native method <b>every</b>, if present.
  412. </p>
  413. <pre>
  414. _.all([true, 1, null, 'yes'], _.identity);
  415. =&gt; false
  416. </pre>
  417. <p id="any">
  418. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  419. <span class="alias">Alias: <b>some</b></span>
  420. <br/>
  421. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  422. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  423. if a true element is found. Delegates to the native method <b>some</b>,
  424. if present.
  425. </p>
  426. <pre>
  427. _.any([null, 0, 'yes', false]);
  428. =&gt; true
  429. </pre>
  430. <p id="include">
  431. <b class="header">include</b><code>_.include(list, value)</code>
  432. <span class="alias">Alias: <b>contains</b></span>
  433. <br/>
  434. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
  435. <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
  436. is an Array.
  437. </p>
  438. <pre>
  439. _.include([1, 2, 3], 3);
  440. =&gt; true
  441. </pre>
  442. <p id="invoke">
  443. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  444. <br/>
  445. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  446. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  447. method invocation.
  448. </p>
  449. <pre>
  450. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  451. =&gt; [[1, 5, 7], [1, 2, 3]]
  452. </pre>
  453. <p id="pluck">
  454. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  455. <br/>
  456. A convenient version of what is perhaps the most common use-case for
  457. <b>map</b>: extracting a list of property values.
  458. </p>
  459. <pre>
  460. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  461. _.pluck(stooges, 'name');
  462. =&gt; ["moe", "larry", "curly"]
  463. </pre>
  464. <p id="max">
  465. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  466. <br/>
  467. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  468. it will be used on each value to generate the criterion by which the
  469. value is ranked.
  470. </p>
  471. <pre>
  472. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  473. _.max(stooges, function(stooge){ return stooge.age; });
  474. =&gt; {name : 'curly', age : 60};
  475. </pre>
  476. <p id="min">
  477. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  478. <br/>
  479. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  480. it will be used on each value to generate the criterion by which the
  481. value is ranked.
  482. </p>
  483. <pre>
  484. var numbers = [10, 5, 100, 2, 1000];
  485. _.min(numbers);
  486. =&gt; 2
  487. </pre>
  488. <p id="sortBy">
  489. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  490. <br/>
  491. Returns a sorted copy of <b>list</b>, ranked in ascending order by the
  492. results of running each value through <b>iterator</b>.
  493. </p>
  494. <pre>
  495. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  496. =&gt; [5, 4, 6, 3, 1, 2]
  497. </pre>
  498. <p id="groupBy">
  499. <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
  500. <br/>
  501. Splits a collection into sets, grouped by the result of running each
  502. value through <b>iterator</b>. If <b>iterator</b> is a string instead of
  503. a function, groups by the property named by <b>iterator</b> on each of
  504. the values.
  505. </p>
  506. <pre>
  507. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  508. =&gt; {1: [1.3], 2: [2.1, 2.4]}
  509. _.groupBy(['one', 'two', 'three'], 'length');
  510. =&gt; {3: ["one", "two"], 5: ["three"]}
  511. </pre>
  512. <p id="sortedIndex">
  513. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  514. <br/>
  515. Uses a binary search to determine the index at which the <b>value</b>
  516. <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  517. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  518. the sort ranking of each value.
  519. </p>
  520. <pre>
  521. _.sortedIndex([10, 20, 30, 40, 50], 35);
  522. =&gt; 3
  523. </pre>
  524. <p id="shuffle">
  525. <b class="header">shuffle</b><code>_.shuffle(list)</code>
  526. <br/>
  527. Returns a shuffled copy of the <b>list</b>, using a version of the
  528. <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
  529. </p>
  530. <pre>
  531. _.shuffle([1, 2, 3, 4, 5, 6]);
  532. =&gt; [4, 1, 6, 3, 5, 2]
  533. </pre>
  534. <p id="toArray">
  535. <b class="header">toArray</b><code>_.toArray(list)</code>
  536. <br/>
  537. Converts the <b>list</b> (anything that can be iterated over), into a
  538. real Array. Useful for transmuting the <b>arguments</b> object.
  539. </p>
  540. <pre>
  541. (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
  542. =&gt; [1, 2, 3]
  543. </pre>
  544. <p id="size">
  545. <b class="header">size</b><code>_.size(list)</code>
  546. <br/>
  547. Return the number of values in the <b>list</b>.
  548. </p>
  549. <pre>
  550. _.size({one : 1, two : 2, three : 3});
  551. =&gt; 3
  552. </pre>
  553. <h2 id="arrays">Array Functions</h2>
  554. <p>
  555. <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
  556. </p>
  557. <p id="first">
  558. <b class="header">first</b><code>_.first(array, [n])</code>
  559. <span class="alias">Alias: <b>head</b></span>
  560. <br/>
  561. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  562. return the first <b>n</b> elements of the array.
  563. </p>
  564. <pre>
  565. _.first([5, 4, 3, 2, 1]);
  566. =&gt; 5
  567. </pre>
  568. <p id="initial">
  569. <b class="header">initial</b><code>_.initial(array, [n])</code>
  570. <br/>
  571. Returns everything but the last entry of the array. Especially useful on
  572. the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
  573. from the result.
  574. </p>
  575. <pre>
  576. _.initial([5, 4, 3, 2, 1]);
  577. =&gt; [5, 4, 3, 2]
  578. </pre>
  579. <p id="last">
  580. <b class="header">last</b><code>_.last(array, [n])</code>
  581. <br/>
  582. Returns the last element of an <b>array</b>. Passing <b>n</b> will return
  583. the last <b>n</b> elements of the array.
  584. </p>
  585. <pre>
  586. _.last([5, 4, 3, 2, 1]);
  587. =&gt; 1
  588. </pre>
  589. <p id="rest">
  590. <b class="header">rest</b><code>_.rest(array, [index])</code>
  591. <span class="alias">Alias: <b>tail</b></span>
  592. <br/>
  593. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  594. to return the values of the array from that index onward.
  595. </p>
  596. <pre>
  597. _.rest([5, 4, 3, 2, 1]);
  598. =&gt; [4, 3, 2, 1]
  599. </pre>
  600. <p id="compact">
  601. <b class="header">compact</b><code>_.compact(array)</code>
  602. <br/>
  603. Returns a copy of the <b>array</b> with all falsy values removed.
  604. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  605. <i>undefined</i> and <i>NaN</i> are all falsy.
  606. </p>
  607. <pre>
  608. _.compact([0, 1, false, 2, '', 3]);
  609. =&gt; [1, 2, 3]
  610. </pre>
  611. <p id="flatten">
  612. <b class="header">flatten</b><code>_.flatten(array, [shallow])</code>
  613. <br/>
  614. Flattens a nested <b>array</b> (the nesting can be to any depth). If you
  615. pass <b>shallow</b>, the array will only be flattened a single level.
  616. </p>
  617. <pre>
  618. _.flatten([1, [2], [3, [[4]]]]);
  619. =&gt; [1, 2, 3, 4];
  620. _.flatten([1, [2], [3, [[4]]]], true);
  621. =&gt; [1, 2, 3, [[4]]];
  622. </pre>
  623. <p id="without">
  624. <b class="header">without</b><code>_.without(array, [*values])</code>
  625. <br/>
  626. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  627. removed. <i>===</i> is used for the equality test.
  628. </p>
  629. <pre>
  630. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  631. =&gt; [2, 3, 4]
  632. </pre>
  633. <p id="union">
  634. <b class="header">union</b><code>_.union(*arrays)</code>
  635. <br/>
  636. Computes the union of the passed-in <b>arrays</b>: the list of unique items,
  637. in order, that are present in one or more of the <b>arrays</b>.
  638. </p>
  639. <pre>
  640. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  641. =&gt; [1, 2, 3, 101, 10]
  642. </pre>
  643. <p id="intersection">
  644. <b class="header">intersection</b><code>_.intersection(*arrays)</code>
  645. <br/>
  646. Computes the list of values that are the intersection of all the <b>arrays</b>.
  647. Each value in the result is present in each of the <b>arrays</b>.
  648. </p>
  649. <pre>
  650. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  651. =&gt; [1, 2]
  652. </pre>
  653. <p id="difference">
  654. <b class="header">difference</b><code>_.difference(array, *others)</code>
  655. <br/>
  656. Similar to <b>without</b>, but returns the values from <b>array</b> that
  657. are not present in the <b>other</b> arrays.
  658. </p>
  659. <pre>
  660. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  661. =&gt; [1, 3, 4]
  662. </pre>
  663. <p id="uniq">
  664. <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
  665. <span class="alias">Alias: <b>unique</b></span>
  666. <br/>
  667. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  668. object equality. If you know in advance that the <b>array</b> is sorted,
  669. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  670. If you want to compute unique items based on a transformation, pass an
  671. <b>iterator</b> function.
  672. </p>
  673. <pre>
  674. _.uniq([1, 2, 1, 3, 1, 4]);
  675. =&gt; [1, 2, 3, 4]
  676. </pre>
  677. <p id="zip">
  678. <b class="header">zip</b><code>_.zip(*arrays)</code>
  679. <br/>
  680. Merges together the values of each of the <b>arrays</b> with the
  681. values at the corresponding position. Useful when you have separate
  682. data sources that are coordinated through matching array indexes.
  683. If you're working with a matrix of nested arrays, <b>zip.apply</b>
  684. can transpose the matrix in a similar fashion.
  685. </p>
  686. <pre>
  687. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  688. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  689. </pre>
  690. <p id="indexOf">
  691. <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
  692. <br/>
  693. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  694. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  695. <b>indexOf</b> function unless it's missing. If you're working with a
  696. large array, and you know that the array is already sorted, pass <tt>true</tt>
  697. for <b>isSorted</b> to use a faster binary search.
  698. </p>
  699. <pre>
  700. _.indexOf([1, 2, 3], 2);
  701. =&gt; 1
  702. </pre>
  703. <p id="lastIndexOf">
  704. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
  705. <br/>
  706. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  707. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  708. function if possible.
  709. </p>
  710. <pre>
  711. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  712. =&gt; 4
  713. </pre>
  714. <p id="range">
  715. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  716. <br/>
  717. A function to create flexibly-numbered lists of integers, handy for
  718. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  719. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  720. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  721. exclusive.
  722. </p>
  723. <pre>
  724. _.range(10);
  725. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  726. _.range(1, 11);
  727. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  728. _.range(0, 30, 5);
  729. =&gt; [0, 5, 10, 15, 20, 25]
  730. _.range(0, -10, -1);
  731. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  732. _.range(0);
  733. =&gt; []
  734. </pre>
  735. <h2 id="functions">Function (uh, ahem) Functions</h2>
  736. <p id="bind">
  737. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  738. <br/>
  739. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  740. the function is called, the value of <i>this</i> will be the <b>object</b>.
  741. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  742. also known as <b>partial application</b>.
  743. </p>
  744. <pre>
  745. var func = function(greeting){ return greeting + ': ' + this.name };
  746. func = _.bind(func, {name : 'moe'}, 'hi');
  747. func();
  748. =&gt; 'hi: moe'
  749. </pre>
  750. <p id="bindAll">
  751. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  752. <br/>
  753. Binds a number of methods on the <b>object</b>, specified by
  754. <b>methodNames</b>, to be run in the context of that object whenever they
  755. are invoked. Very handy for binding functions that are going to be used
  756. as event handlers, which would otherwise be invoked with a fairly useless
  757. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  758. function properties will be bound to it.
  759. </p>
  760. <pre>
  761. var buttonView = {
  762. label : 'underscore',
  763. onClick : function(){ alert('clicked: ' + this.label); },
  764. onHover : function(){ console.log('hovering: ' + this.label); }
  765. };
  766. _.bindAll(buttonView);
  767. jQuery('#underscore_button').bind('click', buttonView.onClick);
  768. =&gt; When the button is clicked, this.label will have the correct value...
  769. </pre>
  770. <p id="memoize">
  771. <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
  772. <br/>
  773. Memoizes a given <b>function</b> by caching the computed result. Useful
  774. for speeding up slow-running computations. If passed an optional
  775. <b>hashFunction</b>, it will be used to compute the hash key for storing
  776. the result, based on the arguments to the original function. The default
  777. <b>hashFunction</b> just uses the first argument to the memoized function
  778. as the key.
  779. </p>
  780. <pre>
  781. var fibonacci = _.memoize(function(n) {
  782. return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  783. });
  784. </pre>
  785. <p id="delay">
  786. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  787. <br/>
  788. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  789. milliseconds. If you pass the optional <b>arguments</b>, they will be
  790. forwarded on to the <b>function</b> when it is invoked.
  791. </p>
  792. <pre>
  793. var log = _.bind(console.log, console);
  794. _.delay(log, 1000, 'logged later');
  795. =&gt; 'logged later' // Appears after one second.
  796. </pre>
  797. <p id="defer">
  798. <b class="header">defer</b><code>_.defer(function)</code>
  799. <br/>
  800. Defers invoking the <b>function</b> until the current call stack has cleared,
  801. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  802. expensive computations or HTML rendering in chunks without blocking the UI thread
  803. from updating.
  804. </p>
  805. <pre>
  806. _.defer(function(){ alert('deferred'); });
  807. // Returns from the function before the alert runs.
  808. </pre>
  809. <p id="throttle">
  810. <b class="header">throttle</b><code>_.throttle(function, wait)</code>
  811. <br/>
  812. Creates and returns a new, throttled version of the passed function,
  813. that, when invoked repeatedly, will only actually call the original function
  814. at most once per every <b>wait</b>
  815. milliseconds. Useful for rate-limiting events that occur faster than you
  816. can keep up with.
  817. </p>
  818. <pre>
  819. var throttled = _.throttle(updatePosition, 100);
  820. $(window).scroll(throttled);
  821. </pre>
  822. <p id="debounce">
  823. <b class="header">debounce</b><code>_.debounce(function, wait)</code>
  824. <br/>
  825. Creates and returns a new debounced version of the passed function that
  826. will postpone its execution until after
  827. <b>wait</b> milliseconds have elapsed since the last time it
  828. was invoked. Useful for implementing behavior that should only happen
  829. <i>after</i> the input has stopped arriving. For example: rendering a
  830. preview of a Markdown comment, recalculating a layout after the window
  831. has stopped being resized, and so on.
  832. </p>
  833. <pre>
  834. var lazyLayout = _.debounce(calculateLayout, 300);
  835. $(window).resize(lazyLayout);
  836. </pre>
  837. <p id="once">
  838. <b class="header">once</b><code>_.once(function)</code>
  839. <br/>
  840. Creates a version of the function that can only be called one time.
  841. Repeated calls to the modified function will have no effect, returning
  842. the value from the original call. Useful for initialization functions,
  843. instead of having to set a boolean flag and then check it later.
  844. </p>
  845. <pre>
  846. var initialize = _.once(createApplication);
  847. initialize();
  848. initialize();
  849. // Application is only created once.
  850. </pre>
  851. <p id="after">
  852. <b class="header">after</b><code>_.after(count, function)</code>
  853. <br/>
  854. Creates a version of the function that will only be run after first
  855. being called <b>count</b> times. Useful for grouping asynchronous responses,
  856. where you want to be sure that all the async calls have finished, before
  857. proceeding.
  858. </p>
  859. <pre>
  860. var renderNotes = _.after(notes.length, render);
  861. _.each(notes, function(note) {
  862. note.asyncSave({success: renderNotes});
  863. });
  864. // renderNotes is run once, after all notes have saved.
  865. </pre>
  866. <p id="wrap">
  867. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  868. <br/>
  869. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  870. passing it as the first argument. This allows the <b>wrapper</b> to
  871. execute code before and after the <b>function</b> runs, adjust the arguments,
  872. and execute it conditionally.
  873. </p>
  874. <pre>
  875. var hello = function(name) { return "hello: " + name; };
  876. hello = _.wrap(hello, function(func) {
  877. return "before, " + func("moe") + ", after";
  878. });
  879. hello();
  880. =&gt; 'before, hello: moe, after'
  881. </pre>
  882. <p id="compose">
  883. <b class="header">compose</b><code>_.compose(*functions)</code>
  884. <br/>
  885. Returns the composition of a list of <b>functions</b>, where each function
  886. consumes the return value of the function that follows. In math terms,
  887. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  888. <i>f(g(h()))</i>.
  889. </p>
  890. <pre>
  891. var greet = function(name){ return "hi: " + name; };
  892. var exclaim = function(statement){ return statement + "!"; };
  893. var welcome = _.compose(exclaim, greet);
  894. welcome('moe');
  895. =&gt; 'hi: moe!'
  896. </pre>
  897. <h2 id="objects">Object Functions</h2>
  898. <p id="keys">
  899. <b class="header">keys</b><code>_.keys(object)</code>
  900. <br/>
  901. Retrieve all the names of the <b>object</b>'s properties.
  902. </p>
  903. <pre>
  904. _.keys({one : 1, two : 2, three : 3});
  905. =&gt; ["one", "two", "three"]
  906. </pre>
  907. <p id="values">
  908. <b class="header">values</b><code>_.values(object)</code>
  909. <br/>
  910. Return all of the values of the <b>object</b>'s properties.
  911. </p>
  912. <pre>
  913. _.values({one : 1, two : 2, three : 3});
  914. =&gt; [1, 2, 3]
  915. </pre>
  916. <p id="functions">
  917. <b class="header">functions</b><code>_.functions(object)</code>
  918. <span class="alias">Alias: <b>methods</b></span>
  919. <br/>
  920. Returns a sorted list of the names of every method in an object &mdash;
  921. that is to say, the name of every function property of the object.
  922. </p>
  923. <pre>
  924. _.functions(_);
  925. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  926. </pre>
  927. <p id="extend">
  928. <b class="header">extend</b><code>_.extend(destination, *sources)</code>
  929. <br/>
  930. Copy all of the properties in the <b>source</b> objects over to the
  931. <b>destination</b> object. It's in-order, so the last source will override
  932. properties of the same name in previous arguments.
  933. </p>
  934. <pre>
  935. _.extend({name : 'moe'}, {age : 50});
  936. =&gt; {name : 'moe', age : 50}
  937. </pre>
  938. <p id="defaults">
  939. <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
  940. <br/>
  941. Fill in missing properties in <b>object</b> with default values from the
  942. <b>defaults</b> objects. As soon as the property is filled, further defaults
  943. will have no effect.
  944. </p>
  945. <pre>
  946. var iceCream = {flavor : "chocolate"};
  947. _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
  948. =&gt; {flavor : "chocolate", sprinkles : "lots"}
  949. </pre>
  950. <p id="clone">
  951. <b class="header">clone</b><code>_.clone(object)</code>
  952. <br/>
  953. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  954. or arrays will be copied by reference, not duplicated.
  955. </p>
  956. <pre>
  957. _.clone({name : 'moe'});
  958. =&gt; {name : 'moe'};
  959. </pre>
  960. <p id="tap">
  961. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  962. <br/>
  963. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  964. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate
  965. results within the chain.
  966. </p>
  967. <pre>
  968. _.chain([1,2,3,200])
  969. .filter(function(num) { return num % 2 == 0; })
  970. .tap(console.log)
  971. .map(function(num) { return num * num })
  972. .value();
  973. =&gt; [2, 200]
  974. =&gt; [4, 40000]
  975. </pre>
  976. <p id="has">
  977. <b class="header">has</b><code>_.has(object, key)</code>
  978. <br/>
  979. Does the object contain the given key? Identical to
  980. <tt>object.hasOwnProperty(key)</tt>, but uses a safe reference to the
  981. <tt>hasOwnProperty</tt> function, in case it's been
  982. <a href="http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/">overridden accidentally</a>.
  983. </p>
  984. <pre>
  985. _.has({a: 1, b: 2, c: 3}, "b");
  986. =&gt; true
  987. </pre>
  988. <p id="isEqual">
  989. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  990. <br/>
  991. Performs an optimized deep comparison between the two objects, to determine
  992. if they should be considered equal.
  993. </p>
  994. <pre>
  995. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  996. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  997. moe == clone;
  998. =&gt; false
  999. _.isEqual(moe, clone);
  1000. =&gt; true
  1001. </pre>
  1002. <p id="isEmpty">
  1003. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  1004. <br/>
  1005. Returns <i>true</i> if <b>object</b> contains no values.
  1006. </p>
  1007. <pre>
  1008. _.isEmpty([1, 2, 3]);
  1009. =&gt; false
  1010. _.isEmpty({});
  1011. =&gt; true
  1012. </pre>
  1013. <p id="isElement">
  1014. <b class="header">isElement</b><code>_.isElement(object)</code>
  1015. <br/>
  1016. Returns <i>true</i> if <b>object</b> is a DOM element.
  1017. </p>
  1018. <pre>
  1019. _.isElement(jQuery('body')[0]);
  1020. =&gt; true
  1021. </pre>
  1022. <p id="isArray">
  1023. <b class="header">isArray</b><code>_.isArray(object)</code>
  1024. <br/>
  1025. Returns <i>true</i> if <b>object</b> is an Array.
  1026. </p>
  1027. <pre>
  1028. (function(){ return _.isArray(arguments); })();
  1029. =&gt; false
  1030. _.isArray([1,2,3]);
  1031. =&gt; true
  1032. </pre>
  1033. <p id="isArguments">
  1034. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  1035. <br/>
  1036. Returns <i>true</i> if <b>object</b> is an Arguments object.
  1037. </p>
  1038. <pre>
  1039. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  1040. =&gt; true
  1041. _.isArguments([1,2,3]);
  1042. =&gt; false
  1043. </pre>
  1044. <p id="isFunction">
  1045. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  1046. <br/>
  1047. Returns <i>true</i> if <b>object</b> is a Function.
  1048. </p>
  1049. <pre>
  1050. _.isFunction(alert);
  1051. =&gt; true
  1052. </pre>
  1053. <p id="isString">
  1054. <b class="header">isString</b><code>_.isString(object)</code>
  1055. <br/>
  1056. Returns <i>true</i> if <b>object</b> is a String.
  1057. </p>
  1058. <pre>
  1059. _.isString("moe");
  1060. =&gt; true
  1061. </pre>
  1062. <p id="isNumber">
  1063. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  1064. <br/>
  1065. Returns <i>true</i> if <b>object</b> is a Number (including <tt>NaN</tt>).
  1066. </p>
  1067. <pre>
  1068. _.isNumber(8.4 * 5);
  1069. =&gt; true
  1070. </pre>
  1071. <p id="isBoolean">
  1072. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  1073. <br/>
  1074. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  1075. </p>
  1076. <pre>
  1077. _.isBoolean(null);
  1078. =&gt; false
  1079. </pre>
  1080. <p id="isDate">
  1081. <b class="header">isDate</b><code>_.isDate(object)</code>
  1082. <br/>
  1083. Returns <i>true</i> if <b>object</b> is a Date.
  1084. </p>
  1085. <pre>
  1086. _.isDate(new Date());
  1087. =&gt; true
  1088. </pre>
  1089. <p id="isRegExp">
  1090. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  1091. <br/>
  1092. Returns <i>true</i> if <b>object</b> is a RegExp.
  1093. </p>
  1094. <pre>
  1095. _.isRegExp(/moe/);
  1096. =&gt; true
  1097. </pre>
  1098. <p id="isNaN">
  1099. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  1100. <br/>
  1101. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br/> Note: this is not
  1102. the same as the native <b>isNaN</b> function, which will also return
  1103. true if the variable is <i>undefined</i>.
  1104. </p>
  1105. <pre>
  1106. _.isNaN(NaN);
  1107. =&gt; true
  1108. isNaN(undefined);
  1109. =&gt; true
  1110. _.isNaN(undefined);
  1111. =&gt; false
  1112. </pre>
  1113. <p id="isNull">
  1114. <b class="header">isNull</b><code>_.isNull(object)</code>
  1115. <br/>
  1116. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  1117. </p>
  1118. <pre>
  1119. _.isNull(null);
  1120. =&gt; true
  1121. _.isNull(undefined);
  1122. =&gt; false
  1123. </pre>
  1124. <p id="isUndefined">
  1125. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  1126. <br/>
  1127. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  1128. </p>
  1129. <pre>
  1130. _.isUndefined(window.missingVariable);
  1131. =&gt; true
  1132. </pre>
  1133. <h2 id="utility">Utility Functions</h2>
  1134. <p id="noConflict">
  1135. <b class="header">noConflict</b><code>_.noConflict()</code>
  1136. <br/>
  1137. Give control of the "_" variable back to its previous owner. Returns
  1138. a reference to the <b>Underscore</b> object.
  1139. </p>
  1140. <pre>
  1141. var underscore = _.noConflict();</pre>
  1142. <p id="identity">
  1143. <b class="header">identity</b><code>_.identity(value)</code>
  1144. <br/>
  1145. Returns the same value that is used as the argument. In math:
  1146. <tt>f(x) = x</tt><br/>
  1147. This function looks useless, but is used throughout Underscore as
  1148. a default iterator.
  1149. </p>
  1150. <pre>
  1151. var moe = {name : 'moe'};
  1152. moe === _.identity(moe);
  1153. =&gt; true</pre>
  1154. <p id="times">
  1155. <b class="header">times</b><code>_.times(n, iterator)</code>
  1156. <br/>
  1157. Invokes the given iterator function <b>n</b> times.
  1158. </p>
  1159. <pre>
  1160. _(3).times(function(){ genie.grantWish(); });</pre>
  1161. <p id="mixin">
  1162. <b class="header">mixin</b><code>_.mixin(object)</code>
  1163. <br/>
  1164. Allows you to extend Underscore with your own utility functions. Pass
  1165. a hash of <tt>{name: function}</tt> definitions to have your functions
  1166. added to the Underscore object, as well as the OOP wrapper.
  1167. </p>
  1168. <pre>
  1169. _.mixin({
  1170. capitalize : function(string) {
  1171. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  1172. }
  1173. });
  1174. _("fabio").capitalize();
  1175. =&gt; "Fabio"
  1176. </pre>
  1177. <p id="uniqueId">
  1178. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  1179. <br/>
  1180. Generate a globally-unique id for client-side models or DOM elements
  1181. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  1182. </p>
  1183. <pre>
  1184. _.uniqueId('contact_');
  1185. =&gt; 'contact_104'</pre>
  1186. <p id="escape">
  1187. <b class="header">escape</b><code>_.escape(string)</code>
  1188. <br/>
  1189. Escapes a string for insertion into HTML, replacing
  1190. <tt>&amp;</tt>, <tt>&lt;</tt>, <tt>&gt;</tt>, <tt>&quot;</tt>, <tt>&#x27;</tt>, and <tt>&#x2F;</tt> characters.
  1191. </p>
  1192. <pre>
  1193. _.escape('Curly, Larry &amp; Moe');
  1194. =&gt; "Curly, Larry &amp;amp; Moe"</pre>
  1195. <p id="template">
  1196. <b class="header">template</b><code>_.template(templateString, [context])</code>
  1197. <br/>
  1198. Compiles JavaScript templates into functions that can be evaluated
  1199. for rendering. Useful for rendering complicated bits of HTML from JSON
  1200. data sources. Template functions can both interpolate variables, using<br/>
  1201. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  1202. <tt>&lt;% &hellip; %&gt;</tt>. If you wish to interpolate a value, and have
  1203. it be HTML-escaped, use <tt>&lt;%- &hellip; %&gt;</tt> When you evaluate a template function, pass in a
  1204. <b>context</b> object that has properties corresponding to the template's free
  1205. variables. If you're writing a one-off, you can pass the <b>context</b>
  1206. object as the second parameter to <b>template</b> in order to render
  1207. immediately instead of returning a template function.
  1208. </p>
  1209. <pre>
  1210. var compiled = _.template("hello: &lt;%= name %&gt;");
  1211. compiled({name : 'moe'});
  1212. =&gt; "hello: moe"
  1213. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  1214. _.template(list, {people : ['moe', 'curly', 'larry']});
  1215. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
  1216. var template = _.template("&lt;b&gt;&lt;%- value %&gt;&lt;/b&gt;");
  1217. template({value : '&lt;script&gt;'});
  1218. =&gt; "&lt;b&gt;&amp;lt;script&amp;gt;&lt;/b&gt;"</pre>
  1219. <p>
  1220. You can also use <tt>print</tt> from within JavaScript code. This is
  1221. sometimes more convenient than using <tt>&lt;%= ... %&gt;</tt>.
  1222. </p>
  1223. <pre>
  1224. var compiled = _.template("&lt;% print('Hello ' + epithet); %&gt;");
  1225. compiled({epithet: "stooge"});
  1226. =&gt; "Hello stooge."</pre>
  1227. <p>
  1228. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  1229. template settings to use different symbols to set off interpolated code.
  1230. Define an <b>interpolate</b> regex to match expressions that should be
  1231. interpolated verbatim, an <b>escape</b> regex to match expressions that should
  1232. be inserted after being HTML escaped, and an <b>evaluate</b> regex to match
  1233. expressions that should be evaluated without insertion into the resulting
  1234. string. You may define or omit any combination of the three.
  1235. For example, to perform
  1236. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  1237. style templating:
  1238. </p>
  1239. <pre>
  1240. _.templateSettings = {
  1241. interpolate : /\{\{(.+?)\}\}/g
  1242. };
  1243. var template = _.template("Hello {{ name }}!");
  1244. template({name : "Mustache"});
  1245. =&gt; "Hello Mustache!"</pre>
  1246. <h2 id="chaining">Chaining</h2>
  1247. <p>
  1248. You can use Underscore in either an object-oriented or a functional style,
  1249. depending on your preference. The following two lines of code are
  1250. identical ways to double a list of numbers.
  1251. </p>
  1252. <pre>
  1253. _.map([1, 2, 3], function(n){ return n * 2; });
  1254. _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
  1255. <p>
  1256. Using the object-oriented style allows you to chain together methods. Calling
  1257. <tt>chain</tt> on a wrapped object will cause all future method calls to
  1258. return wrapped objects as well. When you've finished the computation,
  1259. use <tt>value</tt> to retrieve the final value. Here's an example of chaining
  1260. together a <b>map/flatten/reduce</b>, in order to get the word count of
  1261. every word in a song.
  1262. </p>
  1263. <pre>
  1264. var lyrics = [
  1265. {line : 1, words : "I'm a lumberjack and I'm okay"},
  1266. {line : 2, words : "I sleep all night and I work all day"},
  1267. {line : 3, words : "He's a lumberjack and he's okay"},
  1268. {line : 4, words : "He sleeps all night and he works all day"}
  1269. ];
  1270. _.chain(lyrics)
  1271. .map(function(line) { return line.words.split(' '); })
  1272. .flatten()
  1273. .reduce(function(counts, word) {
  1274. counts[word] = (counts[word] || 0) + 1;
  1275. return counts;
  1276. }, {}).value();
  1277. =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
  1278. <p>
  1279. In addition, the
  1280. <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/prototype">Array prototype's
  1281. methods</a>
  1282. are proxied through the chained Underscore object, so you can slip a
  1283. <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
  1284. modify the array.
  1285. </p>
  1286. <p id="chain">
  1287. <b class="header">chain</b><code>_.chain(obj)</code>
  1288. <br/>
  1289. Returns a wrapped object. Calling methods on this object will continue
  1290. to return wrapped objects until <tt>value</tt> is used.
  1291. </p>
  1292. <pre>
  1293. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  1294. var youngest = _.chain(stooges)
  1295. .sortBy(function(stooge){ return stooge.age; })
  1296. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  1297. .first()
  1298. .value();
  1299. =&gt; "moe is 21"
  1300. </pre>
  1301. <p id="value">
  1302. <b class="header">value</b><code>_(obj).value()</code>
  1303. <br/>
  1304. Extracts the value of a wrapped object.
  1305. </p>
  1306. <pre>
  1307. _([1, 2, 3]).value();
  1308. =&gt; [1, 2, 3]
  1309. </pre>
  1310. <h2 id="links">Links &amp; Suggested Reading</h2>
  1311. <p>
  1312. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  1313. a Lua port of the functions that are applicable in both languages.
  1314. Includes OOP-wrapping and chaining.
  1315. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1316. available on GitHub.
  1317. </p>
  1318. <p>
  1319. <a href="http://brianhaveri.github.com/Underscore.php/">Underscore.php</a>,
  1320. a PHP port of the functions that are applicable in both languages.
  1321. Includes OOP-wrapping and chaining.
  1322. The <a href="http://github.com/brianhaveri/Underscore.php">source</a> is
  1323. available on GitHub.
  1324. </p>
  1325. <p>
  1326. <a href="http://vti.github.com/underscore-perl/">Underscore-perl</a>,
  1327. a Perl port of many of the Underscore.js functions,
  1328. aimed at on Perl hashes and arrays, also
  1329. <a href="https://github.com/vti/underscore-perl/">available on GitHub</a>.
  1330. </p>
  1331. <p>
  1332. <a href="https://github.com/edtsech/underscore.string">Underscore.string</a>,
  1333. an Underscore extension that adds functions for string-manipulation:
  1334. <tt>trim</tt>, <tt>startsWith</tt>, <tt>contains</tt>, <tt>capitalize</tt>,
  1335. <tt>reverse</tt>, <tt>sprintf</tt>, and more.
  1336. </p>
  1337. <p>
  1338. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1339. </p>
  1340. <p>
  1341. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1342. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1343. </p>
  1344. <p>
  1345. Oliver Steele's
  1346. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1347. which includes comprehensive higher-order function support as well as string lambdas.
  1348. </p>
  1349. <p>
  1350. Michael Aufreiter's <a href="http://substance.io/#michael/data-js">Data.js</a>,
  1351. a data manipulation + persistence library for JavaScript.
  1352. </p>
  1353. <p>
  1354. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1355. </p>
  1356. <h2 id="changelog">Change Log</h2>
  1357. <p>
  1358. <b class="header">1.3.1</b> &mdash;
  1359. <small><i>Jan. 23, 2012</i></small>
  1360. <br/>
  1361. <ul>
  1362. <li>
  1363. Added an <tt>_.has</tt> function, as a safer way to use <tt>hasOwnProperty</tt>.
  1364. </li>
  1365. <li>
  1366. Added <tt>_.collect</tt> as an alias for <tt>_.map</tt>. Smalltalkers, rejoice.
  1367. </li>
  1368. <li>
  1369. Reverted an old change so that <tt>_.extend</tt> will correctly copy
  1370. over keys with undefined values again.
  1371. </li>
  1372. <li>
  1373. Bugfix to stop escaping slashes within interpolations in <tt>_.template</tt>.
  1374. </li>
  1375. </ul>
  1376. </p>
  1377. <p>
  1378. <b class="header">1.3.0</b> &mdash;
  1379. <small><i>Jan. 11, 2012</i></small>
  1380. <br/>
  1381. <ul>
  1382. <li>
  1383. Removed AMD (RequireJS) support from Underscore. If you'd like to use
  1384. Underscore with RequireJS, you can load it as a normal script, wrap
  1385. or patch your copy, or download a forked version.
  1386. </li>
  1387. </ul>
  1388. </p>
  1389. <p>
  1390. <b class="header">1.2.4</b> &mdash;
  1391. <small><i>Jan. 4, 2012</i></small>
  1392. <br/>
  1393. <ul>
  1394. <li>
  1395. You now can (and probably should) write <tt>_.chain(list)</tt>
  1396. instead of <tt>_(list).chain()</tt>.
  1397. </li>
  1398. <li>
  1399. Fix for escaped characters in Underscore templates, and for supporting
  1400. customizations of <tt>_.templateSettings</tt> that only define one or
  1401. two of the required regexes.
  1402. </li>
  1403. <li>
  1404. Fix for passing an array as the first argument to an <tt>_.wrap</tt>'d function.
  1405. </li>
  1406. <li>
  1407. Improved compatibility with ClojureScript, which adds a <tt>call</tt>
  1408. function to <tt>String.prototype</tt>.
  1409. </li>
  1410. </ul>
  1411. </p>
  1412. <p>
  1413. <b class="header">1.2.3</b> &mdash;
  1414. <small><i>Dec. 7, 2011</i></small>
  1415. <br/>
  1416. <ul>
  1417. <li>
  1418. Dynamic scope is now preserved for compiled <tt>_.template</tt> functions,
  1419. so you can use the value of <tt>this</tt> if you like.
  1420. </li>
  1421. <li>
  1422. Sparse array support of <tt>_.indexOf</tt>, <tt>_.lastIndexOf</tt>.
  1423. </li>
  1424. <li>
  1425. Both <tt>_.reduce</tt> and <tt>_.reduceRight</tt> can now be passed an
  1426. explicitly <tt>undefined</tt> value. (There's no reason why you'd
  1427. want to do this.)
  1428. </li>
  1429. </ul>
  1430. </p>
  1431. <p>
  1432. <b class="header">1.2.2</b> &mdash;
  1433. <small><i>Nov. 14, 2011</i></small>
  1434. <br/>
  1435. <ul>
  1436. <li>
  1437. Continued tweaks to <tt>_.isEqual</tt> semantics. Now JS primitives are
  1438. considered equivalent to their wrapped versions, and arrays are compared
  1439. by their numeric properties only
  1440. <small>(#351)</small>
  1441. .
  1442. </li>
  1443. <li>
  1444. <tt>_.escape</tt> no longer tries to be smart about not double-escaping
  1445. already-escaped HTML entities. Now it just escapes regardless
  1446. <small>(#350)</small>
  1447. .
  1448. </li>
  1449. <li>
  1450. In <tt>_.template</tt>, you may now leave semicolons out of evaluated
  1451. statements if you wish: <tt>&lt;% }) %&gt;</tt>
  1452. <small>(#369)</small>
  1453. .
  1454. </li>
  1455. <li>
  1456. <tt>_.after(callback, 0)</tt> will now trigger the callback immediately,
  1457. making "after" easier to use with asynchronous APIs
  1458. <small>(#366)</small>
  1459. .
  1460. </li>
  1461. </ul>
  1462. </p>
  1463. <p>
  1464. <b class="header">1.2.1</b> &mdash;
  1465. <small><i>Oct. 24, 2011</i></small>
  1466. <br/>
  1467. <ul>
  1468. <li>
  1469. Several important bug fixes for <tt>_.isEqual</tt>, which should now
  1470. do better on mutated Arrays, and on non-Array objects with
  1471. <tt>length</tt> properties.
  1472. <small>(#329)</small>
  1473. </li>
  1474. <li>
  1475. <b>jrburke</b> contributed Underscore exporting for AMD module loaders,
  1476. and <b>tonylukasavage</b> for Appcelerator Titanium.
  1477. <small>(#335, #338)</small>
  1478. </li>
  1479. <li>
  1480. You can now <tt>_.groupBy(list, 'property')</tt> as a shortcut for
  1481. grouping values by a particular common property.
  1482. </li>
  1483. <li>
  1484. <tt>_.throttle</tt>'d functions now fire immediately upon invocation,
  1485. and are rate-limited thereafter
  1486. <small>(#170, #266)</small>
  1487. .
  1488. </li>
  1489. <li>
  1490. Most of the <tt>_.is[Type]</tt> checks no longer ducktype.
  1491. </li>
  1492. <li>
  1493. The <tt>_.bind</tt> function now also works on constructors, a-la
  1494. ES5 ... but you would never want to use <tt>_.bind</tt> on a
  1495. constructor function.
  1496. </li>
  1497. <li>
  1498. <tt>_.clone</tt> no longer wraps non-object types in Objects.
  1499. </li>
  1500. <li>
  1501. <tt>_.find</tt> and <tt>_.filter</tt> are now the preferred names for
  1502. <tt>_.detect</tt> and <tt>_.select</tt>.
  1503. </li>
  1504. </ul>
  1505. </p>
  1506. <p>
  1507. <b class="header">1.2.0</b> &mdash;
  1508. <small><i>Oct. 5, 2011</i></small>
  1509. <br/>
  1510. <ul>
  1511. <li>
  1512. The <tt>_.isEqual</tt> function now
  1513. supports true deep equality comparisons, with checks for cyclic structures,
  1514. thanks to Kit Cambridge.
  1515. </li>
  1516. <li>
  1517. Underscore templates now support HTML escaping interpolations, using
  1518. <tt>&lt;%- ... %&gt;</tt> syntax.
  1519. </li>
  1520. <li>
  1521. Ryan Tenney contributed <tt>_.shuffle</tt>, which uses a modified
  1522. Fisher-Yates to give you a shuffled copy of an array.
  1523. </li>
  1524. <li>
  1525. <tt>_.uniq</tt> can now be passed an optional iterator, to determine by
  1526. what criteria an object should be considered unique.
  1527. </li>
  1528. <li>
  1529. <tt>_.last</tt> now takes an optional argument which will return the last
  1530. N elements of the list.
  1531. </li>
  1532. <li>
  1533. A new <tt>_.initial</tt> function was added, as a mirror of <tt>_.rest</tt>,
  1534. which returns all the initial values of a list (except the last N).
  1535. </li>
  1536. </ul>
  1537. </p>
  1538. <p>
  1539. <b class="header">1.1.7</b> &mdash;
  1540. <small><i>July 13, 2011</i></small>
  1541. <br/>
  1542. Added <tt>_.groupBy</tt>, which aggregates a collection into groups of like items.
  1543. Added <tt>_.union</tt> and <tt>_.difference</tt>, to complement the
  1544. (re-named) <tt>_.intersection</tt>.
  1545. Various improvements for support of sparse arrays.
  1546. <tt>_.toArray</tt> now returns a clone, if directly passed an array.
  1547. <tt>_.functions</tt> now also returns the names of functions that are present
  1548. in the prototype chain.
  1549. </p>
  1550. <p>
  1551. <b class="header">1.1.6</b> &mdash;
  1552. <small><i>April 18, 2011</i></small>
  1553. <br/>
  1554. Added <tt>_.after</tt>, which will return a function that only runs after
  1555. first being called a specified number of times.
  1556. <tt>_.invoke</tt> can now take a direct function reference.
  1557. <tt>_.every</tt> now requires an iterator function to be passed, which
  1558. mirrors the ECMA5 API.
  1559. <tt>_.extend</tt> no longer copies keys when the value is undefined.
  1560. <tt>_.bind</tt> now errors when trying to bind an undefined value.
  1561. </p>
  1562. <p>
  1563. <b class="header">1.1.5</b> &mdash;
  1564. <small><i>Mar 20, 2011</i></small>
  1565. <br/>
  1566. Added an <tt>_.defaults</tt> function, for use merging together JS objects
  1567. representing default options.
  1568. Added an <tt>_.once</tt> function, for manufacturing functions that should
  1569. only ever execute a single time.
  1570. <tt>_.bind</tt> now delegates to the native ECMAScript 5 version,
  1571. where available.
  1572. <tt>_.keys</tt> now throws an error when used on non-Object values, as in
  1573. ECMAScript 5.
  1574. Fixed a bug with <tt>_.keys</tt> when used over sparse arrays.
  1575. </p>
  1576. <p>
  1577. <b class="header">1.1.4</b> &mdash;
  1578. <small><i>Jan 9, 2011</i></small>
  1579. <br/>
  1580. Improved compliance with ES5's Array methods when passing <tt>null</tt>
  1581. as a value. <tt>_.wrap</tt> now correctly sets <tt>this</tt> for the
  1582. wrapped function. <tt>_.indexOf</tt> now takes an optional flag for
  1583. finding the insertion index in an array that is guaranteed to already
  1584. be sorted. Avoiding the use of <tt>.callee</tt>, to allow <tt>_.isArray</tt>
  1585. to work properly in ES5's strict mode.
  1586. </p>
  1587. <p>
  1588. <b class="header">1.1.3</b> &mdash;
  1589. <small><i>Dec 1, 2010</i></small>
  1590. <br/>
  1591. In CommonJS, Underscore may now be required with just: <br/>
  1592. <tt>var _ = require("underscore")</tt>.
  1593. Added <tt>_.throttle</tt> and <tt>_.debounce</tt> functions.
  1594. Removed <tt>_.breakLoop</tt>, in favor of an ECMA5-style un-<i>break</i>-able
  1595. each implementation &mdash; this removes the try/catch, and you'll now have
  1596. better stack traces for exceptions that are thrown within an Underscore iterator.
  1597. Improved the <b>isType</b> family of functions for better interoperability
  1598. with Internet Explorer host objects.
  1599. <tt>_.template</tt> now correctly escapes backslashes in templates.
  1600. Improved <tt>_.reduce</tt> compatibility with the ECMA5 version:
  1601. if you don't pass an initial value, the first item in the collection is used.
  1602. <tt>_.each</tt> no longer returns the iterated collection, for improved
  1603. consistency with ES5's <tt>forEach</tt>.
  1604. </p>
  1605. <p>
  1606. <b class="header">1.1.2</b><br/>
  1607. Fixed <tt>_.contains</tt>, which was mistakenly pointing at
  1608. <tt>_.intersect</tt> instead of <tt>_.include</tt>, like it should
  1609. have been. Added <tt>_.unique</tt> as an alias for <tt>_.uniq</tt>.
  1610. </p>
  1611. <p>
  1612. <b class="header">1.1.1</b><br/>
  1613. Improved the speed of <tt>_.template</tt>, and its handling of multiline
  1614. interpolations. Ryan Tenney contributed optimizations to many Underscore
  1615. functions. An annotated version of the source code is now available.
  1616. </p>
  1617. <p>
  1618. <b class="header">1.1.0</b><br/>
  1619. The method signature of <tt>_.reduce</tt> has been changed to match
  1620. the ECMAScript 5 signature, instead of the Ruby/Prototype.js version.
  1621. This is a backwards-incompatible change. <tt>_.template</tt> may now be
  1622. called with no arguments, and preserves whitespace. <tt>_.contains</tt>
  1623. is a new alias for <tt>_.include</tt>.
  1624. </p>
  1625. <p>
  1626. <b class="header">1.0.4</b><br/>
  1627. <a href="http://themoell.com/">Andri Möll</a> contributed the <tt>_.memoize</tt>
  1628. function, which can be used to speed up expensive repeated computations
  1629. by caching the results.
  1630. </p>
  1631. <p>
  1632. <b class="header">1.0.3</b><br/>
  1633. Patch that makes <tt>_.isEqual</tt> return <tt>false</tt> if any property
  1634. of the compared object has a <tt>NaN</tt> value. Technically the correct
  1635. thing to do, but of questionable semantics. Watch out for NaN comparisons.
  1636. </p>
  1637. <p>
  1638. <b class="header">1.0.2</b><br/>
  1639. Fixes <tt>_.isArguments</tt> in recent versions of Opera, which have
  1640. arguments objects as real Arrays.
  1641. </p>
  1642. <p>
  1643. <b class="header">1.0.1</b><br/>
  1644. Bugfix for <tt>_.isEqual</tt>, when comparing two objects with the same
  1645. number of undefined keys, but with different names.
  1646. </p>
  1647. <p>
  1648. <b class="header">1.0.0</b><br/>
  1649. Things have been stable for many months now, so Underscore is now
  1650. considered to be out of beta, at <b>1.0</b>. Improvements since <b>0.6</b>
  1651. include <tt>_.isBoolean</tt>, and the ability to have <tt>_.extend</tt>
  1652. take multiple source objects.
  1653. </p>
  1654. <p>
  1655. <b class="header">0.6.0</b><br/>
  1656. Major release. Incorporates a number of
  1657. <a href="http://github.com/ratbeard">Mile Frawley's</a> refactors for
  1658. safer duck-typing on collection functions, and cleaner internals. A new
  1659. <tt>_.mixin</tt> method that allows you to extend Underscore with utility
  1660. functions of your own. Added <tt>_.times</tt>, which works the same as in
  1661. Ruby or Prototype.js. Native support for ECMAScript 5's <tt>Array.isArray</tt>,
  1662. and <tt>Object.keys</tt>.
  1663. </p>
  1664. <p>
  1665. <b class="header">0.5.8</b><br/>
  1666. Fixed Underscore's collection functions to work on
  1667. <a href="https://developer.mozilla.org/En/DOM/NodeList">NodeLists</a> and
  1668. <a href="https://developer.mozilla.org/En/DOM/HTMLCollection">HTMLCollections</a>
  1669. once more, thanks to
  1670. <a href="http://github.com/jmtulloss">Justin Tulloss</a>.
  1671. </p>
  1672. <p>
  1673. <b class="header">0.5.7</b><br/>
  1674. A safer implementation of <tt>_.isArguments</tt>, and a
  1675. faster <tt>_.isNumber</tt>,<br/>thanks to
  1676. <a href="http://jedschmidt.com/">Jed Schmidt</a>.
  1677. </p>
  1678. <p>
  1679. <b class="header">0.5.6</b><br/>
  1680. Customizable delimiters for <tt>_.template</tt>, contributed by
  1681. <a href="http://github.com/iamnoah">Noah Sloan</a>.
  1682. </p>
  1683. <p>
  1684. <b class="header">0.5.5</b><br/>
  1685. Fix for a bug in MobileSafari's OOP-wrapper, with the arguments object.
  1686. </p>
  1687. <p>
  1688. <b class="header">0.5.4</b><br/>
  1689. Fix for multiple single quotes within a template string for
  1690. <tt>_.template</tt>. See:
  1691. <a href="http://www.west-wind.com/Weblog/posts/509108.aspx">Rick Strahl's blog post</a>.
  1692. </p>
  1693. <p>
  1694. <b class="header">0.5.2</b><br/>
  1695. New implementations of <tt>isArray</tt>, <tt>isDate</tt>, <tt>isFunction</tt>,
  1696. <tt>isNumber</tt>, <tt>isRegExp</tt>, and <tt>isString</tt>, thanks to
  1697. a suggestion from
  1698. <a href="http://www.broofa.com/">Robert Kieffer</a>.
  1699. Instead of doing <tt>Object#toString</tt>
  1700. comparisons, they now check for expected properties, which is less safe,
  1701. but more than an order of magnitude faster. Most other Underscore
  1702. functions saw minor speed improvements as a result.
  1703. <a href="http://dolzhenko.org/">Evgeniy Dolzhenko</a>
  1704. contributed <tt>_.tap</tt>,
  1705. <a href="http://ruby-doc.org/core-1.9/classes/Object.html#M000191">similar to Ruby 1.9's</a>,
  1706. which is handy for injecting side effects (like logging) into chained calls.
  1707. </p>
  1708. <p>
  1709. <b class="header">0.5.1</b><br/>
  1710. Added an <tt>_.isArguments</tt> function. Lots of little safety checks
  1711. and optimizations contributed by
  1712. <a href="http://github.com/iamnoah/">Noah Sloan</a> and
  1713. <a href="http://themoell.com/">Andri Möll</a>.
  1714. </p>
  1715. <p>
  1716. <b class="header">0.5.0</b><br/>
  1717. <b>[API Changes]</b> <tt>_.bindAll</tt> now takes the context object as
  1718. its first parameter. If no method names are passed, all of the context
  1719. object's methods are bound to it, enabling chaining and easier binding.
  1720. <tt>_.functions</tt> now takes a single argument and returns the names
  1721. of its Function properties. Calling <tt>_.functions(_)</tt> will get you
  1722. the previous behavior.
  1723. Added <tt>_.isRegExp</tt> so that <tt>isEqual</tt> can now test for RegExp equality.
  1724. All of the "is" functions have been shrunk down into a single definition.
  1725. <a href="http://github.com/grayrest/">Karl Guertin</a> contributed patches.
  1726. </p>
  1727. <p>
  1728. <b class="header">0.4.7</b><br/>
  1729. Added <tt>isDate</tt>, <tt>isNaN</tt>, and <tt>isNull</tt>, for completeness.
  1730. Optimizations for <tt>isEqual</tt> when checking equality between Arrays
  1731. or Dates. <tt>_.keys</tt> is now
  1732. <small><i><b>25%&ndash;2X</b></i></small>
  1733. faster (depending on your
  1734. browser) which speeds up the functions that rely on it, such as <tt>_.each</tt>.
  1735. </p>
  1736. <p>
  1737. <b class="header">0.4.6</b><br/>
  1738. Added the <tt>range</tt> function, a port of the
  1739. <a href="http://docs.python.org/library/functions.html#range">Python
  1740. function of the same name</a>, for generating flexibly-numbered lists
  1741. of integers. Original patch contributed by
  1742. <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
  1743. </p>
  1744. <p>
  1745. <b class="header">0.4.5</b><br/>
  1746. Added <tt>rest</tt> for Arrays and arguments objects, and aliased
  1747. <tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
  1748. thanks to <a href="http://github.com/lukesutton/">Luke Sutton</a>'s patches.
  1749. Added tests ensuring that all Underscore Array functions also work on
  1750. <i>arguments</i> objects.
  1751. </p>
  1752. <p>
  1753. <b class="header">0.4.4</b><br/>
  1754. Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
  1755. <tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
  1756. </p>
  1757. <p>
  1758. <b class="header">0.4.3</b><br/>
  1759. Started using the native <tt>StopIteration</tt> object in browsers that support it.
  1760. Fixed Underscore setup for CommonJS environments.
  1761. </p>
  1762. <p>
  1763. <b class="header">0.4.2</b><br/>
  1764. Renamed the unwrapping function to <tt>value</tt>, for clarity.
  1765. </p>
  1766. <p>
  1767. <b class="header">0.4.1</b><br/>
  1768. Chained Underscore objects now support the Array prototype methods, so
  1769. that you can perform the full range of operations on a wrapped array
  1770. without having to break your chain. Added a <tt>breakLoop</tt> method
  1771. to <b>break</b> in the middle of any Underscore iteration. Added an
  1772. <tt>isEmpty</tt> function that works on arrays and objects.
  1773. </p>
  1774. <p>
  1775. <b class="header">0.4.0</b><br/>
  1776. All Underscore functions can now be called in an object-oriented style,
  1777. like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
  1778. <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
  1779. Wrapped objects can be chained through multiple
  1780. method invocations. A <a href="#functions"><tt>functions</tt></a> method
  1781. was added, providing a sorted list of all the functions in Underscore.
  1782. </p>
  1783. <p>
  1784. <b class="header">0.3.3</b><br/>
  1785. Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
  1786. as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
  1787. </p>
  1788. <p>
  1789. <b class="header">0.3.2</b><br/>
  1790. Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
  1791. interpreters with: <tt>load("underscore.js")</tt>.
  1792. Added <a href="#identity"><tt>identity</tt></a> as a utility function.
  1793. </p>
  1794. <p>
  1795. <b class="header">0.3.1</b><br/>
  1796. All iterators are now passed in the original collection as their third
  1797. argument, the same as JavaScript 1.6's <b>forEach</b>. Iterating over
  1798. objects is now called with <tt>(value, key, collection)</tt>, for details
  1799. see <a href="#each"><tt>_.each</tt></a>.
  1800. </p>
  1801. <p>
  1802. <b class="header">0.3.0</b><br/>
  1803. Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
  1804. comprehensive optimizations, merged in
  1805. <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
  1806. <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
  1807. <a href="http://narwhaljs.org/">Narwhal</a> compliant.
  1808. </p>
  1809. <p>
  1810. <b class="header">0.2.0</b><br/>
  1811. Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
  1812. <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
  1813. <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
  1814. </p>
  1815. <p>
  1816. <b class="header">0.1.1</b><br/>
  1817. Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
  1818. other variables.
  1819. </p>
  1820. <p>
  1821. <b class="header">0.1.0</b><br/>
  1822. Initial release of Underscore.js.
  1823. </p>
  1824. <p>
  1825. <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
  1826. <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project"/>
  1827. </a>
  1828. </p>
  1829. </div>
  1830. </div>
  1831. <!-- Include Underscore, so you can play with it in the console. -->
  1832. <script type="text/javascript" src="underscore.js"></script>
  1833. </body>
  1834. </html>