PageRenderTime 69ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/grunt-contrib-jshint/node_modules/jshint/tests/stable/regression/libs/lodash.js

https://bitbucket.org/biojazzard/gantry-eboracast
JavaScript | 4454 lines | 1801 code | 291 blank | 2362 comment | 475 complexity | 913b539b8245e34f553c81368197c807 MD5 | raw file
Possible License(s): MIT, JSON, BSD-2-Clause, Unlicense, GPL-2.0, WTFPL, LGPL-3.0, Apache-2.0, 0BSD, BSD-3-Clause, CC-BY-SA-3.0

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

  1. /*!
  2. * Lo-Dash v0.6.1 <http://lodash.com>
  3. * Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
  4. * Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
  5. * <http://documentcloud.github.com/underscore>
  6. * Available under MIT license <http://lodash.com/license>
  7. */
  8. ;(function(window, undefined) {
  9. 'use strict';
  10. /**
  11. * Used to cache the last `_.templateSettings.evaluate` delimiter to avoid
  12. * unnecessarily assigning `reEvaluateDelimiter` a new generated regexp.
  13. * Assigned in `_.template`.
  14. */
  15. var lastEvaluateDelimiter;
  16. /**
  17. * Used to cache the last template `options.variable` to avoid unnecessarily
  18. * assigning `reDoubleVariable` a new generated regexp. Assigned in `_.template`.
  19. */
  20. var lastVariable;
  21. /**
  22. * Used to match potentially incorrect data object references, like `obj.obj`,
  23. * in compiled templates. Assigned in `_.template`.
  24. */
  25. var reDoubleVariable;
  26. /**
  27. * Used to match "evaluate" delimiters, including internal delimiters,
  28. * in template text. Assigned in `_.template`.
  29. */
  30. var reEvaluateDelimiter;
  31. /** Detect free variable `exports` */
  32. var freeExports = typeof exports == 'object' && exports &&
  33. (typeof global == 'object' && global && global == global.global && (window = global), exports);
  34. /** Native prototype shortcuts */
  35. var ArrayProto = Array.prototype,
  36. BoolProto = Boolean.prototype,
  37. ObjectProto = Object.prototype,
  38. NumberProto = Number.prototype,
  39. StringProto = String.prototype;
  40. /** Used to generate unique IDs */
  41. var idCounter = 0;
  42. /** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
  43. var largeArraySize = 30;
  44. /** Used to restore the original `_` reference in `noConflict` */
  45. var oldDash = window._;
  46. /** Used to detect delimiter values that should be processed by `tokenizeEvaluate` */
  47. var reComplexDelimiter = /[-+=!~*%&^<>|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/;
  48. /** Used to match HTML entities */
  49. var reEscapedHtml = /&(?:amp|lt|gt|quot|#x27);/g;
  50. /** Used to match empty string literals in compiled template source */
  51. var reEmptyStringLeading = /\b__p \+= '';/g,
  52. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  53. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  54. /** Used to match regexp flags from their coerced string values */
  55. var reFlags = /\w*$/;
  56. /** Used to insert the data object variable into compiled template source */
  57. var reInsertVariable = /(?:__e|__t = )\(\s*(?![\d\s"']|this\.)/g;
  58. /** Used to detect if a method is native */
  59. var reNative = RegExp('^' +
  60. (ObjectProto.valueOf + '')
  61. .replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
  62. .replace(/valueOf|for [^\]]+/g, '.+?') + '$'
  63. );
  64. /** Used to match internally used tokens in template text */
  65. var reToken = /__token(\d+)__/g;
  66. /** Used to match HTML characters */
  67. var reUnescapedHtml = /[&<>"']/g;
  68. /** Used to match unescaped characters in compiled string literals */
  69. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  70. /** Used to fix the JScript [[DontEnum]] bug */
  71. var shadowed = [
  72. 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
  73. 'toLocaleString', 'toString', 'valueOf'
  74. ];
  75. /** Used to make template sourceURLs easier to identify */
  76. var templateCounter = 0;
  77. /** Used to replace template delimiters */
  78. var tokenHead = '__token',
  79. tokenFoot = '__';
  80. /** Used to store tokenized template text snippets */
  81. var tokenized = [];
  82. /** Native method shortcuts */
  83. var concat = ArrayProto.concat,
  84. hasOwnProperty = ObjectProto.hasOwnProperty,
  85. push = ArrayProto.push,
  86. propertyIsEnumerable = ObjectProto.propertyIsEnumerable,
  87. slice = ArrayProto.slice,
  88. toString = ObjectProto.toString;
  89. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  90. var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
  91. nativeFloor = Math.floor,
  92. nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
  93. nativeIsFinite = window.isFinite,
  94. nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
  95. nativeMax = Math.max,
  96. nativeMin = Math.min,
  97. nativeRandom = Math.random;
  98. /** `Object#toString` result shortcuts */
  99. var argsClass = '[object Arguments]',
  100. arrayClass = '[object Array]',
  101. boolClass = '[object Boolean]',
  102. dateClass = '[object Date]',
  103. funcClass = '[object Function]',
  104. numberClass = '[object Number]',
  105. objectClass = '[object Object]',
  106. regexpClass = '[object RegExp]',
  107. stringClass = '[object String]';
  108. /** Timer shortcuts */
  109. var clearTimeout = window.clearTimeout,
  110. setTimeout = window.setTimeout;
  111. /**
  112. * Detect the JScript [[DontEnum]] bug:
  113. *
  114. * In IE < 9 an objects own properties, shadowing non-enumerable ones, are
  115. * made non-enumerable as well.
  116. */
  117. var hasDontEnumBug;
  118. /**
  119. * Detect if `Array#shift` and `Array#splice` augment array-like objects
  120. * incorrectly:
  121. *
  122. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  123. * and `splice()` functions that fail to remove the last element, `value[0]`,
  124. * of array-like objects even though the `length` property is set to `0`.
  125. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  126. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  127. */
  128. var hasObjectSpliceBug;
  129. /** Detect if own properties are iterated after inherited properties (IE < 9) */
  130. var iteratesOwnLast;
  131. /** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
  132. var noArgsEnum = true;
  133. (function() {
  134. var object = { '0': 1, 'length': 1 },
  135. props = [];
  136. function ctor() { this.x = 1; }
  137. ctor.prototype = { 'valueOf': 1, 'y': 1 };
  138. for (var prop in new ctor) { props.push(prop); }
  139. for (prop in arguments) { noArgsEnum = !prop; }
  140. hasDontEnumBug = (props + '').length < 4;
  141. iteratesOwnLast = props[0] != 'x';
  142. hasObjectSpliceBug = (props.splice.call(object, 0, 1), object[0]);
  143. }(1));
  144. /** Detect if an `arguments` object's [[Class]] is unresolvable (Firefox < 4, IE < 9) */
  145. var noArgsClass = !isArguments(arguments);
  146. /** Detect if `Array#slice` cannot be used to convert strings to arrays (Opera < 10.52) */
  147. var noArraySliceOnStrings = slice.call('x')[0] != 'x';
  148. /**
  149. * Detect lack of support for accessing string characters by index:
  150. *
  151. * IE < 8 can't access characters by index and IE 8 can only access
  152. * characters by index on string literals.
  153. */
  154. var noCharByIndex = ('x'[0] + Object('x')[0]) != 'xx';
  155. /**
  156. * Detect if a node's [[Class]] is unresolvable (IE < 9)
  157. * and that the JS engine won't error when attempting to coerce an object to
  158. * a string without a `toString` property value of `typeof` "function".
  159. */
  160. try {
  161. var noNodeClass = ({ 'toString': 0 } + '', toString.call(window.document || 0) == objectClass);
  162. } catch(e) { }
  163. /* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
  164. var isBindFast = nativeBind && /\n|Opera/.test(nativeBind + toString.call(window.opera));
  165. /* Detect if `Object.keys` exists and is inferred to be fast (IE, Opera, V8) */
  166. var isKeysFast = nativeKeys && /^.+$|true/.test(nativeKeys + !!window.attachEvent);
  167. /* Detect if strict mode, "use strict", is inferred to be fast (V8) */
  168. var isStrictFast = !isBindFast;
  169. /**
  170. * Detect if sourceURL syntax is usable without erroring:
  171. *
  172. * The JS engine in Adobe products, like InDesign, will throw a syntax error
  173. * when it encounters a single line comment beginning with the `@` symbol.
  174. *
  175. * The JS engine in Narwhal will generate the function `function anonymous(){//}`
  176. * and throw a syntax error.
  177. *
  178. * Avoid comments beginning `@` symbols in IE because they are part of its
  179. * non-standard conditional compilation support.
  180. * http://msdn.microsoft.com/en-us/library/121hztk3(v=vs.94).aspx
  181. */
  182. try {
  183. var useSourceURL = (Function('//@')(), !window.attachEvent);
  184. } catch(e){ }
  185. /** Used to identify object classifications that are array-like */
  186. var arrayLikeClasses = {};
  187. arrayLikeClasses[boolClass] = arrayLikeClasses[dateClass] = arrayLikeClasses[funcClass] =
  188. arrayLikeClasses[numberClass] = arrayLikeClasses[objectClass] = arrayLikeClasses[regexpClass] = false;
  189. arrayLikeClasses[argsClass] = arrayLikeClasses[arrayClass] = arrayLikeClasses[stringClass] = true;
  190. /** Used to identify object classifications that `_.clone` supports */
  191. var cloneableClasses = {};
  192. cloneableClasses[argsClass] = cloneableClasses[funcClass] = false;
  193. cloneableClasses[arrayClass] = cloneableClasses[boolClass] = cloneableClasses[dateClass] =
  194. cloneableClasses[numberClass] = cloneableClasses[objectClass] = cloneableClasses[regexpClass] =
  195. cloneableClasses[stringClass] = true;
  196. /**
  197. * Used to convert characters to HTML entities:
  198. *
  199. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  200. * don't require escaping in HTML and have no special meaning unless they're part
  201. * of a tag or an unquoted attribute value.
  202. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  203. */
  204. var htmlEscapes = {
  205. '&': '&amp;',
  206. '<': '&lt;',
  207. '>': '&gt;',
  208. '"': '&quot;',
  209. "'": '&#x27;'
  210. };
  211. /** Used to convert HTML entities to characters */
  212. var htmlUnescapes = {
  213. '&amp;': '&',
  214. '&lt;': '<',
  215. '&gt;': '>',
  216. '&quot;': '"',
  217. '&#x27;': "'"
  218. };
  219. /** Used to determine if values are of the language type Object */
  220. var objectTypes = {
  221. 'boolean': false,
  222. 'function': true,
  223. 'object': true,
  224. 'number': false,
  225. 'string': false,
  226. 'undefined': false,
  227. 'unknown': true
  228. };
  229. /** Used to escape characters for inclusion in compiled string literals */
  230. var stringEscapes = {
  231. '\\': '\\',
  232. "'": "'",
  233. '\n': 'n',
  234. '\r': 'r',
  235. '\t': 't',
  236. '\u2028': 'u2028',
  237. '\u2029': 'u2029'
  238. };
  239. /*--------------------------------------------------------------------------*/
  240. /**
  241. * The `lodash` function.
  242. *
  243. * @name _
  244. * @constructor
  245. * @param {Mixed} value The value to wrap in a `LoDash` instance.
  246. * @returns {Object} Returns a `LoDash` instance.
  247. */
  248. function lodash(value) {
  249. // allow invoking `lodash` without the `new` operator
  250. return new LoDash(value);
  251. }
  252. /**
  253. * Creates a `LoDash` instance that wraps a value to allow chaining.
  254. *
  255. * @private
  256. * @constructor
  257. * @param {Mixed} value The value to wrap.
  258. */
  259. function LoDash(value) {
  260. // exit early if already wrapped
  261. if (value && value.__wrapped__) {
  262. return value;
  263. }
  264. this.__wrapped__ = value;
  265. }
  266. /**
  267. * By default, the template delimiters used by Lo-Dash are similar to those in
  268. * embedded Ruby (ERB). Change the following template settings to use alternative
  269. * delimiters.
  270. *
  271. * @static
  272. * @memberOf _
  273. * @type Object
  274. */
  275. lodash.templateSettings = {
  276. /**
  277. * Used to detect `data` property values to be HTML-escaped.
  278. *
  279. * @static
  280. * @memberOf _.templateSettings
  281. * @type RegExp
  282. */
  283. 'escape': /<%-([\s\S]+?)%>/g,
  284. /**
  285. * Used to detect code to be evaluated.
  286. *
  287. * @static
  288. * @memberOf _.templateSettings
  289. * @type RegExp
  290. */
  291. 'evaluate': /<%([\s\S]+?)%>/g,
  292. /**
  293. * Used to detect `data` property values to inject.
  294. *
  295. * @static
  296. * @memberOf _.templateSettings
  297. * @type RegExp
  298. */
  299. 'interpolate': /<%=([\s\S]+?)%>/g,
  300. /**
  301. * Used to reference the data object in the template text.
  302. *
  303. * @static
  304. * @memberOf _.templateSettings
  305. * @type String
  306. */
  307. 'variable': ''
  308. };
  309. /*--------------------------------------------------------------------------*/
  310. /**
  311. * The template used to create iterator functions.
  312. *
  313. * @private
  314. * @param {Obect} data The data object used to populate the text.
  315. * @returns {String} Returns the interpolated text.
  316. */
  317. var iteratorTemplate = template(
  318. // conditional strict mode
  319. '<% if (useStrict) { %>\'use strict\';\n<% } %>' +
  320. // the `iteratee` may be reassigned by the `top` snippet
  321. 'var index, value, iteratee = <%= firstArg %>, ' +
  322. // assign the `result` variable an initial value
  323. 'result<% if (init) { %> = <%= init %><% } %>;\n' +
  324. // add code to exit early or do so if the first argument is falsey
  325. '<%= exit %>;\n' +
  326. // add code after the exit snippet but before the iteration branches
  327. '<%= top %>;\n' +
  328. // the following branch is for iterating arrays and array-like objects
  329. '<% if (arrayBranch) { %>' +
  330. 'var length = iteratee.length; index = -1;' +
  331. ' <% if (objectBranch) { %>\nif (length > -1 && length === length >>> 0) {<% } %>' +
  332. // add support for accessing string characters by index if needed
  333. ' <% if (noCharByIndex) { %>\n' +
  334. ' if (toString.call(iteratee) == stringClass) {\n' +
  335. ' iteratee = iteratee.split(\'\')\n' +
  336. ' }' +
  337. ' <% } %>\n' +
  338. ' <%= arrayBranch.beforeLoop %>;\n' +
  339. ' while (++index < length) {\n' +
  340. ' value = iteratee[index];\n' +
  341. ' <%= arrayBranch.inLoop %>\n' +
  342. ' }' +
  343. ' <% if (objectBranch) { %>\n}<% } %>' +
  344. '<% } %>' +
  345. // the following branch is for iterating an object's own/inherited properties
  346. '<% if (objectBranch) { %>' +
  347. ' <% if (arrayBranch) { %>\nelse {' +
  348. // add support for iterating over `arguments` objects if needed
  349. ' <% } else if (noArgsEnum) { %>\n' +
  350. ' var length = iteratee.length; index = -1;\n' +
  351. ' if (length && isArguments(iteratee)) {\n' +
  352. ' while (++index < length) {\n' +
  353. ' value = iteratee[index += \'\'];\n' +
  354. ' <%= objectBranch.inLoop %>\n' +
  355. ' }\n' +
  356. ' } else {' +
  357. ' <% } %>' +
  358. // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
  359. // (if the prototype or a property on the prototype has been set)
  360. // incorrectly sets a function's `prototype` property [[Enumerable]]
  361. // value to `true`. Because of this Lo-Dash standardizes on skipping
  362. // the the `prototype` property of functions regardless of its
  363. // [[Enumerable]] value.
  364. ' <% if (!hasDontEnumBug) { %>\n' +
  365. ' var skipProto = typeof iteratee == \'function\' && \n' +
  366. ' propertyIsEnumerable.call(iteratee, \'prototype\');\n' +
  367. ' <% } %>' +
  368. // iterate own properties using `Object.keys` if it's fast
  369. ' <% if (isKeysFast && useHas) { %>\n' +
  370. ' var ownIndex = -1,\n' +
  371. ' ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [],\n' +
  372. ' length = ownProps.length;\n\n' +
  373. ' <%= objectBranch.beforeLoop %>;\n' +
  374. ' while (++ownIndex < length) {\n' +
  375. ' index = ownProps[ownIndex];\n' +
  376. ' <% if (!hasDontEnumBug) { %>if (!(skipProto && index == \'prototype\')) {\n <% } %>' +
  377. ' value = iteratee[index];\n' +
  378. ' <%= objectBranch.inLoop %>\n' +
  379. ' <% if (!hasDontEnumBug) { %>}\n<% } %>' +
  380. ' }' +
  381. // else using a for-in loop
  382. ' <% } else { %>\n' +
  383. ' <%= objectBranch.beforeLoop %>;\n' +
  384. ' for (index in iteratee) {' +
  385. ' <% if (!hasDontEnumBug || useHas) { %>\n if (<%' +
  386. ' if (!hasDontEnumBug) { %>!(skipProto && index == \'prototype\')<% }' +
  387. ' if (!hasDontEnumBug && useHas) { %> && <% }' +
  388. ' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' +
  389. ' %>) {' +
  390. ' <% } %>\n' +
  391. ' value = iteratee[index];\n' +
  392. ' <%= objectBranch.inLoop %>;\n' +
  393. ' <% if (!hasDontEnumBug || useHas) { %>}\n<% } %>' +
  394. ' }' +
  395. ' <% } %>' +
  396. // Because IE < 9 can't set the `[[Enumerable]]` attribute of an
  397. // existing property and the `constructor` property of a prototype
  398. // defaults to non-enumerable, Lo-Dash skips the `constructor`
  399. // property when it infers it's iterating over a `prototype` object.
  400. ' <% if (hasDontEnumBug) { %>\n\n' +
  401. ' var ctor = iteratee.constructor;\n' +
  402. ' <% for (var k = 0; k < 7; k++) { %>\n' +
  403. ' index = \'<%= shadowed[k] %>\';\n' +
  404. ' if (<%' +
  405. ' if (shadowed[k] == \'constructor\') {' +
  406. ' %>!(ctor && ctor.prototype === iteratee) && <%' +
  407. ' } %>hasOwnProperty.call(iteratee, index)) {\n' +
  408. ' value = iteratee[index];\n' +
  409. ' <%= objectBranch.inLoop %>\n' +
  410. ' }' +
  411. ' <% } %>' +
  412. ' <% } %>' +
  413. ' <% if (arrayBranch || noArgsEnum) { %>\n}<% } %>' +
  414. '<% } %>\n' +
  415. // add code to the bottom of the iteration function
  416. '<%= bottom %>;\n' +
  417. // finally, return the `result`
  418. 'return result'
  419. );
  420. /**
  421. * Reusable iterator options shared by
  422. * `every`, `filter`, `find`, `forEach`, `forIn`, `forOwn`, `groupBy`, `map`,
  423. * `reject`, `some`, and `sortBy`.
  424. */
  425. var baseIteratorOptions = {
  426. 'args': 'collection, callback, thisArg',
  427. 'init': 'collection',
  428. 'top':
  429. 'if (!callback) {\n' +
  430. ' callback = identity\n' +
  431. '}\n' +
  432. 'else if (thisArg) {\n' +
  433. ' callback = iteratorBind(callback, thisArg)\n' +
  434. '}',
  435. 'inLoop': 'if (callback(value, index, collection) === false) return result'
  436. };
  437. /** Reusable iterator options for `countBy`, `groupBy`, and `sortBy` */
  438. var countByIteratorOptions = {
  439. 'init': '{}',
  440. 'top':
  441. 'var prop;\n' +
  442. 'if (typeof callback != \'function\') {\n' +
  443. ' var valueProp = callback;\n' +
  444. ' callback = function(value) { return value[valueProp] }\n' +
  445. '}\n' +
  446. 'else if (thisArg) {\n' +
  447. ' callback = iteratorBind(callback, thisArg)\n' +
  448. '}',
  449. 'inLoop':
  450. 'prop = callback(value, index, collection);\n' +
  451. '(hasOwnProperty.call(result, prop) ? result[prop]++ : result[prop] = 1)'
  452. };
  453. /** Reusable iterator options for `every` and `some` */
  454. var everyIteratorOptions = {
  455. 'init': 'true',
  456. 'inLoop': 'if (!callback(value, index, collection)) return !result'
  457. };
  458. /** Reusable iterator options for `defaults` and `extend` */
  459. var extendIteratorOptions = {
  460. 'useHas': false,
  461. 'useStrict': false,
  462. 'args': 'object',
  463. 'init': 'object',
  464. 'top':
  465. 'for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {\n' +
  466. ' if (iteratee = arguments[argsIndex]) {',
  467. 'inLoop': 'result[index] = value',
  468. 'bottom': ' }\n}'
  469. };
  470. /** Reusable iterator options for `filter`, `reject`, and `where` */
  471. var filterIteratorOptions = {
  472. 'init': '[]',
  473. 'inLoop': 'callback(value, index, collection) && result.push(value)'
  474. };
  475. /** Reusable iterator options for `find`, `forEach`, `forIn`, and `forOwn` */
  476. var forEachIteratorOptions = {
  477. 'top': 'if (thisArg) callback = iteratorBind(callback, thisArg)'
  478. };
  479. /** Reusable iterator options for `forIn` and `forOwn` */
  480. var forOwnIteratorOptions = {
  481. 'inLoop': {
  482. 'object': baseIteratorOptions.inLoop
  483. }
  484. };
  485. /** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */
  486. var mapIteratorOptions = {
  487. 'init': '',
  488. 'exit': 'if (!collection) return []',
  489. 'beforeLoop': {
  490. 'array': 'result = Array(length)',
  491. 'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
  492. },
  493. 'inLoop': {
  494. 'array': 'result[index] = callback(value, index, collection)',
  495. 'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(callback(value, index, collection))'
  496. }
  497. };
  498. /** Reusable iterator options for `omit` and `pick` */
  499. var omitIteratorOptions = {
  500. 'useHas': false,
  501. 'args': 'object, callback, thisArg',
  502. 'init': '{}',
  503. 'top':
  504. 'var isFunc = typeof callback == \'function\';\n' +
  505. 'if (!isFunc) {\n' +
  506. ' var props = concat.apply(ArrayProto, arguments)\n' +
  507. '} else if (thisArg) {\n' +
  508. ' callback = iteratorBind(callback, thisArg)\n' +
  509. '}',
  510. 'inLoop':
  511. 'if (isFunc\n' +
  512. ' ? !callback(value, index, object)\n' +
  513. ' : indexOf(props, index) < 0\n' +
  514. ') result[index] = value'
  515. };
  516. /*--------------------------------------------------------------------------*/
  517. /**
  518. * Creates a new function optimized for searching large arrays for a given `value`,
  519. * starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
  520. *
  521. * @private
  522. * @param {Array} array The array to search.
  523. * @param {Mixed} value The value to search for.
  524. * @param {Number} [fromIndex=0] The index to start searching from.
  525. * @param {Number} [largeSize=30] The length at which an array is considered large.
  526. * @returns {Boolean} Returns `true` if `value` is found, else `false`.
  527. */
  528. function cachedContains(array, fromIndex, largeSize) {
  529. fromIndex || (fromIndex = 0);
  530. var length = array.length,
  531. isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
  532. cache = isLarge ? {} : array;
  533. if (isLarge) {
  534. // init value cache
  535. var key,
  536. index = fromIndex - 1;
  537. while (++index < length) {
  538. // manually coerce `value` to string because `hasOwnProperty`, in some
  539. // older versions of Firefox, coerces objects incorrectly
  540. key = array[index] + '';
  541. (hasOwnProperty.call(cache, key) ? cache[key] : (cache[key] = [])).push(array[index]);
  542. }
  543. }
  544. return function(value) {
  545. if (isLarge) {
  546. var key = value + '';
  547. return hasOwnProperty.call(cache, key) && indexOf(cache[key], value) > -1;
  548. }
  549. return indexOf(cache, value, fromIndex) > -1;
  550. }
  551. }
  552. /**
  553. * Creates compiled iteration functions. The iteration function will be created
  554. * to iterate over only objects if the first argument of `options.args` is
  555. * "object" or `options.inLoop.array` is falsey.
  556. *
  557. * @private
  558. * @param {Object} [options1, options2, ...] The compile options objects.
  559. *
  560. * useHas - A boolean to specify whether or not to use `hasOwnProperty` checks
  561. * in the object loop.
  562. *
  563. * useStrict - A boolean to specify whether or not to include the ES5
  564. * "use strict" directive.
  565. *
  566. * args - A string of comma separated arguments the iteration function will
  567. * accept.
  568. *
  569. * init - A string to specify the initial value of the `result` variable.
  570. *
  571. * exit - A string of code to use in place of the default exit-early check
  572. * of `if (!arguments[0]) return result`.
  573. *
  574. * top - A string of code to execute after the exit-early check but before
  575. * the iteration branches.
  576. *
  577. * beforeLoop - A string or object containing an "array" or "object" property
  578. * of code to execute before the array or object loops.
  579. *
  580. * inLoop - A string or object containing an "array" or "object" property
  581. * of code to execute in the array or object loops.
  582. *
  583. * bottom - A string of code to execute after the iteration branches but
  584. * before the `result` is returned.
  585. *
  586. * @returns {Function} Returns the compiled function.
  587. */
  588. function createIterator() {
  589. var object,
  590. prop,
  591. value,
  592. index = -1,
  593. length = arguments.length;
  594. // merge options into a template data object
  595. var data = {
  596. 'bottom': '',
  597. 'exit': '',
  598. 'init': '',
  599. 'top': '',
  600. 'arrayBranch': { 'beforeLoop': '' },
  601. 'objectBranch': { 'beforeLoop': '' }
  602. };
  603. while (++index < length) {
  604. object = arguments[index];
  605. for (prop in object) {
  606. value = (value = object[prop]) == null ? '' : value;
  607. // keep this regexp explicit for the build pre-process
  608. if (/beforeLoop|inLoop/.test(prop)) {
  609. if (typeof value == 'string') {
  610. value = { 'array': value, 'object': value };
  611. }
  612. data.arrayBranch[prop] = value.array || '';
  613. data.objectBranch[prop] = value.object || '';
  614. } else {
  615. data[prop] = value;
  616. }
  617. }
  618. }
  619. // set additional template `data` values
  620. var args = data.args,
  621. firstArg = /^[^,]+/.exec(args)[0],
  622. useStrict = data.useStrict;
  623. data.firstArg = firstArg;
  624. data.hasDontEnumBug = hasDontEnumBug;
  625. data.isKeysFast = isKeysFast;
  626. data.noArgsEnum = noArgsEnum;
  627. data.shadowed = shadowed;
  628. data.useHas = data.useHas !== false;
  629. data.useStrict = useStrict == null ? isStrictFast : useStrict;
  630. if (data.noCharByIndex == null) {
  631. data.noCharByIndex = noCharByIndex;
  632. }
  633. if (!data.exit) {
  634. data.exit = 'if (!' + firstArg + ') return result';
  635. }
  636. if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
  637. data.arrayBranch = null;
  638. }
  639. // create the function factory
  640. var factory = Function(
  641. 'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn, ' +
  642. 'hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction, ' +
  643. 'isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys, ' +
  644. 'propertyIsEnumerable, slice, stringClass, toString',
  645. 'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
  646. 'return callee'
  647. );
  648. // return the compiled function
  649. return factory(
  650. arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn,
  651. hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction,
  652. isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys,
  653. propertyIsEnumerable, slice, stringClass, toString
  654. );
  655. }
  656. /**
  657. * Used by `sortBy` to compare transformed `collection` values, stable sorting
  658. * them in ascending order.
  659. *
  660. * @private
  661. * @param {Object} a The object to compare to `b`.
  662. * @param {Object} b The object to compare to `a`.
  663. * @returns {Number} Returns the sort order indicator of `1` or `-1`.
  664. */
  665. function compareAscending(a, b) {
  666. var ai = a.index,
  667. bi = b.index;
  668. a = a.criteria;
  669. b = b.criteria;
  670. if (a === undefined) {
  671. return 1;
  672. }
  673. if (b === undefined) {
  674. return -1;
  675. }
  676. // ensure a stable sort in V8 and other engines
  677. // http://code.google.com/p/v8/issues/detail?id=90
  678. return a < b ? -1 : a > b ? 1 : ai < bi ? -1 : 1;
  679. }
  680. /**
  681. * Used by `template` to replace tokens with their corresponding code snippets.
  682. *
  683. * @private
  684. * @param {String} match The matched token.
  685. * @param {String} index The `tokenized` index of the code snippet.
  686. * @returns {String} Returns the code snippet.
  687. */
  688. function detokenize(match, index) {
  689. return tokenized[index];
  690. }
  691. /**
  692. * Used by `template` to escape characters for inclusion in compiled
  693. * string literals.
  694. *
  695. * @private
  696. * @param {String} match The matched character to escape.
  697. * @returns {String} Returns the escaped character.
  698. */
  699. function escapeStringChar(match) {
  700. return '\\' + stringEscapes[match];
  701. }
  702. /**
  703. * Used by `escape` to convert characters to HTML entities.
  704. *
  705. * @private
  706. * @param {String} match The matched character to escape.
  707. * @returns {String} Returns the escaped character.
  708. */
  709. function escapeHtmlChar(match) {
  710. return htmlEscapes[match];
  711. }
  712. /**
  713. * Creates a new function that, when called, invokes `func` with the `this`
  714. * binding of `thisArg` and the arguments (value, index, object).
  715. *
  716. * @private
  717. * @param {Function} func The function to bind.
  718. * @param {Mixed} [thisArg] The `this` binding of `func`.
  719. * @returns {Function} Returns the new bound function.
  720. */
  721. function iteratorBind(func, thisArg) {
  722. return function(value, index, object) {
  723. return func.call(thisArg, value, index, object);
  724. };
  725. }
  726. /**
  727. * A no-operation function.
  728. *
  729. * @private
  730. */
  731. function noop() {
  732. // no operation performed
  733. }
  734. /**
  735. * Used by `template` to replace "escape" template delimiters with tokens.
  736. *
  737. * @private
  738. * @param {String} match The matched template delimiter.
  739. * @param {String} value The delimiter value.
  740. * @returns {String} Returns a token.
  741. */
  742. function tokenizeEscape(match, value) {
  743. if (match && reComplexDelimiter.test(value)) {
  744. return '<e%-' + value + '%>';
  745. }
  746. var index = tokenized.length;
  747. tokenized[index] = "' +\n__e(" + value + ") +\n'";
  748. return tokenHead + index + tokenFoot;
  749. }
  750. /**
  751. * Used by `template` to replace "evaluate" template delimiters, or complex
  752. * "escape" and "interpolate" delimiters, with tokens.
  753. *
  754. * @private
  755. * @param {String} match The matched template delimiter.
  756. * @param {String} escapeValue The complex "escape" delimiter value.
  757. * @param {String} interpolateValue The complex "interpolate" delimiter value.
  758. * @param {String} [evaluateValue] The "evaluate" delimiter value.
  759. * @returns {String} Returns a token.
  760. */
  761. function tokenizeEvaluate(match, escapeValue, interpolateValue, evaluateValue) {
  762. if (evaluateValue) {
  763. var index = tokenized.length;
  764. tokenized[index] = "';\n" + evaluateValue + ";\n__p += '";
  765. return tokenHead + index + tokenFoot;
  766. }
  767. return escapeValue
  768. ? tokenizeEscape(null, escapeValue)
  769. : tokenizeInterpolate(null, interpolateValue);
  770. }
  771. /**
  772. * Used by `template` to replace "interpolate" template delimiters with tokens.
  773. *
  774. * @private
  775. * @param {String} match The matched template delimiter.
  776. * @param {String} value The delimiter value.
  777. * @returns {String} Returns a token.
  778. */
  779. function tokenizeInterpolate(match, value) {
  780. if (match && reComplexDelimiter.test(value)) {
  781. return '<e%=' + value + '%>';
  782. }
  783. var index = tokenized.length;
  784. tokenized[index] = "' +\n((__t = (" + value + ")) == null ? '' : __t) +\n'";
  785. return tokenHead + index + tokenFoot;
  786. }
  787. /**
  788. * Used by `unescape` to convert HTML entities to characters.
  789. *
  790. * @private
  791. * @param {String} match The matched character to unescape.
  792. * @returns {String} Returns the unescaped character.
  793. */
  794. function unescapeHtmlChar(match) {
  795. return htmlUnescapes[match];
  796. }
  797. /*--------------------------------------------------------------------------*/
  798. /**
  799. * Checks if `value` is an `arguments` object.
  800. *
  801. * @static
  802. * @memberOf _
  803. * @category Objects
  804. * @param {Mixed} value The value to check.
  805. * @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  806. * @example
  807. *
  808. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  809. * // => true
  810. *
  811. * _.isArguments([1, 2, 3]);
  812. * // => false
  813. */
  814. function isArguments(value) {
  815. return toString.call(value) == argsClass;
  816. }
  817. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  818. if (noArgsClass) {
  819. isArguments = function(value) {
  820. return !!(value && hasOwnProperty.call(value, 'callee'));
  821. };
  822. }
  823. /**
  824. * Checks if `value` is an array.
  825. *
  826. * @static
  827. * @memberOf _
  828. * @category Objects
  829. * @param {Mixed} value The value to check.
  830. * @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
  831. * @example
  832. *
  833. * (function() { return _.isArray(arguments); })();
  834. * // => false
  835. *
  836. * _.isArray([1, 2, 3]);
  837. * // => true
  838. */
  839. var isArray = nativeIsArray || function(value) {
  840. return toString.call(value) == arrayClass;
  841. };
  842. /**
  843. * Checks if `value` is a function.
  844. *
  845. * @static
  846. * @memberOf _
  847. * @category Objects
  848. * @param {Mixed} value The value to check.
  849. * @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
  850. * @example
  851. *
  852. * _.isFunction(''.concat);
  853. * // => true
  854. */
  855. function isFunction(value) {
  856. return typeof value == 'function';
  857. }
  858. // fallback for older versions of Chrome and Safari
  859. if (isFunction(/x/)) {
  860. isFunction = function(value) {
  861. return toString.call(value) == funcClass;
  862. };
  863. }
  864. /**
  865. * A fallback implementation of `isPlainObject`.
  866. *
  867. * @private
  868. * @param {Mixed} value The value to check.
  869. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  870. * `arguments` objects.
  871. * @returns {Boolean} Returns `true` if the `value` is a plain `Object` object,
  872. * else `false`.
  873. */
  874. function isPlainFallback(value, skipArgsCheck) {
  875. // avoid non-objects and false positives for `arguments` objects
  876. var result = false;
  877. if (!(value && typeof value == 'object') || (!skipArgsCheck && isArguments(value))) {
  878. return result;
  879. }
  880. // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
  881. // methods that are `typeof` "string" and still can coerce nodes to strings.
  882. // Also check that the constructor is `Object` (i.e. `Object instanceof Object`)
  883. var ctor = value.constructor;
  884. if ((!noNodeClass || !(typeof value.toString != 'function' && typeof (value + '') == 'string')) &&
  885. (!isFunction(ctor) || ctor instanceof ctor)) {
  886. // IE < 9 iterates inherited properties before own properties. If the first
  887. // iterated property is an object's own property then there are no inherited
  888. // enumerable properties.
  889. if (iteratesOwnLast) {
  890. forIn(value, function(objValue, objKey) {
  891. result = !hasOwnProperty.call(value, objKey);
  892. return false;
  893. });
  894. return result === false;
  895. }
  896. // In most environments an object's own properties are iterated before
  897. // its inherited properties. If the last iterated property is an object's
  898. // own property then there are no inherited enumerable properties.
  899. forIn(value, function(objValue, objKey) {
  900. result = objKey;
  901. });
  902. return result === false || hasOwnProperty.call(value, result);
  903. }
  904. return result;
  905. }
  906. /**
  907. * Checks if a given `value` is an object created by the `Object` constructor
  908. * assuming objects created by the `Object` constructor have no inherited
  909. * enumerable properties and that there are no `Object.prototype` extensions.
  910. *
  911. * @private
  912. * @param {Mixed} value The value to check.
  913. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  914. * `arguments` objects.
  915. * @returns {Boolean} Returns `true` if the `value` is a plain `Object` object,
  916. * else `false`.
  917. */
  918. var isPlainObject = objectTypes.__proto__ != ObjectProto ? isPlainFallback : function(value, skipArgsCheck) {
  919. if (!value) {
  920. return false;
  921. }
  922. var valueOf = value.valueOf,
  923. objProto = typeof valueOf == 'function' && valueOf.__proto__.__proto__;
  924. return objProto
  925. ? value == objProto || (value.__proto__ == objProto && (skipArgsCheck || !isArguments(value)))
  926. : isPlainFallback(value);
  927. };
  928. /**
  929. * A shim implementation of `Object.keys` that produces an array of the given
  930. * object's own enumerable property names.
  931. *
  932. * @private
  933. * @param {Object} object The object to inspect.
  934. * @returns {Array} Returns a new array of property names.
  935. */
  936. var shimKeys = createIterator({
  937. 'args': 'object',
  938. 'init': '[]',
  939. 'inLoop': 'result.push(index)'
  940. });
  941. /*--------------------------------------------------------------------------*/
  942. /**
  943. * Creates a clone of `value`. If `deep` is `true`, all nested objects will
  944. * also be cloned otherwise they will be assigned by reference. If a value has
  945. * a `clone` method it will be used to perform the clone. Functions, DOM nodes,
  946. * `arguments` objects, and objects created by constructors other than `Object`
  947. * are **not** cloned unless they have a custom `clone` method.
  948. *
  949. * @static
  950. * @memberOf _
  951. * @category Objects
  952. * @param {Mixed} value The value to clone.
  953. * @param {Boolean} deep A flag to indicate a deep clone.
  954. * @param {Object} [guard] Internally used to allow this method to work with
  955. * others like `_.map` without using their callback `index` argument for `deep`.
  956. * @param {Array} [stack=[]] Internally used to keep track of traversed objects
  957. * to avoid circular references.
  958. * @param {Object} thorough Internally used to indicate whether or not to perform
  959. * a more thorough clone of non-object values.
  960. * @returns {Mixed} Returns the cloned `value`.
  961. * @example
  962. *
  963. * var stooges = [
  964. * { 'name': 'moe', 'age': 40 },
  965. * { 'name': 'larry', 'age': 50 },
  966. * { 'name': 'curly', 'age': 60 }
  967. * ];
  968. *
  969. * _.clone({ 'name': 'moe' });
  970. * // => { 'name': 'moe' }
  971. *
  972. * var shallow = _.clone(stooges);
  973. * shallow[0] === stooges[0];
  974. * // => true
  975. *
  976. * var deep = _.clone(stooges, true);
  977. * shallow[0] === stooges[0];
  978. * // => false
  979. */
  980. function clone(value, deep, guard, stack, thorough) {
  981. if (value == null) {
  982. return value;
  983. }
  984. if (guard) {
  985. deep = false;
  986. }
  987. // avoid slower checks on primitives
  988. thorough || (thorough = { 'value': null });
  989. if (thorough.value == null) {
  990. // primitives passed from iframes use the primary document's native prototypes
  991. thorough.value = !!(BoolProto.clone || NumberProto.clone || StringProto.clone);
  992. }
  993. // use custom `clone` method if available
  994. var isObj = objectTypes[typeof value];
  995. if ((isObj || thorough.value) && value.clone && isFunction(value.clone)) {
  996. thorough.value = null;
  997. return value.clone(deep);
  998. }
  999. // inspect [[Class]]
  1000. if (isObj) {
  1001. // don't clone `arguments` objects, functions, or non-object Objects
  1002. var className = toString.call(value);
  1003. if (!cloneableClasses[className] || (noArgsClass && isArguments(value))) {
  1004. return value;
  1005. }
  1006. var isArr = className == arrayClass;
  1007. isObj = isArr || (className == objectClass ? isPlainObject(value, true) : isObj);
  1008. }
  1009. // shallow clone
  1010. if (!isObj || !deep) {
  1011. // don't clone functions
  1012. return isObj
  1013. ? (isArr ? slice.call(value) : extend({}, value))
  1014. : value;
  1015. }
  1016. var ctor = value.constructor;
  1017. switch (className) {
  1018. case boolClass:
  1019. return new ctor(value == true);
  1020. case dateClass:
  1021. return new ctor(+value);
  1022. case numberClass:
  1023. case stringClass:
  1024. return new ctor(value);
  1025. case regexpClass:
  1026. return ctor(value.source, reFlags.exec(value));
  1027. }
  1028. // check for circular references and return corresponding clone
  1029. stack || (stack = []);
  1030. var length = stack.length;
  1031. while (length--) {
  1032. if (stack[length].source == value) {
  1033. return stack[length].value;
  1034. }
  1035. }
  1036. // init cloned object
  1037. length = value.length;
  1038. var result = isArr ? ctor(length) : {};
  1039. // add current clone and original source value to the stack of traversed objects
  1040. stack.push({ 'value': result, 'source': value });
  1041. // recursively populate clone (susceptible to call stack limits)
  1042. if (isArr) {
  1043. var index = -1;
  1044. while (++index < length) {
  1045. result[index] = clone(value[index], deep, null, stack, thorough);
  1046. }
  1047. } else {
  1048. forOwn(value, function(objValue, key) {
  1049. result[key] = clone(objValue, deep, null, stack, thorough);
  1050. });
  1051. }
  1052. return result;
  1053. }
  1054. /**
  1055. * Assigns enumerable properties of the default object(s) to the `destination`
  1056. * object for all `destination` properties that resolve to `null`/`undefined`.
  1057. * Once a property is set, additional defaults of the same property will be
  1058. * ignored.
  1059. *
  1060. * @static
  1061. * @memberOf _
  1062. * @category Objects
  1063. * @param {Object} object The destination object.
  1064. * @param {Object} [default1, default2, ...] The default objects.
  1065. * @returns {Object} Returns the destination object.
  1066. * @example
  1067. *
  1068. * var iceCream = { 'flavor': 'chocolate' };
  1069. * _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
  1070. * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
  1071. */
  1072. var defaults = createIterator(extendIteratorOptions, {
  1073. 'inLoop': 'if (result[index] == null) ' + extendIteratorOptions.inLoop
  1074. });
  1075. /**
  1076. * Assigns enumerable properties of the source object(s) to the `destination`
  1077. * object. Subsequent sources will overwrite propery assignments of previous
  1078. * sources.
  1079. *
  1080. * @static
  1081. * @memberOf _
  1082. * @category Objects
  1083. * @param {Object} object The destination object.
  1084. * @param {Object} [source1, source2, ...] The source objects.
  1085. * @returns {Object} Returns the destination object.
  1086. * @example
  1087. *
  1088. * _.extend({ 'name': 'moe' }, { 'age': 40 });
  1089. * // => { 'name': 'moe', 'age': 40 }
  1090. */
  1091. var extend = createIterator(extendIteratorOptions);
  1092. /**
  1093. * Iterates over `object`'s own and inherited enumerable properties, executing
  1094. * the `callback` for each property. The `callback` is bound to `thisArg` and
  1095. * invoked with 3 arguments; (value, key, object). Callbacks may exit iteration
  1096. * early by explicitly returning `false`.
  1097. *
  1098. * @static
  1099. * @memberOf _
  1100. * @category Objects
  1101. * @param {Object} object The object to iterate over.
  1102. * @param {Function} callback The function called per iteration.
  1103. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1104. * @returns {Object} Returns `object`.
  1105. * @example
  1106. *
  1107. * function Dog(name) {
  1108. * this.name = name;
  1109. * }
  1110. *
  1111. * Dog.prototype.bark = function() {
  1112. * alert('Woof, woof!');
  1113. * };
  1114. *
  1115. * _.forIn(new Dog('Dagny'), function(value, key) {
  1116. * alert(key);
  1117. * });
  1118. * // => alerts 'name' and 'bark' (order is not guaranteed)
  1119. */
  1120. var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
  1121. 'useHas': false
  1122. });
  1123. /**
  1124. * Iterates over `object`'s own enumerable properties, executing the `callback`
  1125. * for each property. The `callback` is bound to `thisArg` and invoked with 3
  1126. * arguments; (value, key, object). Callbacks may exit iteration early by
  1127. * explicitly returning `false`.
  1128. *
  1129. * @static
  1130. * @memberOf _
  1131. * @category Objects
  1132. * @param {Object} object The object to iterate over.
  1133. * @param {Function} callback The function called per iteration.
  1134. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1135. * @returns {Object} Returns `object`.
  1136. * @example
  1137. *
  1138. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  1139. * alert(key);
  1140. * });
  1141. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  1142. */
  1143. var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
  1144. /**
  1145. * Creates a sorted array of all enumerable properties, own and inherited,
  1146. * of `object` that have function values.
  1147. *
  1148. * @static
  1149. * @memberOf _
  1150. * @alias methods
  1151. * @category Objects
  1152. * @param {Object} object The object to inspect.
  1153. * @returns {Array} Returns a new array of property names that have function values.
  1154. * @example
  1155. *
  1156. * _.functions(_);
  1157. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1158. */
  1159. var functions = createIterator({
  1160. 'useHas': false,
  1161. 'args': 'object',
  1162. 'init': '[]',
  1163. 'inLoop': 'if (isFunction(value)) result.push(index)',
  1164. 'bottom': 'result.sort()'
  1165. });
  1166. /**
  1167. * Checks if the specified object `property` exists and is a direct property,
  1168. * instead of an inherited property.
  1169. *
  1170. * @static
  1171. * @memberOf _
  1172. * @category Objects
  1173. * @param {Object} object The object to check.
  1174. * @param {String} property The property to check for.
  1175. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  1176. * @example
  1177. *
  1178. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1179. * // => true
  1180. */
  1181. function has(object, property) {
  1182. return object ? hasOwnProperty.call(object, property) : false;
  1183. }
  1184. /**
  1185. * Invert the keys and values of an object. The values must be serializable.
  1186. *
  1187. * @static
  1188. * @memberOf _
  1189. * @alias methods
  1190. * @category Objects
  1191. * @param {Object} object The object to inspect.
  1192. * @returns {Array} Returns a new array of property names that have function values.
  1193. * @example
  1194. *
  1195. * var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}
  1196. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1197. */
  1198. var invert = createIterator({
  1199. 'args': 'object',
  1200. 'init': '{}',
  1201. 'inLoop': 'result[value] = index'
  1202. });
  1203. /**
  1204. * Checks if `value` is a boolean (`true` or `false`) value.
  1205. *
  1206. * @static
  1207. * @memberOf _
  1208. * @category Objects
  1209. * @param {Mixed} value The value to check.
  1210. * @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1211. * @example
  1212. *
  1213. * _.isBoolean(null);
  1214. * // => false
  1215. */
  1216. function isBoolean(value) {
  1217. return value === true || value === false || toString.call(value) == boolClass;
  1218. }
  1219. /**
  1220. * Checks if `value` is a date.
  1221. *
  1222. * @static
  1223. * @memberOf _
  1224. * @category Objects
  1225. * @param {Mixed} value The value to check.
  1226. * @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
  1227. * @example
  1228. *
  1229. * _.isDate(new Date);
  1230. * // => true
  1231. */
  1232. function isDate(value) {
  1233. return toString.call(value) == dateClass;
  1234. }
  1235. /**
  1236. * Checks if `value` is a DOM element.
  1237. *
  1238. * @static
  1239. * @memberOf _
  1240. * @category Objects
  1241. * @param {Mixed} value The value to check.
  1242. * @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1243. * @example
  1244. *
  1245. * _.isElement(document.body);
  1246. * // => true
  1247. */
  1248. function isElement(value) {
  1249. return value ? value.nodeType === 1 : false;
  1250. }
  1251. /**
  1252. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1253. * length of `0` and objects with no own enumerable properties are considered
  1254. * "empty".
  1255. *
  1256. * @static
  1257. * @memberOf _
  1258. * @category Objects
  1259. * @param {Array|Object|String} value The value to inspect.
  1260. * @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
  1261. * @example
  1262. *
  1263. * _.isEmpty([1, 2, 3]);
  1264. * // => false
  1265. *
  1266. * _.isEmpty({});
  1267. * // => true
  1268. *
  1269. * _.isEmpty('');
  1270. * // => true
  1271. */
  1272. var isEmpty = createIterator({
  1273. 'args': 'value',
  1274. 'init': 'true',
  1275. 'top':
  1276. 'var className = toString.call(value),\n' +
  1277. ' length = value.length;\n' +
  1278. 'if (arrayLikeClasses[className]' +
  1279. (noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
  1280. ' (className == objectClass && length > -1 && length === length >>> 0 &&\n' +
  1281. ' isFunction(value.splice))' +
  1282. ') return !length',
  1283. 'inLoop': {
  1284. 'object': 'return false'
  1285. }
  1286. });
  1287. /**
  1288. * Performs a deep comparison between two values to determine if they are
  1289. * equivalent to each other. If a value has an `isEqual` method it will be
  1290. * used to perform the comparison.
  1291. *
  1292. * @static
  1293. * @memberOf _
  1294. * @category Objects
  1295. * @param {Mixed} a The value to compare.
  1296. * @param {Mixed} b The other value to compare.
  1297. * @param {Array} [stack=[]] Internally used to keep track of traversed objects
  1298. * to avoid circular references.
  1299. * @param {Object} thorough Internally used to indicate whether or not to perform
  1300. * a more thorough comparison of non-object values.
  1301. * @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
  1302. * @example
  1303. *
  1304. * var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1305. * var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1306. *
  1307. * moe == clone;
  1308. * // => false
  1309. *
  1310. * _.isEqual(moe, clone);
  1311. * // => true
  1312. */
  1313. function isEqual(a, b, stack, thorough) {
  1314. // a strict comparison is necessary because `null == undefined`
  1315. if (a == null || b == null) {
  1316. return a === b;
  1317. }
  1318. // avoid slower checks on non-objects
  1319. thorough || (thorough = { 'value': null });
  1320. if (thorough.value == null) {
  1321. // primitives passed from iframes use the primary document's native prototypes
  1322. thorough.value = !!(BoolProto.isEqual || NumberProto.isEqual || StringProto.isEqual);
  1323. }
  1324. if (objectTypes[typeof a] || objectTypes[typeof b] || thorough.value) {
  1325. // unwrap any LoDash wrapped values
  1326. a = a.__wrapped__ || a;
  1327. b = b.__wrapped__ || b;
  1328. // use custom `isEqual` method if available
  1329. if (a.isEqual && isFunction(a.isEqual)) {
  1330. thorough.value = null;
  1331. return a.isEqual(b);
  1332. }
  1333. if (b.isEqual && isFunction(b.isEqual)) {
  1334. thorough.value = null;
  1335. return b.isEqual(a);
  1336. }
  1337. }
  1338. // exit early for identical values
  1339. if (a === b) {
  1340. // treat `+0` vs. `-0` as not equal
  1341. return a !== 0 || (1 / a == 1 / b);
  1342. }
  1343. // compare [[Class]] names
  1344. var className = toString.call(a);
  1345. if (className != toString.call(b)) {
  1346. return false;
  1347. }
  1348. switch (className) {
  1349. case boolClass:
  1350. case dateClass:
  1351. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  1352. // to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
  1353. return +a == +b;
  1354. case numberClass:
  1355. // treat `NaN` vs. `NaN` as equal
  1356. return a != +a
  1357. ? b != +b
  1358. // but treat `+0` vs. `-0` as not equal
  1359. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  1360. case regexpClass:
  1361. case stringClass:
  1362. // coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
  1363. // treat string primitives and their corresponding object instances as equal
  1364. return a == b + '';
  1365. }
  1366. // exit early, in older browsers, if `a` is array-like but not `b`
  1367. var isArr = arrayLikeClasses[className];
  1368. if (noArgsClass && !isArr && (isArr = isArguments(a)) && !isArguments(b)) {
  1369. return false;
  1370. }
  1371. // exit for functions and DOM nodes
  1372. if (!isArr && (className != objectClass || (noNodeClass && (
  1373. (typeof a.toString != 'function' && typeof (a + '') == 'string') ||
  1374. (typeof b.toString != 'function' && typeof (b + '') == 'string'))))) {
  1375. return false;
  1376. }
  1377. // assume cyclic structures are equal
  1378. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  1379. // section 15.12.3, abstract operatiā€¦

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