PageRenderTime 81ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/underscore/index.html

https://bitbucket.org/asci/brainstormer
HTML | 1937 lines | 1754 code | 182 blank | 1 comment | 0 complexity | da6f949492160cea5e90522c66915f85 MD5 | raw file
Possible License(s): MIT, Apache-2.0, BSD-3-Clause

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

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="chrome=1">
  6. <title>Underscore.js</title>
  7. <style>
  8. body {
  9. font-size: 14px;
  10. line-height: 22px;
  11. background: #f4f4f4 url(docs/images/background.png);
  12. color: #000;
  13. font-family: Helvetica Neue, Helvetica, Arial;
  14. }
  15. .interface {
  16. font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
  17. }
  18. div#sidebar {
  19. background: #fff;
  20. position: fixed;
  21. top: 0; left: 0; bottom: 0;
  22. width: 200px;
  23. overflow-y: auto;
  24. overflow-x: hidden;
  25. padding: 15px 0 30px 30px;
  26. border-right: 1px solid #bbb;
  27. box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
  28. }
  29. a.toc_title, a.toc_title:visited {
  30. display: block;
  31. color: black;
  32. font-weight: bold;
  33. margin-top: 15px;
  34. }
  35. a.toc_title:hover {
  36. text-decoration: underline;
  37. }
  38. #sidebar .version {
  39. font-size: 10px;
  40. font-weight: normal;
  41. }
  42. ul.toc_section {
  43. font-size: 11px;
  44. line-height: 14px;
  45. margin: 5px 0 0 0;
  46. padding-left: 0px;
  47. list-style-type: none;
  48. font-family: Lucida Grande;
  49. }
  50. .toc_section li {
  51. cursor: pointer;
  52. margin: 0 0 3px 0;
  53. }
  54. .toc_section li a {
  55. text-decoration: none;
  56. color: black;
  57. }
  58. .toc_section li a:hover {
  59. text-decoration: underline;
  60. }
  61. div.container {
  62. width: 550px;
  63. margin: 40px 0 50px 260px;
  64. }
  65. div.warning {
  66. margin-top: 15px;
  67. font: bold 11px Arial;
  68. color: #770000;
  69. }
  70. p {
  71. margin: 20px 0;
  72. width: 550px;
  73. }
  74. a, a:visited {
  75. color: #444;
  76. }
  77. a:active, a:hover {
  78. color: #000;
  79. }
  80. h1, h2, h3, h4, h5, h6 {
  81. padding-top: 20px;
  82. }
  83. h2 {
  84. font-size: 20px;
  85. }
  86. b.header {
  87. font-size: 16px;
  88. line-height: 30px;
  89. }
  90. span.alias {
  91. font-size: 14px;
  92. font-style: italic;
  93. margin-left: 20px;
  94. }
  95. table, tr, td {
  96. margin: 0; padding: 0;
  97. }
  98. td {
  99. padding: 2px 12px 2px 0;
  100. }
  101. ul {
  102. list-style-type: circle;
  103. padding: 0 0 0 20px;
  104. }
  105. li {
  106. width: 500px;
  107. margin-bottom: 10px;
  108. }
  109. code, pre, tt {
  110. font-family: Monaco, Consolas, "Lucida Console", monospace;
  111. font-size: 12px;
  112. line-height: 18px;
  113. font-style: normal;
  114. }
  115. tt {
  116. padding: 0px 3px;
  117. background: #fff;
  118. border: 1px solid #ddd;
  119. zoom: 1;
  120. }
  121. code {
  122. margin-left: 20px;
  123. }
  124. pre {
  125. font-size: 12px;
  126. padding: 2px 0 2px 15px;
  127. border-left: 5px solid #bbb;
  128. margin: 0px 0 30px;
  129. }
  130. </style>
  131. </head>
  132. <body>
  133. <div id="sidebar" class="interface">
  134. <a class="toc_title" href="#">
  135. Underscore.js <span class="version">(1.3.0)</span>
  136. </a>
  137. <a class="toc_title" href="#">
  138. Introduction
  139. </a>
  140. <a class="toc_title" href="#collections">
  141. Collections
  142. </a>
  143. <ul class="toc_section">
  144. <li>- <a href="#each">each</a></li>
  145. <li>- <a href="#map">map</a></li>
  146. <li>- <a href="#reduce">reduce</a></li>
  147. <li>- <a href="#reduceRight">reduceRight</a></li>
  148. <li>- <a href="#find">find</a></li>
  149. <li>- <a href="#filter">filter</a></li>
  150. <li>- <a href="#reject">reject</a></li>
  151. <li>- <a href="#all">all</a></li>
  152. <li>- <a href="#any">any</a></li>
  153. <li>- <a href="#include">include</a></li>
  154. <li>- <a href="#invoke">invoke</a></li>
  155. <li>- <a href="#pluck">pluck</a></li>
  156. <li>- <a href="#max">max</a></li>
  157. <li>- <a href="#min">min</a></li>
  158. <li>- <a href="#sortBy">sortBy</a></li>
  159. <li>- <a href="#groupBy">groupBy</a></li>
  160. <li>- <a href="#sortedIndex">sortedIndex</a></li>
  161. <li>- <a href="#shuffle">shuffle</a></li>
  162. <li>- <a href="#toArray">toArray</a></li>
  163. <li>- <a href="#size">size</a></li>
  164. </ul>
  165. <a class="toc_title" href="#arrays">
  166. Arrays
  167. </a>
  168. <ul class="toc_section">
  169. <li>- <a href="#first">first</a></li>
  170. <li>- <a href="#initial">initial</a></li>
  171. <li>- <a href="#last">last</a></li>
  172. <li>- <a href="#rest">rest</a></li>
  173. <li>- <a href="#compact">compact</a></li>
  174. <li>- <a href="#flatten">flatten</a></li>
  175. <li>- <a href="#without">without</a></li>
  176. <li>- <a href="#union">union</a></li>
  177. <li>- <a href="#intersection">intersection</a></li>
  178. <li>- <a href="#difference">difference</a></li>
  179. <li>- <a href="#uniq">uniq</a></li>
  180. <li>- <a href="#zip">zip</a></li>
  181. <li>- <a href="#indexOf">indexOf</a></li>
  182. <li>- <a href="#lastIndexOf">lastIndexOf</a></li>
  183. <li>- <a href="#range">range</a></li>
  184. </ul>
  185. <a class="toc_title" href="#functions">
  186. Functions
  187. </a>
  188. <ul class="toc_section">
  189. <li>- <a href="#bind">bind</a></li>
  190. <li>- <a href="#bindAll">bindAll</a></li>
  191. <li>- <a href="#memoize">memoize</a></li>
  192. <li>- <a href="#delay">delay</a></li>
  193. <li>- <a href="#defer">defer</a></li>
  194. <li>- <a href="#throttle">throttle</a></li>
  195. <li>- <a href="#debounce">debounce</a></li>
  196. <li>- <a href="#once">once</a></li>
  197. <li>- <a href="#after">after</a></li>
  198. <li>- <a href="#wrap">wrap</a></li>
  199. <li>- <a href="#compose">compose</a></li>
  200. </ul>
  201. <a class="toc_title" href="#objects">
  202. Objects
  203. </a>
  204. <ul class="toc_section">
  205. <li>- <a href="#keys">keys</a></li>
  206. <li>- <a href="#values">values</a></li>
  207. <li>- <a href="#functions">functions</a></li>
  208. <li>- <a href="#extend">extend</a></li>
  209. <li>- <a href="#defaults">defaults</a></li>
  210. <li>- <a href="#clone">clone</a></li>
  211. <li>- <a href="#tap">tap</a></li>
  212. <li>- <a href="#isEqual">isEqual</a></li>
  213. <li>- <a href="#isEmpty">isEmpty</a></li>
  214. <li>- <a href="#isElement">isElement</a></li>
  215. <li>- <a href="#isArray">isArray</a></li>
  216. <li>- <a href="#isArguments">isArguments</a></li>
  217. <li>- <a href="#isFunction">isFunction</a></li>
  218. <li>- <a href="#isString">isString</a></li>
  219. <li>- <a href="#isNumber">isNumber</a></li>
  220. <li>- <a href="#isBoolean">isBoolean</a></li>
  221. <li>- <a href="#isDate">isDate</a></li>
  222. <li>- <a href="#isRegExp">isRegExp</a></li>
  223. <li>- <a href="#isNaN">isNaN</a></li>
  224. <li>- <a href="#isNull">isNull</a></li>
  225. <li>- <a href="#isUndefined">isUndefined</a></li>
  226. </ul>
  227. <a class="toc_title" href="#utility">
  228. Utility
  229. </a>
  230. <ul class="toc_section">
  231. <li>- <a href="#noConflict">noConflict</a></li>
  232. <li>- <a href="#identity">identity</a></li>
  233. <li>- <a href="#times">times</a></li>
  234. <li>- <a href="#mixin">mixin</a></li>
  235. <li>- <a href="#uniqueId">uniqueId</a></li>
  236. <li>- <a href="#escape">escape</a></li>
  237. <li>- <a href="#template">template</a></li>
  238. </ul>
  239. <a class="toc_title" href="#chaining">
  240. Chaining
  241. </a>
  242. <ul class="toc_section">
  243. <li>- <a href="#chain">chain</a></li>
  244. <li>- <a href="#value">value</a></li>
  245. </ul>
  246. <a class="toc_title" href="#links">
  247. Links
  248. </a>
  249. <a class="toc_title" href="#changelog">
  250. Change Log
  251. </a>
  252. </div>
  253. <div class="container">
  254. <p id="introduction">
  255. <img style="width: 396px; height: 69px;" src="docs/images/underscore.png" alt="Underscore.js" />
  256. </p>
  257. <p>
  258. <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
  259. utility-belt library for JavaScript that provides a lot of the
  260. functional programming support that you would expect in
  261. <a href="http://prototypejs.org/api">Prototype.js</a>
  262. (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
  263. but without extending any of the built-in JavaScript objects. It's the
  264. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
  265. and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
  266. </p>
  267. <p>
  268. Underscore provides 60-odd functions that support both the usual
  269. functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
  270. as well as more specialized helpers: function binding, javascript
  271. templating, deep equality testing, and so on. It delegates to built-in
  272. functions, if present, so modern browsers will use the
  273. native implementations of <b>forEach</b>, <b>map</b>, <b>reduce</b>,
  274. <b>filter</b>, <b>every</b>, <b>some</b> and <b>indexOf</b>.
  275. </p>
  276. <p>
  277. A complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
  278. is included for your perusal.
  279. </p>
  280. <p>
  281. You may also read through the <a href="docs/underscore.html">annotated source code</a>.
  282. </p>
  283. <p>
  284. The project is
  285. <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
  286. You can report bugs and discuss features on the
  287. <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
  288. on Freenode in the <tt>#documentcloud</tt> channel,
  289. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  290. </p>
  291. <p>
  292. <i>Underscore is an open-source component of <a href="http://documentcloud.org/">DocumentCloud</a>.</i>
  293. </p>
  294. <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
  295. <table>
  296. <tr>
  297. <td><a href="underscore.js">Development Version (1.3.0)</a></td>
  298. <td><i>34kb, Uncompressed with Comments</i></td>
  299. </tr>
  300. <tr>
  301. <td><a href="underscore-min.js">Production Version (1.3.0)</a></td>
  302. <td><i>&lt; 4kb, Minified and Gzipped</i></td>
  303. </tr>
  304. </table>
  305. <div class="warning">Upgrade warning: version 1.3.0 removes AMD (RequireJS) support.</div>
  306. <div id="documentation">
  307. <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
  308. <p id="each">
  309. <b class="header">each</b><code>_.each(list, iterator, [context])</code>
  310. <span class="alias">Alias: <b>forEach</b></span>
  311. <br />
  312. Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
  313. function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
  314. passed. Each invocation of <b>iterator</b> is called with three arguments:
  315. <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
  316. arguments will be <tt>(value, key, list)</tt>. Delegates to the native
  317. <b>forEach</b> function if it exists.
  318. </p>
  319. <pre>
  320. _.each([1, 2, 3], function(num){ alert(num); });
  321. =&gt; alerts each number in turn...
  322. _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });
  323. =&gt; alerts each number in turn...</pre>
  324. <p id="map">
  325. <b class="header">map</b><code>_.map(list, iterator, [context])</code>
  326. <br />
  327. Produces a new array of values by mapping each value in <b>list</b>
  328. through a transformation function (<b>iterator</b>). If the native <b>map</b> method
  329. exists, it will be used instead. If <b>list</b> is a JavaScript object,
  330. <b>iterator</b>'s arguments will be <tt>(value, key, list)</tt>.
  331. </p>
  332. <pre>
  333. _.map([1, 2, 3], function(num){ return num * 3; });
  334. =&gt; [3, 6, 9]
  335. _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
  336. =&gt; [3, 6, 9]</pre>
  337. <p id="reduce">
  338. <b class="header">reduce</b><code>_.reduce(list, iterator, memo, [context])</code>
  339. <span class="alias">Aliases: <b>inject, foldl</b></span>
  340. <br />
  341. Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
  342. <b>list</b> of values into a single value. <b>Memo</b> is the initial state
  343. of the reduction, and each successive step of it should be returned by
  344. <b>iterator</b>.
  345. </p>
  346. <pre>
  347. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  348. =&gt; 6
  349. </pre>
  350. <p id="reduceRight">
  351. <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
  352. <span class="alias">Alias: <b>foldr</b></span>
  353. <br />
  354. The right-associative version of <b>reduce</b>. Delegates to the
  355. JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
  356. is not as useful in JavaScript as it would be in a language with lazy
  357. evaluation.
  358. </p>
  359. <pre>
  360. var list = [[0, 1], [2, 3], [4, 5]];
  361. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  362. =&gt; [4, 5, 2, 3, 0, 1]
  363. </pre>
  364. <p id="find">
  365. <b class="header">find</b><code>_.find(list, iterator, [context])</code>
  366. <span class="alias">Alias: <b>detect</b></span>
  367. <br />
  368. Looks through each value in the <b>list</b>, returning the first one that
  369. passes a truth test (<b>iterator</b>). The function returns as
  370. soon as it finds an acceptable element, and doesn't traverse the
  371. entire list.
  372. </p>
  373. <pre>
  374. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  375. =&gt; 2
  376. </pre>
  377. <p id="filter">
  378. <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
  379. <span class="alias">Alias: <b>select</b></span>
  380. <br />
  381. Looks through each value in the <b>list</b>, returning an array of all
  382. the values that pass a truth test (<b>iterator</b>). Delegates to the
  383. native <b>filter</b> method, if it exists.
  384. </p>
  385. <pre>
  386. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  387. =&gt; [2, 4, 6]
  388. </pre>
  389. <p id="reject">
  390. <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
  391. <br />
  392. Returns the values in <b>list</b> without the elements that the truth
  393. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
  394. </p>
  395. <pre>
  396. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  397. =&gt; [1, 3, 5]
  398. </pre>
  399. <p id="all">
  400. <b class="header">all</b><code>_.all(list, iterator, [context])</code>
  401. <span class="alias">Alias: <b>every</b></span>
  402. <br />
  403. Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
  404. truth test. Delegates to the native method <b>every</b>, if present.
  405. </p>
  406. <pre>
  407. _.all([true, 1, null, 'yes'], _.identity);
  408. =&gt; false
  409. </pre>
  410. <p id="any">
  411. <b class="header">any</b><code>_.any(list, [iterator], [context])</code>
  412. <span class="alias">Alias: <b>some</b></span>
  413. <br />
  414. Returns <i>true</i> if any of the values in the <b>list</b> pass the
  415. <b>iterator</b> truth test. Short-circuits and stops traversing the list
  416. if a true element is found. Delegates to the native method <b>some</b>,
  417. if present.
  418. </p>
  419. <pre>
  420. _.any([null, 0, 'yes', false]);
  421. =&gt; true
  422. </pre>
  423. <p id="include">
  424. <b class="header">include</b><code>_.include(list, value)</code>
  425. <span class="alias">Alias: <b>contains</b></span>
  426. <br />
  427. Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
  428. <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
  429. is an Array.
  430. </p>
  431. <pre>
  432. _.include([1, 2, 3], 3);
  433. =&gt; true
  434. </pre>
  435. <p id="invoke">
  436. <b class="header">invoke</b><code>_.invoke(list, methodName, [*arguments])</code>
  437. <br />
  438. Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
  439. Any extra arguments passed to <b>invoke</b> will be forwarded on to the
  440. method invocation.
  441. </p>
  442. <pre>
  443. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  444. =&gt; [[1, 5, 7], [1, 2, 3]]
  445. </pre>
  446. <p id="pluck">
  447. <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
  448. <br />
  449. A convenient version of what is perhaps the most common use-case for
  450. <b>map</b>: extracting a list of property values.
  451. </p>
  452. <pre>
  453. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  454. _.pluck(stooges, 'name');
  455. =&gt; ["moe", "larry", "curly"]
  456. </pre>
  457. <p id="max">
  458. <b class="header">max</b><code>_.max(list, [iterator], [context])</code>
  459. <br />
  460. Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
  461. it will be used on each value to generate the criterion by which the
  462. value is ranked.
  463. </p>
  464. <pre>
  465. var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
  466. _.max(stooges, function(stooge){ return stooge.age; });
  467. =&gt; {name : 'curly', age : 60};
  468. </pre>
  469. <p id="min">
  470. <b class="header">min</b><code>_.min(list, [iterator], [context])</code>
  471. <br />
  472. Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
  473. it will be used on each value to generate the criterion by which the
  474. value is ranked.
  475. </p>
  476. <pre>
  477. var numbers = [10, 5, 100, 2, 1000];
  478. _.min(numbers);
  479. =&gt; 2
  480. </pre>
  481. <p id="sortBy">
  482. <b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
  483. <br />
  484. Returns a sorted copy of <b>list</b>, ranked by the results of running
  485. each value through <b>iterator</b>.
  486. </p>
  487. <pre>
  488. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  489. =&gt; [5, 4, 6, 3, 1, 2]
  490. </pre>
  491. <p id="groupBy">
  492. <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
  493. <br />
  494. Splits a collection into sets, grouped by the result of running each
  495. value through <b>iterator</b>. If <b>iterator</b> is a string instead of
  496. a function, groups by the property named by <b>iterator</b> on each of
  497. the values.
  498. </p>
  499. <pre>
  500. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  501. =&gt; {1: [1.3], 2: [2.1, 2.4]}
  502. _.groupBy(['one', 'two', 'three'], 'length');
  503. =&gt; {3: ["one", "two"], 5: ["three"]}
  504. </pre>
  505. <p id="sortedIndex">
  506. <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
  507. <br />
  508. Uses a binary search to determine the index at which the <b>value</b>
  509. <i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
  510. sorted order. If an <b>iterator</b> is passed, it will be used to compute
  511. the sort ranking of each value.
  512. </p>
  513. <pre>
  514. _.sortedIndex([10, 20, 30, 40, 50], 35);
  515. =&gt; 3
  516. </pre>
  517. <p id="shuffle">
  518. <b class="header">shuffle</b><code>_.shuffle(list)</code>
  519. <br />
  520. Returns a shuffled copy of the <b>list</b>, using a version of the
  521. <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
  522. </p>
  523. <pre>
  524. _.shuffle([1, 2, 3, 4, 5, 6]);
  525. =&gt; [4, 1, 6, 3, 5, 2]
  526. </pre>
  527. <p id="toArray">
  528. <b class="header">toArray</b><code>_.toArray(list)</code>
  529. <br />
  530. Converts the <b>list</b> (anything that can be iterated over), into a
  531. real Array. Useful for transmuting the <b>arguments</b> object.
  532. </p>
  533. <pre>
  534. (function(){ return _.toArray(arguments).slice(0); })(1, 2, 3);
  535. =&gt; [1, 2, 3]
  536. </pre>
  537. <p id="size">
  538. <b class="header">size</b><code>_.size(list)</code>
  539. <br />
  540. Return the number of values in the <b>list</b>.
  541. </p>
  542. <pre>
  543. _.size({one : 1, two : 2, three : 3});
  544. =&gt; 3
  545. </pre>
  546. <h2 id="arrays">Array Functions</h2>
  547. <p>
  548. <i>Note: All array functions will also work on the <b>arguments</b> object.</i>
  549. </p>
  550. <p id="first">
  551. <b class="header">first</b><code>_.first(array, [n])</code>
  552. <span class="alias">Alias: <b>head</b></span>
  553. <br />
  554. Returns the first element of an <b>array</b>. Passing <b>n</b> will
  555. return the first <b>n</b> elements of the array.
  556. </p>
  557. <pre>
  558. _.first([5, 4, 3, 2, 1]);
  559. =&gt; 5
  560. </pre>
  561. <p id="initial">
  562. <b class="header">initial</b><code>_.initial(array, [n])</code>
  563. <br />
  564. Returns everything but the last entry of the array. Especially useful on
  565. the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
  566. from the result.
  567. </p>
  568. <pre>
  569. _.initial([5, 4, 3, 2, 1]);
  570. =&gt; [5, 4, 3, 2]
  571. </pre>
  572. <p id="last">
  573. <b class="header">last</b><code>_.last(array, [n])</code>
  574. <br />
  575. Returns the last element of an <b>array</b>. Passing <b>n</b> will return
  576. the last <b>n</b> elements of the array.
  577. </p>
  578. <pre>
  579. _.last([5, 4, 3, 2, 1]);
  580. =&gt; 1
  581. </pre>
  582. <p id="rest">
  583. <b class="header">rest</b><code>_.rest(array, [index])</code>
  584. <span class="alias">Alias: <b>tail</b></span>
  585. <br />
  586. Returns the <b>rest</b> of the elements in an array. Pass an <b>index</b>
  587. to return the values of the array from that index onward.
  588. </p>
  589. <pre>
  590. _.rest([5, 4, 3, 2, 1]);
  591. =&gt; [4, 3, 2, 1]
  592. </pre>
  593. <p id="compact">
  594. <b class="header">compact</b><code>_.compact(array)</code>
  595. <br />
  596. Returns a copy of the <b>array</b> with all falsy values removed.
  597. In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
  598. <i>undefined</i> and <i>NaN</i> are all falsy.
  599. </p>
  600. <pre>
  601. _.compact([0, 1, false, 2, '', 3]);
  602. =&gt; [1, 2, 3]
  603. </pre>
  604. <p id="flatten">
  605. <b class="header">flatten</b><code>_.flatten(array)</code>
  606. <br />
  607. Flattens a nested <b>array</b> (the nesting can be to any depth).
  608. </p>
  609. <pre>
  610. _.flatten([1, [2], [3, [[[4]]]]]);
  611. =&gt; [1, 2, 3, 4];
  612. </pre>
  613. <p id="without">
  614. <b class="header">without</b><code>_.without(array, [*values])</code>
  615. <br />
  616. Returns a copy of the <b>array</b> with all instances of the <b>values</b>
  617. removed. <i>===</i> is used for the equality test.
  618. </p>
  619. <pre>
  620. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  621. =&gt; [2, 3, 4]
  622. </pre>
  623. <p id="union">
  624. <b class="header">union</b><code>_.union(*arrays)</code>
  625. <br />
  626. Computes the union of the passed-in <b>arrays</b>: the list of unique items,
  627. in order, that are present in one or more of the <b>arrays</b>.
  628. </p>
  629. <pre>
  630. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  631. =&gt; [1, 2, 3, 101, 10]
  632. </pre>
  633. <p id="intersection">
  634. <b class="header">intersection</b><code>_.intersection(*arrays)</code>
  635. <br />
  636. Computes the list of values that are the intersection of all the <b>arrays</b>.
  637. Each value in the result is present in each of the <b>arrays</b>.
  638. </p>
  639. <pre>
  640. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  641. =&gt; [1, 2]
  642. </pre>
  643. <p id="difference">
  644. <b class="header">difference</b><code>_.difference(array, *others)</code>
  645. <br />
  646. Similar to <b>without</b>, but returns the values from <b>array</b> that
  647. are not present in the <b>other</b> arrays.
  648. </p>
  649. <pre>
  650. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  651. =&gt; [1, 3, 4]
  652. </pre>
  653. <p id="uniq">
  654. <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
  655. <span class="alias">Alias: <b>unique</b></span>
  656. <br />
  657. Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
  658. object equality. If you know in advance that the <b>array</b> is sorted,
  659. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  660. If you want to compute unique items based on a transformation, pass an
  661. <b>iterator</b> function.
  662. </p>
  663. <pre>
  664. _.uniq([1, 2, 1, 3, 1, 4]);
  665. =&gt; [1, 2, 3, 4]
  666. </pre>
  667. <p id="zip">
  668. <b class="header">zip</b><code>_.zip(*arrays)</code>
  669. <br />
  670. Merges together the values of each of the <b>arrays</b> with the
  671. values at the corresponding position. Useful when you have separate
  672. data sources that are coordinated through matching array indexes.
  673. If you're working with a matrix of nested arrays, <b>zip.apply</b>
  674. can transpose the matrix in a similar fashion.
  675. </p>
  676. <pre>
  677. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  678. =&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
  679. </pre>
  680. <p id="indexOf">
  681. <b class="header">indexOf</b><code>_.indexOf(array, value, [isSorted])</code>
  682. <br />
  683. Returns the index at which <b>value</b> can be found in the <b>array</b>,
  684. or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
  685. <b>indexOf</b> function unless it's missing. If you're working with a
  686. large array, and you know that the array is already sorted, pass <tt>true</tt>
  687. for <b>isSorted</b> to use a faster binary search.
  688. </p>
  689. <pre>
  690. _.indexOf([1, 2, 3], 2);
  691. =&gt; 1
  692. </pre>
  693. <p id="lastIndexOf">
  694. <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
  695. <br />
  696. Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
  697. or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
  698. function if possible.
  699. </p>
  700. <pre>
  701. _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  702. =&gt; 4
  703. </pre>
  704. <p id="range">
  705. <b class="header">range</b><code>_.range([start], stop, [step])</code>
  706. <br />
  707. A function to create flexibly-numbered lists of integers, handy for
  708. <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
  709. to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
  710. from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
  711. exclusive.
  712. </p>
  713. <pre>
  714. _.range(10);
  715. =&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  716. _.range(1, 11);
  717. =&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  718. _.range(0, 30, 5);
  719. =&gt; [0, 5, 10, 15, 20, 25]
  720. _.range(0, -10, -1);
  721. =&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  722. _.range(0);
  723. =&gt; []
  724. </pre>
  725. <h2 id="functions">Function (uh, ahem) Functions</h2>
  726. <p id="bind">
  727. <b class="header">bind</b><code>_.bind(function, object, [*arguments])</code>
  728. <br />
  729. Bind a <b>function</b> to an <b>object</b>, meaning that whenever
  730. the function is called, the value of <i>this</i> will be the <b>object</b>.
  731. Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
  732. also known as <b>partial application</b>.
  733. </p>
  734. <pre>
  735. var func = function(greeting){ return greeting + ': ' + this.name };
  736. func = _.bind(func, {name : 'moe'}, 'hi');
  737. func();
  738. =&gt; 'hi: moe'
  739. </pre>
  740. <p id="bindAll">
  741. <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
  742. <br />
  743. Binds a number of methods on the <b>object</b>, specified by
  744. <b>methodNames</b>, to be run in the context of that object whenever they
  745. are invoked. Very handy for binding functions that are going to be used
  746. as event handlers, which would otherwise be invoked with a fairly useless
  747. <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
  748. function properties will be bound to it.
  749. </p>
  750. <pre>
  751. var buttonView = {
  752. label : 'underscore',
  753. onClick : function(){ alert('clicked: ' + this.label); },
  754. onHover : function(){ console.log('hovering: ' + this.label); }
  755. };
  756. _.bindAll(buttonView);
  757. jQuery('#underscore_button').bind('click', buttonView.onClick);
  758. =&gt; When the button is clicked, this.label will have the correct value...
  759. </pre>
  760. <p id="memoize">
  761. <b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
  762. <br />
  763. Memoizes a given <b>function</b> by caching the computed result. Useful
  764. for speeding up slow-running computations. If passed an optional
  765. <b>hashFunction</b>, it will be used to compute the hash key for storing
  766. the result, based on the arguments to the original function. The default
  767. <b>hashFunction</b> just uses the first argument to the memoized function
  768. as the key.
  769. </p>
  770. <pre>
  771. var fibonacci = _.memoize(function(n) {
  772. return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  773. });
  774. </pre>
  775. <p id="delay">
  776. <b class="header">delay</b><code>_.delay(function, wait, [*arguments])</code>
  777. <br />
  778. Much like <b>setTimeout</b>, invokes <b>function</b> after <b>wait</b>
  779. milliseconds. If you pass the optional <b>arguments</b>, they will be
  780. forwarded on to the <b>function</b> when it is invoked.
  781. </p>
  782. <pre>
  783. var log = _.bind(console.log, console);
  784. _.delay(log, 1000, 'logged later');
  785. =&gt; 'logged later' // Appears after one second.
  786. </pre>
  787. <p id="defer">
  788. <b class="header">defer</b><code>_.defer(function)</code>
  789. <br />
  790. Defers invoking the <b>function</b> until the current call stack has cleared,
  791. similar to using <b>setTimeout</b> with a delay of 0. Useful for performing
  792. expensive computations or HTML rendering in chunks without blocking the UI thread
  793. from updating.
  794. </p>
  795. <pre>
  796. _.defer(function(){ alert('deferred'); });
  797. // Returns from the function before the alert runs.
  798. </pre>
  799. <p id="throttle">
  800. <b class="header">throttle</b><code>_.throttle(function, wait)</code>
  801. <br />
  802. Creates and returns a new, throttled version of the passed function,
  803. that, when invoked repeatedly, will only actually call the original function
  804. at most once per every <b>wait</b>
  805. milliseconds. Useful for rate-limiting events that occur faster than you
  806. can keep up with.
  807. </p>
  808. <pre>
  809. var throttled = _.throttle(updatePosition, 100);
  810. $(window).scroll(throttled);
  811. </pre>
  812. <p id="debounce">
  813. <b class="header">debounce</b><code>_.debounce(function, wait)</code>
  814. <br />
  815. Creates and returns a new debounced version of the passed function that
  816. will postpone its execution until after
  817. <b>wait</b> milliseconds have elapsed since the last time it
  818. was invoked. Useful for implementing behavior that should only happen
  819. <i>after</i> the input has stopped arriving. For example: rendering a
  820. preview of a Markdown comment, recalculating a layout after the window
  821. has stopped being resized, and so on.
  822. </p>
  823. <pre>
  824. var lazyLayout = _.debounce(calculateLayout, 300);
  825. $(window).resize(lazyLayout);
  826. </pre>
  827. <p id="once">
  828. <b class="header">once</b><code>_.once(function)</code>
  829. <br />
  830. Creates a version of the function that can only be called one time.
  831. Repeated calls to the modified function will have no effect, returning
  832. the value from the original call. Useful for initialization functions,
  833. instead of having to set a boolean flag and then check it later.
  834. </p>
  835. <pre>
  836. var initialize = _.once(createApplication);
  837. initialize();
  838. initialize();
  839. // Application is only created once.
  840. </pre>
  841. <p id="after">
  842. <b class="header">after</b><code>_.after(count, function)</code>
  843. <br />
  844. Creates a version of the function that will only be run after first
  845. being called <b>count</b> times. Useful for grouping asynchronous responses,
  846. where you want to be sure that all the async calls have finished, before
  847. proceeding.
  848. </p>
  849. <pre>
  850. var renderNotes = _.after(notes.length, render);
  851. _.each(notes, function(note) {
  852. note.asyncSave({success: renderNotes});
  853. });
  854. // renderNotes is run once, after all notes have saved.
  855. </pre>
  856. <p id="wrap">
  857. <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
  858. <br />
  859. Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
  860. passing it as the first argument. This allows the <b>wrapper</b> to
  861. execute code before and after the <b>function</b> runs, adjust the arguments,
  862. and execute it conditionally.
  863. </p>
  864. <pre>
  865. var hello = function(name) { return "hello: " + name; };
  866. hello = _.wrap(hello, function(func) {
  867. return "before, " + func("moe") + ", after";
  868. });
  869. hello();
  870. =&gt; 'before, hello: moe, after'
  871. </pre>
  872. <p id="compose">
  873. <b class="header">compose</b><code>_.compose(*functions)</code>
  874. <br />
  875. Returns the composition of a list of <b>functions</b>, where each function
  876. consumes the return value of the function that follows. In math terms,
  877. composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
  878. <i>f(g(h()))</i>.
  879. </p>
  880. <pre>
  881. var greet = function(name){ return "hi: " + name; };
  882. var exclaim = function(statement){ return statement + "!"; };
  883. var welcome = _.compose(exclaim, greet);
  884. welcome('moe');
  885. =&gt; 'hi: moe!'
  886. </pre>
  887. <h2 id="objects">Object Functions</h2>
  888. <p id="keys">
  889. <b class="header">keys</b><code>_.keys(object)</code>
  890. <br />
  891. Retrieve all the names of the <b>object</b>'s properties.
  892. </p>
  893. <pre>
  894. _.keys({one : 1, two : 2, three : 3});
  895. =&gt; ["one", "two", "three"]
  896. </pre>
  897. <p id="values">
  898. <b class="header">values</b><code>_.values(object)</code>
  899. <br />
  900. Return all of the values of the <b>object</b>'s properties.
  901. </p>
  902. <pre>
  903. _.values({one : 1, two : 2, three : 3});
  904. =&gt; [1, 2, 3]
  905. </pre>
  906. <p id="functions">
  907. <b class="header">functions</b><code>_.functions(object)</code>
  908. <span class="alias">Alias: <b>methods</b></span>
  909. <br />
  910. Returns a sorted list of the names of every method in an object &mdash;
  911. that is to say, the name of every function property of the object.
  912. </p>
  913. <pre>
  914. _.functions(_);
  915. =&gt; ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
  916. </pre>
  917. <p id="extend">
  918. <b class="header">extend</b><code>_.extend(destination, *sources)</code>
  919. <br />
  920. Copy all of the properties in the <b>source</b> objects over to the
  921. <b>destination</b> object. It's in-order, so the last source will override
  922. properties of the same name in previous arguments.
  923. </p>
  924. <pre>
  925. _.extend({name : 'moe'}, {age : 50});
  926. =&gt; {name : 'moe', age : 50}
  927. </pre>
  928. <p id="defaults">
  929. <b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
  930. <br />
  931. Fill in missing properties in <b>object</b> with default values from the
  932. <b>defaults</b> objects. As soon as the property is filled, further defaults
  933. will have no effect.
  934. </p>
  935. <pre>
  936. var iceCream = {flavor : "chocolate"};
  937. _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
  938. =&gt; {flavor : "chocolate", sprinkles : "lots"}
  939. </pre>
  940. <p id="clone">
  941. <b class="header">clone</b><code>_.clone(object)</code>
  942. <br />
  943. Create a shallow-copied clone of the <b>object</b>. Any nested objects
  944. or arrays will be copied by reference, not duplicated.
  945. </p>
  946. <pre>
  947. _.clone({name : 'moe'});
  948. =&gt; {name : 'moe'};
  949. </pre>
  950. <p id="tap">
  951. <b class="header">tap</b><code>_.tap(object, interceptor)</code>
  952. <br />
  953. Invokes <b>interceptor</b> with the <b>object</b>, and then returns <b>object</b>.
  954. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
  955. </p>
  956. <pre>
  957. _.chain([1,2,3,200])
  958. .filter(function(num) { return num % 2 == 0; })
  959. .tap(console.log)
  960. .map(function(num) { return num * num })
  961. .value();
  962. =&gt; [2, 200]
  963. =&gt; [4, 40000]
  964. </pre>
  965. <p id="isEqual">
  966. <b class="header">isEqual</b><code>_.isEqual(object, other)</code>
  967. <br />
  968. Performs an optimized deep comparison between the two objects, to determine
  969. if they should be considered equal.
  970. </p>
  971. <pre>
  972. var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
  973. var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
  974. moe == clone;
  975. =&gt; false
  976. _.isEqual(moe, clone);
  977. =&gt; true
  978. </pre>
  979. <p id="isEmpty">
  980. <b class="header">isEmpty</b><code>_.isEmpty(object)</code>
  981. <br />
  982. Returns <i>true</i> if <b>object</b> contains no values.
  983. </p>
  984. <pre>
  985. _.isEmpty([1, 2, 3]);
  986. =&gt; false
  987. _.isEmpty({});
  988. =&gt; true
  989. </pre>
  990. <p id="isElement">
  991. <b class="header">isElement</b><code>_.isElement(object)</code>
  992. <br />
  993. Returns <i>true</i> if <b>object</b> is a DOM element.
  994. </p>
  995. <pre>
  996. _.isElement(jQuery('body')[0]);
  997. =&gt; true
  998. </pre>
  999. <p id="isArray">
  1000. <b class="header">isArray</b><code>_.isArray(object)</code>
  1001. <br />
  1002. Returns <i>true</i> if <b>object</b> is an Array.
  1003. </p>
  1004. <pre>
  1005. (function(){ return _.isArray(arguments); })();
  1006. =&gt; false
  1007. _.isArray([1,2,3]);
  1008. =&gt; true
  1009. </pre>
  1010. <p id="isArguments">
  1011. <b class="header">isArguments</b><code>_.isArguments(object)</code>
  1012. <br />
  1013. Returns <i>true</i> if <b>object</b> is an Arguments object.
  1014. </p>
  1015. <pre>
  1016. (function(){ return _.isArguments(arguments); })(1, 2, 3);
  1017. =&gt; true
  1018. _.isArguments([1,2,3]);
  1019. =&gt; false
  1020. </pre>
  1021. <p id="isFunction">
  1022. <b class="header">isFunction</b><code>_.isFunction(object)</code>
  1023. <br />
  1024. Returns <i>true</i> if <b>object</b> is a Function.
  1025. </p>
  1026. <pre>
  1027. _.isFunction(alert);
  1028. =&gt; true
  1029. </pre>
  1030. <p id="isString">
  1031. <b class="header">isString</b><code>_.isString(object)</code>
  1032. <br />
  1033. Returns <i>true</i> if <b>object</b> is a String.
  1034. </p>
  1035. <pre>
  1036. _.isString("moe");
  1037. =&gt; true
  1038. </pre>
  1039. <p id="isNumber">
  1040. <b class="header">isNumber</b><code>_.isNumber(object)</code>
  1041. <br />
  1042. Returns <i>true</i> if <b>object</b> is a Number (including <tt>NaN</tt>).
  1043. </p>
  1044. <pre>
  1045. _.isNumber(8.4 * 5);
  1046. =&gt; true
  1047. </pre>
  1048. <p id="isBoolean">
  1049. <b class="header">isBoolean</b><code>_.isBoolean(object)</code>
  1050. <br />
  1051. Returns <i>true</i> if <b>object</b> is either <i>true</i> or <i>false</i>.
  1052. </p>
  1053. <pre>
  1054. _.isBoolean(null);
  1055. =&gt; false
  1056. </pre>
  1057. <p id="isDate">
  1058. <b class="header">isDate</b><code>_.isDate(object)</code>
  1059. <br />
  1060. Returns <i>true</i> if <b>object</b> is a Date.
  1061. </p>
  1062. <pre>
  1063. _.isDate(new Date());
  1064. =&gt; true
  1065. </pre>
  1066. <p id="isRegExp">
  1067. <b class="header">isRegExp</b><code>_.isRegExp(object)</code>
  1068. <br />
  1069. Returns <i>true</i> if <b>object</b> is a RegExp.
  1070. </p>
  1071. <pre>
  1072. _.isRegExp(/moe/);
  1073. =&gt; true
  1074. </pre>
  1075. <p id="isNaN">
  1076. <b class="header">isNaN</b><code>_.isNaN(object)</code>
  1077. <br />
  1078. Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
  1079. the same as the native <b>isNaN</b> function, which will also return
  1080. true if the variable is <i>undefined</i>.
  1081. </p>
  1082. <pre>
  1083. _.isNaN(NaN);
  1084. =&gt; true
  1085. isNaN(undefined);
  1086. =&gt; true
  1087. _.isNaN(undefined);
  1088. =&gt; false
  1089. </pre>
  1090. <p id="isNull">
  1091. <b class="header">isNull</b><code>_.isNull(object)</code>
  1092. <br />
  1093. Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
  1094. </p>
  1095. <pre>
  1096. _.isNull(null);
  1097. =&gt; true
  1098. _.isNull(undefined);
  1099. =&gt; false
  1100. </pre>
  1101. <p id="isUndefined">
  1102. <b class="header">isUndefined</b><code>_.isUndefined(variable)</code>
  1103. <br />
  1104. Returns <i>true</i> if <b>variable</b> is <i>undefined</i>.
  1105. </p>
  1106. <pre>
  1107. _.isUndefined(window.missingVariable);
  1108. =&gt; true
  1109. </pre>
  1110. <h2 id="utility">Utility Functions</h2>
  1111. <p id="noConflict">
  1112. <b class="header">noConflict</b><code>_.noConflict()</code>
  1113. <br />
  1114. Give control of the "_" variable back to its previous owner. Returns
  1115. a reference to the <b>Underscore</b> object.
  1116. </p>
  1117. <pre>
  1118. var underscore = _.noConflict();</pre>
  1119. <p id="identity">
  1120. <b class="header">identity</b><code>_.identity(value)</code>
  1121. <br />
  1122. Returns the same value that is used as the argument. In math:
  1123. <tt>f(x) = x</tt><br />
  1124. This function looks useless, but is used throughout Underscore as
  1125. a default iterator.
  1126. </p>
  1127. <pre>
  1128. var moe = {name : 'moe'};
  1129. moe === _.identity(moe);
  1130. =&gt; true</pre>
  1131. <p id="times">
  1132. <b class="header">times</b><code>_.times(n, iterator)</code>
  1133. <br />
  1134. Invokes the given iterator function <b>n</b> times.
  1135. </p>
  1136. <pre>
  1137. _(3).times(function(){ genie.grantWish(); });</pre>
  1138. <p id="mixin">
  1139. <b class="header">mixin</b><code>_.mixin(object)</code>
  1140. <br />
  1141. Allows you to extend Underscore with your own utility functions. Pass
  1142. a hash of <tt>{name: function}</tt> definitions to have your functions
  1143. added to the Underscore object, as well as the OOP wrapper.
  1144. </p>
  1145. <pre>
  1146. _.mixin({
  1147. capitalize : function(string) {
  1148. return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  1149. }
  1150. });
  1151. _("fabio").capitalize();
  1152. =&gt; "Fabio"
  1153. </pre>
  1154. <p id="uniqueId">
  1155. <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
  1156. <br />
  1157. Generate a globally-unique id for client-side models or DOM elements
  1158. that need one. If <b>prefix</b> is passed, the id will be appended to it.
  1159. </p>
  1160. <pre>
  1161. _.uniqueId('contact_');
  1162. =&gt; 'contact_104'</pre>
  1163. <p id="escape">
  1164. <b class="header">escape</b><code>_.escape(string)</code>
  1165. <br />
  1166. Escapes a string for insertion into HTML, replacing
  1167. <tt>&amp;</tt>, <tt>&lt;</tt>, <tt>&gt;</tt>, <tt>&quot;</tt>, <tt>&#x27;</tt>, and <tt>&#x2F;</tt> characters.
  1168. </p>
  1169. <pre>
  1170. _.escape('Curly, Larry &amp; Moe');
  1171. =&gt; "Curly, Larry &amp;amp; Moe"</pre>
  1172. <p id="template">
  1173. <b class="header">template</b><code>_.template(templateString, [context])</code>
  1174. <br />
  1175. Compiles JavaScript templates into functions that can be evaluated
  1176. for rendering. Useful for rendering complicated bits of HTML from JSON
  1177. data sources. Template functions can both interpolate variables, using<br />
  1178. <tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
  1179. <tt>&lt;% &hellip; %&gt;</tt>. If you wish to interpolate a value, and have
  1180. it be HTML-escaped, use <tt>&lt;%- &hellip; %&gt;</tt> When you evaluate a template function, pass in a
  1181. <b>context</b> object that has properties corresponding to the template's free
  1182. variables. If you're writing a one-off, you can pass the <b>context</b>
  1183. object as the second parameter to <b>template</b> in order to render
  1184. immediately instead of returning a template function.
  1185. </p>
  1186. <pre>
  1187. var compiled = _.template("hello: &lt;%= name %&gt;");
  1188. compiled({name : 'moe'});
  1189. =&gt; "hello: moe"
  1190. var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
  1191. _.template(list, {people : ['moe', 'curly', 'larry']});
  1192. =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
  1193. var template = _.template("&lt;b&gt;&lt;%- value %&gt;&lt;/b&gt;");
  1194. template({value : '&lt;script&gt;'});
  1195. =&gt; "&lt;b&gt;&amp;lt;script&amp;gt;&lt;/b&gt;"</pre>
  1196. <p>
  1197. You can also use <tt>print</tt> from within JavaScript code. This is
  1198. sometimes more convenient than using <tt>&lt;%= ... %&gt;</tt>.
  1199. </p>
  1200. <pre>
  1201. var compiled = _.template("&lt;% print('Hello ' + epithet); %&gt;");
  1202. compiled({epithet: "stooge"});
  1203. =&gt; "Hello stooge."</pre>
  1204. <p>
  1205. If ERB-style delimiters aren't your cup of tea, you can change Underscore's
  1206. template settings to use different symbols to set off interpolated code.
  1207. Define an <b>interpolate</b> regex to match expressions that should be
  1208. interpolated verbatim, an <b>escape</b> regex to match expressions that should
  1209. be inserted after being HTML escaped, and an <b>evaluate</b> regex to match
  1210. expressions that should be evaluated without insertion into the resulting
  1211. string. You may define or omit any combination of the three.
  1212. For example, to perform
  1213. <a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
  1214. style templating:
  1215. </p>
  1216. <pre>
  1217. _.templateSettings = {
  1218. interpolate : /\{\{(.+?)\}\}/g
  1219. };
  1220. var template = _.template("Hello {{ name }}!");
  1221. template({name : "Mustache"});
  1222. =&gt; "Hello Mustache!"</pre>
  1223. <h2 id="chaining">Chaining</h2>
  1224. <p>
  1225. You can use Underscore in either an object-oriented or a functional style,
  1226. depending on your preference. The following two lines of code are
  1227. identical ways to double a list of numbers.
  1228. </p>
  1229. <pre>
  1230. _.map([1, 2, 3], function(n){ return n * 2; });
  1231. _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
  1232. <p>
  1233. Using the object-oriented style allows you to chain together methods. Calling
  1234. <tt>chain</tt> on a wrapped object will cause all future method calls to
  1235. return wrapped objects as well. When you've finished the computation,
  1236. use <tt>value</tt> to retrieve the final value. Here's an example of chaining
  1237. together a <b>map/flatten/reduce</b>, in order to get the word count of
  1238. every word in a song.
  1239. </p>
  1240. <pre>
  1241. var lyrics = [
  1242. {line : 1, words : "I'm a lumberjack and I'm okay"},
  1243. {line : 2, words : "I sleep all night and I work all day"},
  1244. {line : 3, words : "He's a lumberjack and he's okay"},
  1245. {line : 4, words : "He sleeps all night and he works all day"}
  1246. ];
  1247. _.chain(lyrics)
  1248. .map(function(line) { return line.words.split(' '); })
  1249. .flatten()
  1250. .reduce(function(counts, word) {
  1251. counts[word] = (counts[word] || 0) + 1;
  1252. return counts;
  1253. }, {}).value();
  1254. =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
  1255. <p>
  1256. In addition, the
  1257. <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/prototype">Array prototype's methods</a>
  1258. are proxied through the chained Underscore object, so you can slip a
  1259. <tt>reverse</tt> or a <tt>push</tt> into your chain, and continue to
  1260. modify the array.
  1261. </p>
  1262. <p id="chain">
  1263. <b class="header">chain</b><code>_.chain(obj)</code>
  1264. <br />
  1265. Returns a wrapped object. Calling methods on this object will continue
  1266. to return wrapped objects until <tt>value</tt> is used.
  1267. </p>
  1268. <pre>
  1269. var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
  1270. var youngest = _.chain(stooges)
  1271. .sortBy(function(stooge){ return stooge.age; })
  1272. .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
  1273. .first()
  1274. .value();
  1275. =&gt; "moe is 21"
  1276. </pre>
  1277. <p id="value">
  1278. <b class="header">value</b><code>_(obj).value()</code>
  1279. <br />
  1280. Extracts the value of a wrapped object.
  1281. </p>
  1282. <pre>
  1283. _([1, 2, 3]).value();
  1284. =&gt; [1, 2, 3]
  1285. </pre>
  1286. <h2 id="links">Links &amp; Suggested Reading</h2>
  1287. <p>
  1288. <a href="http://mirven.github.com/underscore.lua/">Underscore.lua</a>,
  1289. a Lua port of the functions that are applicable in both languages.
  1290. Includes OOP-wrapping and chaining.
  1291. The <a href="http://github.com/mirven/underscore.lua">source</a> is
  1292. available on GitHub.
  1293. </p>
  1294. <p>
  1295. <a href="http://brianhaveri.github.com/Underscore.php/">Underscore.php</a>,
  1296. a PHP port of the functions that are applicable in both languages.
  1297. Includes OOP-wrapping and chaining.
  1298. The <a href="http://github.com/brianhaveri/Underscore.php">source</a> is
  1299. available on GitHub.
  1300. </p>
  1301. <p>
  1302. <a href="http://vti.github.com/underscore-perl/">Underscore-perl</a>,
  1303. a Perl port of many of the Underscore.js functions,
  1304. aimed at on Perl hashes and arrays, also
  1305. <a href="https://github.com/vti/underscore-perl/">available on GitHub</a>.
  1306. </p>
  1307. <p>
  1308. <a href="https://github.com/edtsech/underscore.string">Underscore.string</a>,
  1309. an Underscore extension that adds functions for string-manipulation:
  1310. <tt>trim</tt>, <tt>startsWith</tt>, <tt>contains</tt>, <tt>capitalize</tt>,
  1311. <tt>reverse</tt>, <tt>sprintf</tt>, and more.
  1312. </p>
  1313. <p>
  1314. Ruby's <a href="http://ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> module.
  1315. </p>
  1316. <p>
  1317. <a href="http://www.prototypejs.org/">Prototype.js</a>, which provides
  1318. JavaScript with collection functions in the manner closest to Ruby's Enumerable.
  1319. </p>
  1320. <p>
  1321. Oliver Steele's
  1322. <a href="http://osteele.com/sources/javascript/functional/">Functional JavaScript</a>,
  1323. which includes comprehensive higher-order function support as well as string lambdas.
  1324. </p>
  1325. <p>
  1326. Michael Aufreiter's <a href="http://substance.io/#michael/data-js">Data.js</a>,
  1327. a data manipulation + persistence library for JavaScript.
  1328. </p>
  1329. <p>
  1330. Python's <a href="http://docs.python.org/library/itertools.html">itertools</a>.
  1331. </p>
  1332. <h2 id="changelog">Change Log</h2>
  1333. <p>
  1334. <b class="header">1.3.0</b> &mdash; <small><i

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