PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/utils/server/node_modules/bogart/node_modules/underscore/index.html

http://github.com/ogx/Calculation2D
HTML | 1832 lines | 1668 code | 164 blank | 0 comment | 0 complexity | 51984563302211f0ab61b5f68e63d17b MD5 | raw file
Possible License(s): Apache-2.0

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

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