PageRenderTime 105ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/underscore/index.html

https://bitbucket.org/mpuckett/iostudio-whiteboard
HTML | 2343 lines | 2132 code | 210 blank | 1 comment | 0 complexity | 20c718ca60f1422be20c831f1b2def9c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT

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

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="chrome=1" />
  6. <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.0)</span>
  181. </a>
  182. <a class="toc_title" href="#">
  183. Introduction
  184. </a>
  185. <a class="toc_title" href="#collections">
  186. Collections
  187. </a>
  188. <ul class="toc_section">
  189. <li>- <a href="#each">each</a></li>
  190. <li>- <a href="#map">map</a></li>
  191. <li>- <a href="#reduce">reduce</a></li>
  192. <li>- <a href="#reduceRight">reduceRight</a></li>
  193. <li>- <a href="#find">find</a></li>
  194. <li>- <a href="#filter">filter</a></li>
  195. <li>- <a href="#where">where</a></li>
  196. <li>- <a href="#reject">reject</a></li>
  197. <li>- <a href="#all">all</a></li>
  198. <li>- <a href="#any">any</a></li>
  199. <li>- <a href="#contains">contains</a></li>
  200. <li>- <a href="#invoke">invoke</a></li>
  201. <li>- <a href="#pluck">pluck</a></li>
  202. <li>- <a href="#max">max</a></li>
  203. <li>- <a href="#min">min</a></li>
  204. <li>- <a href="#sortBy">sortBy</a></li>
  205. <li>- <a href="#groupBy">groupBy</a></li>
  206. <li>- <a href="#countBy">countBy</a></li>
  207. <li>- <a href="#shuffle">shuffle</a></li>
  208. <li>- <a href="#toArray">toArray</a></li>
  209. <li>- <a href="#size">size</a></li>
  210. </ul>
  211. <a class="toc_title" href="#arrays">
  212. Arrays
  213. </a>
  214. <ul class="toc_section">
  215. <li>- <a href="#first">first</a></li>
  216. <li>- <a href="#initial">initial</a></li>
  217. <li>- <a href="#last">last</a></li>
  218. <li>- <a href="#rest">rest</a></li>
  219. <li>- <a href="#compact">compact</a></li>
  220. <li>- <a href="#flatten">flatten</a></li>
  221. <li>- <a href="#without">without</a></li>
  222. <li>- <a href="#union">union</a></li>
  223. <li>- <a href="#intersection">intersection</a></li>
  224. <li>- <a href="#difference">difference</a></li>
  225. <li>- <a href="#uniq">uniq</a></li>
  226. <li>- <a href="#zip">zip</a></li>
  227. <li>- <a href="#object">object</a></li>
  228. <li>- <a href="#indexOf">indexOf</a></li>
  229. <li>- <a href="#lastIndexOf">lastIndexOf</a></li>
  230. <li>- <a href="#sortedIndex">sortedIndex</a></li>
  231. <li>- <a href="#range">range</a></li>
  232. </ul>
  233. <a class="toc_title" href="#functions">
  234. Functions
  235. </a>
  236. <ul class="toc_section">
  237. <li>- <a href="#bind">bind</a></li>
  238. <li>- <a href="#bindAll">bindAll</a></li>
  239. <li>- <a href="#memoize">memoize</a></li>
  240. <li>- <a href="#delay">delay</a></li>
  241. <li>- <a href="#defer">defer</a></li>
  242. <li>- <a href="#throttle">throttle</a></li>
  243. <li>- <a href="#debounce">debounce</a></li>
  244. <li>- <a href="#once">once</a></li>
  245. <li>- <a href="#after">after</a></li>
  246. <li>- <a href="#wrap">wrap</a></li>
  247. <li>- <a href="#compose">compose</a></li>
  248. </ul>
  249. <a class="toc_title" href="#objects">
  250. Objects
  251. </a>
  252. <ul class="toc_section">
  253. <li>- <a href="#keys">keys</a></li>
  254. <li>- <a href="#values">values</a></li>
  255. <li>- <a href="#pairs">pairs</a></li>
  256. <li>- <a href="#invert">invert</a></li>
  257. <li>- <a href="#object-functions">functions</a></li>
  258. <li>- <a href="#extend">extend</a></li>
  259. <li>- <a href="#pick">pick</a></li>
  260. <li>- <a href="#omit">omit</a></li>
  261. <li>- <a href="#defaults">defaults</a></li>
  262. <li>- <a href="#clone">clone</a></li>
  263. <li>- <a href="#tap">tap</a></li>
  264. <li>- <a href="#has">has</a></li>
  265. <li>- <a href="#isEqual">isEqual</a></li>
  266. <li>- <a href="#isEmpty">isEmpty</a></li>
  267. <li>- <a href="#isElement">isElement</a></li>
  268. <li>- <a href="#isArray">isArray</a></li>
  269. <li>- <a href="#isObject">isObject</a></li>
  270. <li>- <a href="#isArguments">isArguments</a></li>
  271. <li>- <a href="#isFunction">isFunction</a></li>
  272. <li>- <a href="#isString">isString</a></li>
  273. <li>- <a href="#isNumber">isNumber</a></li>
  274. <li>- <a href="#isFinite">isFinite</a></li>
  275. <li>- <a href="#isBoolean">isBoolean</a></li>
  276. <li>- <a href="#isDate">isDate</a></li>
  277. <li>- <a href="#isRegExp">isRegExp</a></li>
  278. <li>- <a href="#isNaN">isNaN</a></li>
  279. <li>- <a href="#isNull">isNull</a></li>
  280. <li>- <a href="#isUndefined">isUndefined</a></li>
  281. </ul>
  282. <a class="toc_title" href="#utility">
  283. Utility
  284. </a>
  285. <ul class="toc_section">
  286. <li>- <a href="#noConflict">noConflict</a></li>
  287. <li>- <a href="#identity">identity</a></li>
  288. <li>- <a href="#times">times</a></li>
  289. <li>- <a href="#random">random</a></li>
  290. <li>- <a href="#mixin">mixin</a></li>
  291. <li>- <a href="#uniqueId">uniqueId</a></li>
  292. <li>- <a href="#escape">escape</a></li>
  293. <li>- <a href="#result">result</a></li>
  294. <li>- <a href="#template">template</a></li>
  295. </ul>
  296. <a class="toc_title" href="#chaining">
  297. Chaining
  298. </a>
  299. <ul class="toc_section">
  300. <li>- <a href="#chain">chain</a></li>
  301. <li>- <a href="#value">value</a></li>
  302. </ul>
  303. <a class="toc_title" href="#links">
  304. Links
  305. </a>
  306. <a class="toc_title" href="#changelog">
  307. Change Log
  308. </a>
  309. </div>
  310. <div class="container">
  311. <p id="introduction">
  312. <img id="logo" src="docs/images/underscore.png" alt="Underscore.js" />
  313. </p>
  314. <p>
  315. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  316. utility-belt library for JavaScript that provides a lot of the
  317. functional programming support that you would expect in
  318. <a href="http://prototypejs.org/api">Prototype.js</a>
  319. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  320. but without extending any of the built-in JavaScript objects. It's the
  321. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
  322. and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
  323. </p>
  324. <p>
  325. Underscore provides 80-odd functions that support both the usual
  326. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  327. as well as more specialized helpers: function binding, javascript
  328. templating, deep equality testing, and so on. It delegates to built-in
  329. functions, if present, so modern browsers will use the
  330. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  331. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  332. </p>
  333. <p>
  334. A complete <a href="test/">Test &amp; Benchmark Suite</a>
  335. is included for your perusal.
  336. </p>
  337. <p>
  338. You may also read through the <a href="docs/underscore.html">annotated source code</a>.
  339. </p>
  340. <p>
  341. The project is
  342. <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
  343. You can report bugs and discuss features on the
  344. <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
  345. on Freenode in the <tt>#documentcloud</tt> channel,
  346. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  347. </p>
  348. <p>
  349. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  350. </p>
  351. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  352. <table>
  353. <tr>
  354. <td><a href="underscore.js">Development Version (1.4.0)</a></td>
  355. <td><i>40kb, Uncompressed with Plentiful Comments</i></td>
  356. </tr>
  357. <tr>
  358. <td><a href="underscore-min.js">Production Version (1.4.0)</a></td>
  359. <td><i>4kb, Minified and Gzipped</i></td>
  360. </tr>
  361. <tr>
  362. <td colspan="2"><div class="rule"></div></td>
  363. </tr>
  364. <tr>
  365. <td><a href="https://raw.github.com/documentcloud/underscore/master/underscore.js">Edge Version</a></td>
  366. <td><i>Unreleased, current <tt>master</tt>, use at your own risk</i></td>
  367. </tr>
  368. </table>
  369. <div id="documentation">
  370. <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
  371. <p id="each">
  372. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  373. <span class="alias">Alias: <b>forEach</b></span>
  374. <br />
  375. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  376. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  377. passed. Each invocation of <b>iterator</b> is called with three arguments:
  378. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  379. arguments will be <tt>(value, key, list)</tt>. Delegates to the native
  380. <b>forEach</b> function if it exists.
  381. </p>
  382. <pre>
  383. _.each([1, 2, 3], function(num){ alert(num); });
  384. =&gt; alerts each number in turn...
  385. _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
  386. =&gt; alerts each number in turn...</pre>
  387. <p id="map">
  388. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  389. <span class="alias">Alias: <b>collect</b></span>
  390. <br />
  391. Produces a new array of values by mapping each value in <b>list</b>
  392. through a transformation function (<b>iterator</b>). If the native <b>map</b> method
  393. exists, it will be used instead. If <b>list</b> is a JavaScript object,
  394. <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
  395. </p>
  396. <pre>
  397. _.map([1, 2, 3], function(num){ return num * 3; });
  398. =&gt; [3, 6, 9]
  399. _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
  400. =&gt; [3, 6, 9]</pre>
  401. <p id="reduce">
  402. <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
  403. <span class="alias">Aliases: <b>inject, foldl</b></span>
  404. <br />
  405. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  406. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  407. of the reduction, and each successive step of it should be returned by
  408. <b>iterator</b>. The iterator is passed four arguments: the <tt>memo</tt>,
  409. then the <tt>value</tt> and <tt>index</tt> (or key) of the iteration,
  410. and finally a reference to the entire <tt>list</tt>.
  411. </p>
  412. <pre>
  413. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  414. =&gt; 6
  415. </pre>
  416. <p id="reduceRight">
  417. <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
  418. <span class="alias">Alias: <b>foldr</b></span>
  419. <br />
  420. The right-associative version of <b>reduce</b>. Delegates to the
  421. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  422. is not as useful in JavaScript as it would be in a language with lazy
  423. evaluation.
  424. </p>
  425. <pre>
  426. var list = [[0, 1], [2, 3], [4, 5]];
  427. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  428. =&gt; [4, 5, 2, 3, 0, 1]
  429. </pre>
  430. <p id="find">
  431. <b class="header">find</b><code>_.find(list, iterator, [context])</code>
  432. <span class="alias">Alias: <b>detect</b></span>
  433. <br />
  434. Looks through each value in the <b>list</b>, returning the first one that
  435. passes a truth test (<b>iterator</b>). The function returns as
  436. soon as it finds an acceptable element, and doesn't traverse the
  437. entire list.
  438. </p>
  439. <pre>
  440. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  441. =&gt; 2
  442. </pre>
  443. <p id="filter">
  444. <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
  445. <span class="alias">Alias: <b>select</b></span>
  446. <br />
  447. Looks through each value in the <b>list</b>, returning an array of all
  448. the values that pass a truth test (<b>iterator</b>). Delegates to the
  449. native <b>filter</b> method, if it exists.
  450. </p>
  451. <pre>
  452. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  453. =&gt; [2, 4, 6]
  454. </pre>
  455. <p id="where">
  456. <b class="header">where</b><code>_.where(list, properties)</code>
  457. <br />
  458. Looks through each value in the <b>list</b>, returning an array of all
  459. the values that contain all of the key-value pairs listed in <b>properties</b>.
  460. </p>
  461. <pre>
  462. _.where(listOfPlays, {author: "Shakespeare", year: 1611});
  463. =&gt; [{title: "Cymbeline", author: "Shakespeare", year: 1611},
  464. {title: "The Tempest", author: "Shakespeare", year: 1611}]
  465. </pre>
  466. <p id="reject">
  467. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  468. <br />
  469. Returns the values in <b>list</b> without the elements that the truth
  470. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
  471. </p>
  472. <pre>
  473. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  474. =&gt; [1, 3, 5]
  475. </pre>
  476. <p id="all">
  477. <b class="header">all</b><code>_.all(list, iterator, [context])</code>
  478. <span class="alias">Alias: <b>every</b></span>
  479. <br />
  480. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  481. truth test. Delegates to the native method <b>every</b>, if present.
  482. </p>
  483. <pre>
  484. _.all([true, 1, null, 'yes'], _.identity);
  485. =&gt; false
  486. </pre>
  487. <p id="any">
  488. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  489. <span class="alias">Alias: <b>some</b></span>
  490. <br />
  491. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  492. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  493. if a true element is found. Delegates to the native method <b>some</b>,
  494. if present.
  495. </p>
  496. <pre>
  497. _.any([null, 0, 'yes', false]);
  498. =&gt; true
  499. </pre>
  500. <p id="contains">
  501. <b class="header">contains</b><code>_.contains(list, value)</code>
  502. <span class="alias">Alias: <b>include</b></span>
  503. <br />
  504. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>.
  505. Uses <b>indexOf</b> internally, if <b>list</b> is an Array.
  506. </p>
  507. <pre>
  508. _.contains([1, 2, 3], 3);
  509. =&gt; true
  510. </pre>
  511. <p id="invoke">
  512. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  513. <br />
  514. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  515. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  516. method invocation.
  517. </p>
  518. <pre>
  519. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  520. =&gt; [[1, 5, 7], [1, 2, 3]]
  521. </pre>
  522. <p id="pluck">
  523. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  524. <br />
  525. A convenient version of what is perhaps the most common use-case for
  526. <b>map</b>: extracting a list of property values.
  527. </p>
  528. <pre>
  529. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  530. _.pluck(stooges, 'name');
  531. =&gt; ["moe", "larry", "curly"]
  532. </pre>
  533. <p id="max">
  534. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  535. <br />
  536. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  537. it will be used on each value to generate the criterion by which the
  538. value is ranked.
  539. </p>
  540. <pre>
  541. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  542. _.max(stooges, function(stooge){ return stooge.age; });
  543. =&gt; {name : 'curly', age : 60};
  544. </pre>
  545. <p id="min">
  546. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  547. <br />
  548. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  549. it will be used on each value to generate the criterion by which the
  550. value is ranked.
  551. </p>
  552. <pre>
  553. var numbers = [10, 5, 100, 2, 1000];
  554. _.min(numbers);
  555. =&gt; 2
  556. </pre>
  557. <p id="sortBy">
  558. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  559. <br />
  560. Returns a sorted copy of <b>list</b>, ranked in ascending order by the
  561. results of running each value through <b>iterator</b>. Iterator may
  562. also be the string name of the property to sort by (eg. <tt>length</tt>).
  563. </p>
  564. <pre>
  565. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  566. =&gt; [5, 4, 6, 3, 1, 2]
  567. </pre>
  568. <p id="groupBy">
  569. <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
  570. <br />
  571. Splits a collection into sets, grouped by the result of running each
  572. value through <b>iterator</b>. If <b>iterator</b> is a string instead of
  573. a function, groups by the property named by <b>iterator</b> on each of
  574. the values.
  575. </p>
  576. <pre>
  577. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  578. =&gt; {1: [1.3], 2: [2.1, 2.4]}
  579. _.groupBy(['one', 'two', 'three'], 'length');
  580. =&gt; {3: ["one", "two"], 5: ["three"]}
  581. </pre>
  582. <p id="countBy">
  583. <b class="header">countBy</b><code>_.countBy(list, iterator)</code>
  584. <br />
  585. Sorts a list into groups and returns a count for the number of objects
  586. in each group.
  587. Similar to <tt>groupBy</tt>, but instead of returning a list of values,
  588. returns a count for the number of values in that group.
  589. </p>
  590. <pre>
  591. _.countBy([1, 2, 3, 4, 5], function(num) {
  592. return num % 2 == 0 ? 'even' : 'odd';
  593. });
  594. =&gt; {odd: 3, even: 2}
  595. </pre>
  596. <p id="shuffle">
  597. <b class="header">shuffle</b><code>_.shuffle(list)</code>
  598. <br />
  599. Returns a shuffled copy of the <b>list</b>, using a version of the
  600. <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
  601. </p>
  602. <pre>
  603. _.shuffle([1, 2, 3, 4, 5, 6]);
  604. =&gt; [4, 1, 6, 3, 5, 2]
  605. </pre>
  606. <p id="toArray">
  607. <b class="header">toArray</b><code>_.toArray(list)</code>
  608. <br />
  609. Converts the <b>list</b> (anything that can be iterated over), into a
  610. real Array. Useful for transmuting the <b>arguments</b> object.
  611. </p>
  612. <pre>
  613. (function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  614. =&gt; [2, 3, 4]
  615. </pre>
  616. <p id="size">
  617. <b class="header">size</b><code>_.size(list)</code>
  618. <br />
  619. Return the number of values in the <b>list</b>.
  620. </p>
  621. <pre>
  622. _.size({one : 1, two : 2, three : 3});
  623. =&gt; 3
  624. </pre>
  625. <h2 id="arrays">Array Functions</h2>
  626. <p>
  627. <i>
  628. Note: All array functions will also work on the <b>arguments</b> object.
  629. However, Underscore functions are not designed to work on "sparse" arrays.
  630. </i>
  631. </p>
  632. <p id="first">
  633. <b class="header">first</b><code>_.first(array, [n])</code>
  634. <span class="alias">Alias: <b>head</b>, <b>take</b></span>
  635. <br />
  636. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  637. return the first <b>n</b> elements of the array.
  638. </p>
  639. <pre>
  640. _.first([5, 4, 3, 2, 1]);
  641. =&gt; 5
  642. </pre>
  643. <p id="initial">
  644. <b class="header">initial</b><code>_.initial(array, [n])</code>
  645. <br />
  646. Returns everything but the last entry of the array. Especially useful on
  647. the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
  648. from the result.
  649. </p>
  650. <pre>
  651. _.initial([5, 4, 3, 2, 1]);
  652. =&gt; [5, 4, 3, 2]
  653. </pre>
  654. <p id="last">
  655. <b class="header">last</b><code>_.last(array, [n])</code>
  656. <br />
  657. Returns the last element of an <b>array</b>. Passing <b>n</b> will return
  658. the last <b>n</b> elements of the array.
  659. </p>
  660. <pre>
  661. _.last([5, 4, 3, 2, 1]);
  662. =&gt; 1
  663. </pre>
  664. <p id="rest">
  665. <b class="header">rest</b><code>_.rest(array, [index])</code>
  666. <span class="alias">Alias: <b>tail, drop</b></span>
  667. <br />
  668. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  669. to return the values of the array from that index onward.
  670. </p>
  671. <pre>
  672. _.rest([5, 4, 3, 2, 1]);
  673. =&gt; [4, 3, 2, 1]
  674. </pre>
  675. <p id="compact">
  676. <b class="header">compact</b><code>_.compact(array)</code>
  677. <br />
  678. Returns a copy of the <b>array</b> with all falsy values removed.
  679. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  680. <i>undefined</i> and <i>NaN</i> are all falsy.
  681. </p>
  682. <pre>
  683. _.compact([0, 1, false, 2, '', 3]);
  684. =&gt; [1, 2, 3]
  685. </pre>
  686. <p id="flatten">
  687. <b class="header">flatten</b><code>_.flatten(array, [shallow])</code>
  688. <br />
  689. Flattens a nested <b>array</b> (the nesting can be to any depth). If you
  690. pass <b>shallow</b>, the array will only be flattened a single level.
  691. </p>
  692. <pre>
  693. _.flatten([1, [2], [3, [[4]]]]);
  694. =&gt; [1, 2, 3, 4];
  695. _.flatten([1, [2], [3, [[4]]]], true);
  696. =&gt; [1, 2, 3, [[4]]];
  697. </pre>
  698. <p id="without">
  699. <b class="header">without</b><code>_.without(array, [*values])</code>
  700. <br />
  701. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  702. removed.
  703. </p>
  704. <pre>
  705. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  706. =&gt; [2, 3, 4]
  707. </pre>
  708. <p id="union">
  709. <b class="header">union</b><code>_.union(*arrays)</code>
  710. <br />
  711. Computes the union of the passed-in <b>arrays</b>: the list of unique items,
  712. in order, that are present in one or more of the <b>arrays</b>.
  713. </p>
  714. <pre>
  715. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  716. =&gt; [1, 2, 3, 101, 10]
  717. </pre>
  718. <p id="intersection">
  719. <b class="header">intersection</b><code>_.intersection(*arrays)</code>
  720. <br />
  721. Computes the list of values that are the intersection of all the <b>arrays</b>.
  722. Each value in the result is present in each of the <b>arrays</b>.
  723. </p>
  724. <pre>
  725. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  726. =&gt; [1, 2]
  727. </pre>
  728. <p id="difference">
  729. <b class="header">difference</b><code>_.difference(array, *others)</code>
  730. <br />
  731. Similar to <b>without</b>, but returns the values from <b>array</b> that
  732. are not present in the <b>other</b> arrays.
  733. </p>
  734. <pre>
  735. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  736. =&gt; [1, 3, 4]
  737. </pre>
  738. <p id="uniq">
  739. <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
  740. <span class="alias">Alias: <b>unique</b></span>
  741. <br />
  742. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  743. object equality. If you know in advance that the <b>array</b> is sorted,
  744. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  745. If you want to compute unique items based on a transformation, pass an
  746. <b>iterator</b> function.
  747. </p>
  748. <pre>
  749. _.uniq([1, 2, 1, 3, 1, 4]);
  750. =&gt; [1, 2, 3, 4]
  751. </pre>
  752. <p id="zip">
  753. <b class="header">zip</b><code>_.zip(*arrays)</code>
  754. <br />
  755. Merges together the values of each of the <b>arrays</b> with the
  756. values at the corresponding position. Useful when you have separate
  757. data sources that are coordinated through matching array indexes.
  758. If you're working with a matrix of nested arrays, <b>zip.apply</b>
  759. can transpose the matrix in a similar fashion.
  760. </p>
  761. <pre>
  762. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  763. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  764. </pre>
  765. <p id="object">
  766. <b class="header">object</b><code>_.object(list, [values])</code>
  767. <br />
  768. Converts arrays into objects. Pass either a single list of
  769. <tt>[key, value]</tt> pairs, or a list of keys, and a list of values.
  770. </p>
  771. <pre>
  772. _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
  773. =&gt; {moe: 30, larry: 40, curly: 50}
  774. _.object([['moe', 30], ['larry', 40], ['curly', 50]]);
  775. =&gt; {moe: 30, larry: 40, curly: 50}
  776. </pre>
  777. <p id="indexOf">
  778. <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
  779. <br />
  780. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  781. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  782. <b>indexOf</b> function unless it's missing. If you're working with a
  783. large array, and you know that the array is already sorted, pass <tt>true</tt>
  784. for <b>isSorted</b> to use a faster binary search ... or, pass a number as
  785. the third argument in order to look for the first matching value in the
  786. array after the given index.
  787. </p>
  788. <pre>
  789. _.indexOf([1, 2, 3], 2);
  790. =&gt; 1
  791. </pre>
  792. <p id="lastIndexOf">
  793. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value, [fromIndex])</code>
  794. <br />
  795. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  796. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  797. function if possible. Pass <b>fromIndex</b> to start your search at a
  798. given index.
  799. </p>
  800. <pre>
  801. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  802. =&gt; 4
  803. </pre>
  804. <p id="sortedIndex">
  805. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  806. <br />
  807. Uses a binary search to determine the index at which the <b>value</b>
  808. <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  809. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  810. the sort ranking of each value, including the <b>value</b> you pass.
  811. </p>
  812. <pre>
  813. _.sortedIndex([10, 20, 30, 40, 50], 35);
  814. =&gt; 3
  815. </pre>
  816. <p id="range">
  817. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  818. <br />
  819. A function to create flexibly-numbered lists of integers, handy for
  820. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  821. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  822. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  823. exclusive.
  824. </p>
  825. <pre>
  826. _.range(10);
  827. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  828. _.range(1, 11);
  829. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  830. _.range(0, 30, 5);
  831. =&gt; [0, 5, 10, 15, 20, 25]
  832. _.range(0, -10, -1);
  833. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  834. _.range(0);
  835. =&gt; []
  836. </pre>
  837. <h2 id="functions">Function (uh, ahem) Functions</h2>
  838. <p id="bind">
  839. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  840. <br />
  841. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  842. the function is called, the value of <i>this</i> will be the <b>object</b>.
  843. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  844. also known as <b>partial application</b>.
  845. </p>
  846. <pre>
  847. var func = function(greeting){ return greeting + ': ' + this.name };
  848. func = _.bind(func, {name : 'moe'}, 'hi');
  849. func();
  850. =&gt; 'hi: moe'
  851. </pre>
  852. <p id="bindAll">
  853. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  854. <br />
  855. Binds a number of methods on the <b>object</b>, specified by
  856. <b>methodNames</b>, to be run in the context of that object whenever they
  857. are invoked. Very handy for binding functions that are going to be used
  858. as event handlers, which would otherwise be invoked with a fairly useless
  859. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  860. function properties will be bound to it.
  861. </p>
  862. <pre>
  863. var buttonView = {
  864. label : 'underscore',
  865. onClick : function(){ alert('clicked: ' + this.label); },
  866. onHover : function(){ console.log('hovering: ' + this.label); }
  867. };
  868. _.bindAll(buttonView);
  869. jQuery('#underscore_button').bind('click', buttonView.onClick);
  870. =&gt; When the button is clicked, this.label will have the correct value...
  871. </pre>
  872. <p id="memoize">
  873. <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
  874. <br />
  875. Memoizes a given <b>function</b> by caching the computed result. Useful
  876. for speeding up slow-running computations. If passed an optional
  877. <b>hashFunction</b>, it will be used to compute the hash key for storing
  878. the result, based on the arguments to the original function. The default
  879. <b>hashFunction</b> just uses the first argument to the memoized function
  880. as the key.
  881. </p>
  882. <pre>
  883. var fibonacci = _.memoize(function(n) {
  884. return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  885. });
  886. </pre>
  887. <p id="delay">
  888. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  889. <br />
  890. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  891. milliseconds. If you pass the optional <b>arguments</b>, they will be
  892. forwarded on to the <b>function</b> when it is invoked.
  893. </p>
  894. <pre>
  895. var log = _.bind(console.log, console);
  896. _.delay(log, 1000, 'logged later');
  897. =&gt; 'logged later' // Appears after one second.
  898. </pre>
  899. <p id="defer">
  900. <b class="header">defer</b><code>_.defer(function, [*arguments])</code>
  901. <br />
  902. Defers invoking the <b>function</b> until the current call stack has cleared,
  903. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  904. expensive computations or HTML rendering in chunks without blocking the UI thread
  905. from updating. If you pass the optional <b>arguments</b>, they will be
  906. forwarded on to the <b>function</b> when it is invoked.
  907. </p>
  908. <pre>
  909. _.defer(function(){ alert('deferred'); });
  910. // Returns from the function before the alert runs.
  911. </pre>
  912. <p id="throttle">
  913. <b class="header">throttle</b><code>_.throttle(function, wait)</code>
  914. <br />
  915. Creates and returns a new, throttled version of the passed function,
  916. that, when invoked repeatedly, will only actually call the original function
  917. at most once per every <b>wait</b>
  918. milliseconds. Useful for rate-limiting events that occur faster than you
  919. can keep up with.
  920. </p>
  921. <pre>
  922. var throttled = _.throttle(updatePosition, 100);
  923. $(window).scroll(throttled);
  924. </pre>
  925. <p id="debounce">
  926. <b class="header">debounce</b><code>_.debounce(function, wait, [immediate])</code>
  927. <br />
  928. Creates and returns a new debounced version of the passed function that
  929. will postpone its execution until after
  930. <b>wait</b> milliseconds have elapsed since the last time it
  931. was invoked. Useful for implementing behavior that should only happen
  932. <i>after</i> the input has stopped arriving. For example: rendering a
  933. preview of a Markdown comment, recalculating a layout after the window
  934. has stopped being resized, and so on.
  935. </p>
  936. <p>
  937. Pass <tt>true</tt> for the <b>immediate</b> parameter to cause
  938. <b>debounce</b> to trigger the function on the leading instead of the
  939. trailing edge of the <b>wait</b> interval. Useful in circumstances like
  940. preventing accidental double-clicks on a "submit" button from firing a
  941. second time.
  942. </p>
  943. <pre>
  944. var lazyLayout = _.debounce(calculateLayout, 300);
  945. $(window).resize(lazyLayout);
  946. </pre>
  947. <p id="once">
  948. <b class="header">once</b><code>_.once(function)</code>
  949. <br />
  950. Creates a version of the function that can only be called one time.
  951. Repeated calls to the modified function will have no effect, returning
  952. the value from the original call. Useful for initialization functions,
  953. instead of having to set a boolean flag and then check it later.
  954. </p>
  955. <pre>
  956. var initialize = _.once(createApplication);
  957. initialize();
  958. initialize();
  959. // Application is only created once.
  960. </pre>
  961. <p id="after">
  962. <b class="header">after</b><code>_.after(count, function)</code>
  963. <br />
  964. Creates a version of the function that will only be run after first
  965. being called <b>count</b> times. Useful for grouping asynchronous responses,
  966. where you want to be sure that all the async calls have finished, before
  967. proceeding.
  968. </p>
  969. <pre>
  970. var renderNotes = _.after(notes.length, render);
  971. _.each(notes, function(note) {
  972. note.asyncSave({success: renderNotes});
  973. });
  974. // renderNotes is run once, after all notes have saved.
  975. </pre>
  976. <p id="wrap">
  977. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  978. <br />
  979. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  980. passing it as the first argument. This allows the <b>wrapper</b> to
  981. execute code before and after the <b>function</b> runs, adjust the arguments,
  982. and execute it conditionally.
  983. </p>
  984. <pre>
  985. var hello = function(name) { return "hello: " + name; };
  986. hello = _.wrap(hello, function(func) {
  987. return "before, " + func("moe") + ", after";
  988. });
  989. hello();
  990. =&gt; 'before, hello: moe, after'
  991. </pre>
  992. <p id="compose">
  993. <b class="header">compose</b><code>_.compose(*functions)</code>
  994. <br />
  995. Returns the composition of a list of <b>functions</b>, where each function
  996. consumes the return value of the function that follows. In math terms,
  997. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  998. <i>f(g(h()))</i>.
  999. </p>
  1000. <pre>
  1001. var greet = function(name){ return "hi: " + name; };
  1002. var exclaim = function(statement){ return statement + "!"; };
  1003. var welcome = _.compose(exclaim, greet);
  1004. welcome('moe');
  1005. =&gt; 'hi: moe!'
  1006. </pre>
  1007. <h2 id="objects">Object Functions</h2>
  1008. <p id="keys">
  1009. <b class="header">keys</b><code>_.keys(object)</code>
  1010. <br />
  1011. Retrieve all the names of the <b>object</b>'s properties.
  1012. </p>
  1013. <pre>
  1014. _.keys({one : 1, two : 2, three : 3});
  1015. =&gt; ["one", "two", "three"]
  1016. </pre>
  1017. <p id="values">
  1018. <b class="header">values</b><code>_.values(object)</code>
  1019. <br />
  1020. Return all of the values of the <b>object</b>'s properties.
  1021. </p>
  1022. <pre>
  1023. _.values({one : 1, two : 2, three : 3});
  1024. =&gt; [1, 2, 3]
  1025. </pre>
  1026. <p id="pairs">
  1027. <b class="header">pairs</b><code>_.pairs(object)</code>
  1028. <br />
  1029. Convert an object into a list of <tt>[key, value]</tt> pairs.
  1030. </p>
  1031. <pre>
  1032. _.pairs({one : 1, two : 2, three : 3});
  1033. =&gt; [[one: 1], [two: 2], [three: 3]]
  1034. </pre>
  1035. <p id="invert">
  1036. <b class="header">invert</b><code>_.invert(object)</code>
  1037. <br />
  1038. Returns a copy of the <b>object</b> where the keys have become the values
  1039. and the values the keys. For this to work, all of your object's values
  1040. should be unique and string serializable.
  1041. </p>
  1042. <pre>
  1043. _.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"});
  1044. =&gt; {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};
  1045. </pre>
  1046. <p id="object-functions">
  1047. <b class="header">functions</b><code>_.functions(object)</code>
  1048. <span class="alias">Alias: <b>methods</b></span>
  1049. <br />
  1050. Returns a sorted list of the names of every method in an object &mdash;
  1051. that is to say, the name of every function property of the object.
  1052. </p>
  1053. <pre>
  1054. _.functions(_);
  1055. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  1056. </pre>
  1057. <p id="extend">
  1058. <b class="header">extend</b><code>_.extend(destination, *sources)</code>
  1059. <br />
  1060. Copy all of the properties in the <b>source</b> objects over to the
  1061. <b>destination</b> object, and return the <b>destination</b> object.
  1062. It's in-order, so the last source will override properties of the same
  1063. name in previous arguments.
  1064. </p>
  1065. <pre>
  1066. _.extend({name : 'moe'}, {age : 50});
  1067. =&gt; {name : 'moe', age : 50}
  1068. </pre>
  1069. <p id="pick">
  1070. <b class="header">pick</b><code>_.pick(object, *keys)</code>
  1071. <br />
  1072. Return a copy of the <b>object</b>, filtered to only have values for
  1073. the whitelisted <b>keys</b> (or array of valid keys).
  1074. </p>
  1075. <pre>
  1076. _.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');
  1077. =&gt; {name : 'moe', age : 50}
  1078. </pre>
  1079. <p id="omit">
  1080. <b class="header">omit</b><code>_.omit(object, *keys)</code>
  1081. <br />
  1082. Return a copy of the <b>object</b>, filtered to omit the blacklisted
  1083. <b>keys</b> (or array of keys).
  1084. </p>
  1085. <pre>
  1086. _.omit({name : 'moe', age : 50, userid : 'moe1'}, 'userid');
  1087. =&gt; {name : 'moe', age : 50}
  1088. </pre>
  1089. <p id="defaults">
  1090. <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
  1091. <br />
  1092. Fill in null and undefined properties in <b>object</b> with values from the
  1093. <b>defaults</b> objects, and return the <b>object</b>. As soon as the
  1094. property is filled, further defaults will have no effect.
  1095. </p>
  1096. <pre>
  1097. var iceCream = {flavor : "chocolate"};
  1098. _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
  1099. =&gt; {flavor : "chocolate", sprinkles : "lots"}
  1100. </pre>
  1101. <p id="clone">
  1102. <b class="header">clone</b><code>_.clone(object)</code>
  1103. <br />
  1104. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  1105. or arrays will be copied by reference, not duplicated.
  1106. </p>
  1107. <pre>
  1108. _.clone({name : 'moe'});
  1109. =&gt; {name : 'moe'};
  1110. </pre>
  1111. <p id="tap">
  1112. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  1113. <br />
  1114. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  1115. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  1116. </p>
  1117. <pre>
  1118. _.chain([1,2,3,200])
  1119. .filter(function(num) { return num % 2 == 0; })
  1120. .tap(alert)
  1121. .map(function(num) { return num * num })
  1122. .value();
  1123. =&gt; // [2, 200] (alerted)
  1124. =&gt; [4, 40000]
  1125. </pre>
  1126. <p id="has">
  1127. <b class="header">has</b><code>_.has(object, key)</code>
  1128. <br />
  1129. Does the object contain the given key? Identical to
  1130. <tt>object.hasOwnProperty(key)</tt>, but uses a safe reference to the
  1131. <tt>hasOwnProperty</tt> function, in case it's been
  1132. <a href="http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/">overridden accidentally</a>.
  1133. </p>
  1134. <pre>
  1135. _.has({a: 1, b: 2, c: 3}, "b");
  1136. =&gt; true
  1137. </pre>
  1138. <p id="isEqual">
  1139. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  1140. <br />
  1141. Performs an optimized deep comparison between the two objects, to determine
  1142. if they should be considered equal.
  1143. </p>
  1144. <pre>
  1145. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  1146. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  1147. moe == clone;
  1148. =&gt; false
  1149. _.isEqual(moe, clone);
  1150. =&gt; true
  1151. </pre>
  1152. <p id="isEmpty">
  1153. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  1154. <br />
  1155. Returns <i>true</i> if <b>object</b> contains no values.
  1156. </p>
  1157. <pre>
  1158. _.isEmpty([1, 2, 3]);
  1159. =&gt; false
  1160. _.isEmpty({});
  1161. =&gt; true
  1162. </pre>
  1163. <p id="isElement">
  1164. <b class="header">isElement</b><code>_.isElement(object)</code>
  1165. <br />
  1166. Returns <i>true</i> if <b>object</b> is a DOM element.
  1167. </p>
  1168. <pre>
  1169. _.isElement(jQuery('body')[0]);
  1170. =&gt; true
  1171. </pre>
  1172. <p id="isArray">
  1173. <b class="header">isArray</b><code>_.isArray(object)</code>
  1174. <br />
  1175. Returns <i>true</i> if <b>object</b> is an Array.
  1176. </p>
  1177. <pre>
  1178. (function(){ return _.isArray(arguments); })();
  1179. =&gt; false
  1180. _.isArray([1,2,3]);
  1181. =&gt; true
  1182. </pre>
  1183. <p id="isObject">
  1184. <b class="header">isObject</b><code>_.isObject(value)</code>
  1185. <br />
  1186. Returns <i>true</i> if <b>value</b> is an Object. Note that JavaScript
  1187. arrays and functions are objects, while (normal) strings and numbers are not.
  1188. </p>
  1189. <pre>
  1190. _.isObject({});
  1191. =&gt; true
  1192. _.isObject(1);
  1193. =&gt; false
  1194. </pre>
  1195. <p id="isArguments">
  1196. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  1197. <br />
  1198. Returns <i>true</i> if <b>object</b> is an Arguments object.
  1199. </p>
  1200. <pre>
  1201. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  1202. =&gt; true
  1203. _.isArguments([1,2,3]);
  1204. =&gt; false
  1205. </pre>
  1206. <p id="isFunction">
  1207. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  1208. <br />
  1209. Returns <i>true</i> if <b>object</b> is a Function.
  1210. </p>
  1211. <pre>
  1212. _.isFunction(alert);
  1213. =&gt; true
  1214. </pre>
  1215. <p id="isString">
  1216. <b class="header">isString</b><code>_.isString(object)</code>
  1217. <br />
  1218. Returns <i>true</i> if <b>object</b> is a String.
  1219. </p>
  1220. <pre>
  1221. _.isString("moe");
  1222. =&gt; true
  1223. </pre>
  1224. <p id="isNumber">
  1225. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  1226. <br />
  1227. Returns <i>true</i> if <b>object</b> is a Number (including <tt>NaN</tt>).
  1228. </p>
  1229. <pre>
  1230. _.isNumber(8.4 * 5);
  1231. =&gt; true
  1232. </pre>
  1233. <p id="isFinite">
  1234. <b class="header">isFinite</b><code>_.isFinite(object)</code>
  1235. <br />
  1236. Returns <i>true</i> if <b>object</b> is a finite Number.
  1237. </p>
  1238. <pre>
  1239. _.isFinite(-101);
  1240. =&gt; true
  1241. _.isFinite(-Infinity);
  1242. =&gt; false
  1243. </pre>
  1244. <p id="isBoolean">
  1245. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  1246. <br />
  1247. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  1248. </p>
  1249. <pre>
  1250. _.isBoolean(null);
  1251. =&gt; false
  1252. </pre>
  1253. <p id="isDate">
  1254. <b class="header">isDate</b><code>_.isDate(object)</code>
  1255. <br />
  1256. Returns <i>true</i> if <b>object</b> is a Date.
  1257. </p>
  1258. <pre>
  1259. _.isDate(new Date());
  1260. =&gt; true
  1261. </pre>
  1262. <p id="isRegExp">
  1263. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  1264. <br />
  1265. Returns <i>true</i> if <b>object</b> is a RegExp.
  1266. </p>
  1267. <pre>
  1268. _.isRegExp(/moe/);
  1269. =&gt; true
  1270. </pre>
  1271. <p id="isNaN">
  1272. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  1273. <br />
  1274. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  1275. the same as the native <b>isNaN</b> function, which will also return
  1276. true if the variable is <i>undefined</i>.
  1277. </p>
  1278. <pre>
  1279. _.isNaN(NaN);
  1280. =&gt; true
  1281. isNaN(undefined);
  1282. =&gt; true
  1283. _.isNaN(undefined);
  1284. =&gt; false
  1285. </pre>
  1286. <p id="isNull">
  1287. <b class="header">isNull</b><code>_.isNull(object)</code>
  1288. <br />
  1289. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  1290. </p>
  1291. <pre>
  1292. _.isNull(null);
  1293. =&gt; true
  1294. _.isNull(undefined);
  1295. =&gt; false
  1296. </pre>
  1297. <p id="isUndefined">
  1298. <b class="header">isUndefined</b><code>_.isUndefined(value)</code>
  1299. <br />
  1300. Returns <i>true</i> if <b>value</b> is <i>undefined</i>.
  1301. </p>
  1302. <pre>
  1303. _.isUndefined(window.missingVariable);
  1304. =&gt; true
  1305. </pre>
  1306. <h2 id="utility">Utility Functions</h2>
  1307. <p id="noConflict">
  1308. <b class="header">noConflict</b><code>_.noConflict()</code>
  1309. <br />
  1310. Give control of the "_" variable back to its previous owner. Returns
  1311. a reference to the <b>Underscore</b> object.
  1312. </p>
  1313. <pre>
  1314. var underscore = _.noConflict();</pre>
  1315. <p id="identity">
  1316. <b class="header">identity</b><code>_.identity(value)</code>
  1317. <br />
  1318. Returns the same value that is used as the argument. In math:
  1319. <tt>f(x) = x</tt><br />
  1320. This function looks useless, but is used throughout Underscore as
  1321. a default iterator.
  1322. </p>
  1323. <pre>
  1324. var moe = {name : 'moe'};
  1325. moe === _.identity(moe);
  1326. =&gt; true</pre>
  1327. <p id="times">
  1328. <b class="header">times</b><code>_.times(n, iterator, [context])</code>
  1329. <br />
  1330. Invokes the given iterator function <b>n</b> times. Each invocation of
  1331. <b>iterator</b> is called with an <tt>index</tt> argument.
  1332. </p>
  1333. <pre>
  1334. _(3).times(function(n){ genie.grantWishNumber(n); });</pre>
  1335. <p id="random">
  1336. <b class="header">random</b><code>_.random(min, max)</code>
  1337. <br />
  1338. Returns a random integer between <b>min</b> and <b>max</b>, inclusive.
  1339. If you only pass one argument, it will return a number between <tt>0</tt>
  1340. and that number.
  1341. </p>
  1342. <pre>
  1343. _.random(0, 100);
  1344. =&gt; 42</pre>
  1345. <p id="mixin">
  1346. <b class="header">mixin</b><code>_.mixin(object)</code>
  1347. <br />
  1348. Allows you to extend Underscore with your own utility functions. Pass
  1349. a hash of <tt>{name: function}</tt> definitions to have your functions
  1350. added to the Underscore object, as well as the OOP wrapper.
  1351. </p>
  1352. <pre>
  1353. _.mixin({
  1354. capitalize : function(string) {
  1355. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  1356. }
  1357. });
  1358. _("fabio").capitalize();
  1359. =&gt; "Fabio"
  1360. </pre>
  1361. <p id="uniqueId">
  1362. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  1363. <br />
  1364. Generate a globally-unique id for client-side models or DOM elements
  1365. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  1366. Without <b>prefix</b>, returns an integer.
  1367. </p>
  1368. <pre>
  1369. _.uniqueId('contact_');
  1370. =&gt; 'contact_104'</pre>
  1371. <p id="escape">
  1372. <b class="header">escape</b><code>_.escape(string)</code>

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