You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2467 lines
88 KiB

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