PageRenderTime 141ms CodeModel.GetById 42ms RepoModel.GetById 3ms app.codeStats 0ms

/ajax/libs//0.5.2/lodash.js

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