PageRenderTime 51ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs//0.5.1/lodash.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 1512 lines | 1398 code | 35 blank | 79 comment | 21 complexity | 47c14a2607e0325522764feda4218219 MD5 | raw file
  1. /*!
  2. * Lo-Dash v0.5.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 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': 'obj'
  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 = 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 (!(skipProto && index == \'prototype\')) {\n' +
  330. ' value = iteratee[index];\n' +
  331. ' <%= objectBranch.inLoop %>\n' +
  332. ' }\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. ' <% } else { %>\n' +
  344. // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
  345. // (if the prototype or a property on the prototype has been set)
  346. // incorrectly sets a function's `prototype` property [[Enumerable]]
  347. // value to `true`. Because of this Lo-Dash standardizes on skipping
  348. // the the `prototype` property of functions regardless of its
  349. // [[Enumerable]] value.
  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, sorting them
  601. * stabily 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 (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. var index = tokenized.length;
  750. if (escapeValue) {
  751. tokenized[index] = "' +\n__e(" + escapeValue + ") +\n'";
  752. } else if (evaluateValue) {
  753. tokenized[index] = "';\n" + evaluateValue + ";\n__p += '";
  754. } else if (interpolateValue) {
  755. tokenized[index] = "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  756. }
  757. return token + index;
  758. }
  759. /**
  760. * Used by `template` to replace "interpolate" template delimiters with tokens.
  761. *
  762. * @private
  763. * @param {String} match The matched template delimiter.
  764. * @param {String} value The delimiter value.
  765. * @returns {String} Returns a token.
  766. */
  767. function tokenizeInterpolate(match, value) {
  768. if (reComplexDelimiter.test(value)) {
  769. return '<e%=' + value + '%>';
  770. }
  771. var index = tokenized.length;
  772. tokenized[index] = "' +\n((__t = (" + value + ")) == null ? '' : __t) +\n'";
  773. return token + index;
  774. }
  775. /*--------------------------------------------------------------------------*/
  776. /**
  777. * Checks if `value` is an `arguments` object.
  778. *
  779. * @static
  780. * @memberOf _
  781. * @category Objects
  782. * @param {Mixed} value The value to check.
  783. * @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  784. * @example
  785. *
  786. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  787. * // => true
  788. *
  789. * _.isArguments([1, 2, 3]);
  790. * // => false
  791. */
  792. function isArguments(value) {
  793. return toString.call(value) == argsClass;
  794. }
  795. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  796. if (noArgsClass) {
  797. isArguments = function(value) {
  798. return !!(value && hasOwnProperty.call(value, 'callee'));
  799. };
  800. }
  801. /**
  802. * Checks if `value` is an array.
  803. *
  804. * @static
  805. * @memberOf _
  806. * @category Objects
  807. * @param {Mixed} value The value to check.
  808. * @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
  809. * @example
  810. *
  811. * (function() { return _.isArray(arguments); })();
  812. * // => false
  813. *
  814. * _.isArray([1, 2, 3]);
  815. * // => true
  816. */
  817. var isArray = nativeIsArray || function(value) {
  818. return toString.call(value) == arrayClass;
  819. };
  820. /**
  821. * Checks if `value` is a function.
  822. *
  823. * @static
  824. * @memberOf _
  825. * @category Objects
  826. * @param {Mixed} value The value to check.
  827. * @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
  828. * @example
  829. *
  830. * _.isFunction(''.concat);
  831. * // => true
  832. */
  833. function isFunction(value) {
  834. return typeof value == 'function';
  835. }
  836. // fallback for older versions of Chrome and Safari
  837. if (isFunction(/x/)) {
  838. isFunction = function(value) {
  839. return toString.call(value) == funcClass;
  840. };
  841. }
  842. /**
  843. * A shim implementation of `Object.keys` that produces an array of the given
  844. * object's own enumerable property names.
  845. *
  846. * @private
  847. * @param {Object} object The object to inspect.
  848. * @returns {Array} Returns a new array of property names.
  849. */
  850. var shimKeys = createIterator({
  851. 'args': 'object',
  852. 'exit': 'if (!(object && objectTypes[typeof object])) throw TypeError()',
  853. 'init': '[]',
  854. 'inLoop': 'result.push(index)'
  855. });
  856. /*--------------------------------------------------------------------------*/
  857. /**
  858. * Creates a clone of `value`. If `deep` is `true`, all nested objects will
  859. * also be cloned otherwise they will be assigned by reference. If a value has
  860. * a `clone` method it will be used to perform the clone. Functions, DOM nodes,
  861. * `arguments` objects, and objects created by constructors other than `Object`
  862. * are **not** cloned unless they have a custom `clone` method.
  863. *
  864. * @static
  865. * @memberOf _
  866. * @category Objects
  867. * @param {Mixed} value The value to clone.
  868. * @param {Boolean} deep A flag to indicate a deep clone.
  869. * @param {Object} [guard] Internally used to allow this method to work with
  870. * others like `_.map` without using their callback `index` argument for `deep`.
  871. * @param {Array} [stack=[]] Internally used to keep track of traversed objects
  872. * to avoid circular references.
  873. * @param {Object} thorough Internally used to indicate whether or not to perform
  874. * a more thorough clone of non-object values.
  875. * @returns {Mixed} Returns the cloned `value`.
  876. * @example
  877. *
  878. * var stooges = [
  879. * { 'name': 'moe', 'age': 40 },
  880. * { 'name': 'larry', 'age': 50 },
  881. * { 'name': 'curly', 'age': 60 }
  882. * ];
  883. *
  884. * _.clone({ 'name': 'moe' });
  885. * // => { 'name': 'moe' }
  886. *
  887. * var shallow = _.clone(stooges);
  888. * shallow[0] === stooges[0];
  889. * // => true
  890. *
  891. * var deep = _.clone(stooges, true);
  892. * shallow[0] === stooges[0];
  893. * // => false
  894. */
  895. function clone(value, deep, guard, stack, thorough) {
  896. if (value == null) {
  897. return value;
  898. }
  899. if (guard) {
  900. deep = false;
  901. }
  902. // avoid slower checks on primitives
  903. thorough || (thorough = { 'value': null });
  904. if (thorough.value == null) {
  905. // primitives passed from iframes use the primary document's native prototypes
  906. thorough.value = !!(BoolProto.clone || NumberProto.clone || StringProto.clone);
  907. }
  908. // use custom `clone` method if available
  909. var isObj = objectTypes[typeof value];
  910. if ((isObj || thorough.value) && value.clone && isFunction(value.clone)) {
  911. thorough.value = null;
  912. return value.clone(deep);
  913. }
  914. // inspect [[Class]]
  915. if (isObj) {
  916. // don't clone `arguments` objects, functions, or non-object Objects
  917. var className = toString.call(value);
  918. if (!cloneableClasses[className] || (noArgsClass && isArguments(value))) {
  919. return value;
  920. }
  921. var isArr = className == arrayClass;
  922. isObj = isArr || (className == objectClass ? isPlainObject(value, true) : isObj);
  923. }
  924. // shallow clone
  925. if (!isObj || !deep) {
  926. // don't clone functions
  927. return isObj
  928. ? (isArr ? slice.call(value) : extend({}, value))
  929. : value;
  930. }
  931. var ctor = value.constructor;
  932. switch (className) {
  933. case boolClass:
  934. return new ctor(value == true);
  935. case dateClass:
  936. return new ctor(+value);
  937. case numberClass:
  938. case stringClass:
  939. return new ctor(value);
  940. case regexpClass:
  941. return ctor(value.source, reFlags.exec(value));
  942. }
  943. // check for circular references and return corresponding clone
  944. stack || (stack = []);
  945. var length = stack.length;
  946. while (length--) {
  947. if (stack[length].source == value) {
  948. return stack[length].value;
  949. }
  950. }
  951. // init cloned object
  952. length = value.length;
  953. var result = isArr ? ctor(length) : {};
  954. // add current clone and original source value to the stack of traversed objects
  955. stack.push({ 'value': result, 'source': value });
  956. // recursively populate clone (susceptible to call stack limits)
  957. if (isArr) {
  958. var index = -1;
  959. while (++index < length) {
  960. result[index] = clone(value[index], deep, null, stack, thorough);
  961. }
  962. } else {
  963. forOwn(value, function(objValue, key) {
  964. result[key] = clone(objValue, deep, null, stack, thorough);
  965. });
  966. }
  967. return result;
  968. }
  969. /**
  970. * Assigns enumerable properties of the default object(s) to the `destination`
  971. * object for all `destination` properties that resolve to `null`/`undefined`.
  972. * Once a property is set, additional defaults of the same property will be
  973. * ignored.
  974. *
  975. * @static
  976. * @memberOf _
  977. * @category Objects
  978. * @param {Object} object The destination object.
  979. * @param {Object} [default1, default2, ...] The default objects.
  980. * @returns {Object} Returns the destination object.
  981. * @example
  982. *
  983. * var iceCream = { 'flavor': 'chocolate' };
  984. * _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
  985. * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
  986. */
  987. var defaults = createIterator(extendIteratorOptions, {
  988. 'inLoop': 'if (result[index] == null) ' + extendIteratorOptions.inLoop
  989. });
  990. /**
  991. * Creates a shallow clone of `object` excluding the specified properties.
  992. * Property names may be specified as individual arguments or as arrays of
  993. * property names.
  994. *
  995. * @static
  996. * @memberOf _
  997. * @category Objects
  998. * @param {Object} object The source object.
  999. * @param {Object} [prop1, prop2, ...] The properties to drop.
  1000. * @returns {Object} Returns an object without the dropped properties.
  1001. * @example
  1002. *
  1003. * _.drop({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
  1004. * // => { 'name': 'moe', 'age': 40 }
  1005. */
  1006. var drop = createIterator({
  1007. 'useHas': false,
  1008. 'args': 'object',
  1009. 'init': '{}',
  1010. 'top': 'var props = concat.apply(ArrayProto, arguments)',
  1011. 'inLoop': 'if (indexOf(props, index) < 0) result[index] = value'
  1012. });
  1013. /**
  1014. * Assigns enumerable properties of the source object(s) to the `destination`
  1015. * object. Subsequent sources will overwrite propery assignments of previous
  1016. * sources.
  1017. *
  1018. * @static
  1019. * @memberOf _
  1020. * @category Objects
  1021. * @param {Object} object The destination object.
  1022. * @param {Object} [source1, source2, ...] The source objects.
  1023. * @returns {Object} Returns the destination object.
  1024. * @example
  1025. *
  1026. * _.extend({ 'name': 'moe' }, { 'age': 40 });
  1027. * // => { 'name': 'moe', 'age': 40 }
  1028. */
  1029. var extend = createIterator(extendIteratorOptions);
  1030. /**
  1031. * Iterates over `object`'s own and inherited enumerable properties, executing
  1032. * the `callback` for each property. The `callback` is bound to `thisArg` and
  1033. * invoked with 3 arguments; (value, key, object). Callbacks may exit iteration
  1034. * early by explicitly returning `false`.
  1035. *
  1036. * @static
  1037. * @memberOf _
  1038. * @category Objects
  1039. * @param {Object} object The object to iterate over.
  1040. * @param {Function} callback The function called per iteration.
  1041. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1042. * @returns {Object} Returns `object`.
  1043. * @example
  1044. *
  1045. * function Dog(name) {
  1046. * this.name = name;
  1047. * }
  1048. *
  1049. * Dog.prototype.bark = function() {
  1050. * alert('Woof, woof!');
  1051. * };
  1052. *
  1053. * _.forIn(new Dog('Dagny'), function(value, key) {
  1054. * alert(key);
  1055. * });
  1056. * // => alerts 'name' and 'bark' (order is not guaranteed)
  1057. */
  1058. var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
  1059. 'useHas': false
  1060. });
  1061. /**
  1062. * Iterates over `object`'s own enumerable properties, executing the `callback`
  1063. * for each property. The `callback` is bound to `thisArg` and invoked with 3
  1064. * arguments; (value, key, object). Callbacks may exit iteration early by
  1065. * explicitly returning `false`.
  1066. *
  1067. * @static
  1068. * @memberOf _
  1069. * @category Objects
  1070. * @param {Object} object The object to iterate over.
  1071. * @param {Function} callback The function called per iteration.
  1072. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1073. * @returns {Object} Returns `object`.
  1074. * @example
  1075. *
  1076. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  1077. * alert(key);
  1078. * });
  1079. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  1080. */
  1081. var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
  1082. /**
  1083. * Creates a sorted array of all enumerable properties, own and inherited,
  1084. * of `object` that have function values.
  1085. *
  1086. * @static
  1087. * @memberOf _
  1088. * @alias methods
  1089. * @category Objects
  1090. * @param {Object} object The object to inspect.
  1091. * @returns {Array} Returns a new array of property names that have function values.
  1092. * @example
  1093. *
  1094. * _.functions(_);
  1095. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1096. */
  1097. var functions = createIterator({
  1098. 'useHas': false,
  1099. 'args': 'object',
  1100. 'init': '[]',
  1101. 'inLoop': 'if (isFunction(value)) result.push(index)',
  1102. 'bottom': 'result.sort()'
  1103. });
  1104. /**
  1105. * Checks if the specified object `property` exists and is a direct property,
  1106. * instead of an inherited property.
  1107. *
  1108. * @static
  1109. * @memberOf _
  1110. * @category Objects
  1111. * @param {Object} object The object to check.
  1112. * @param {String} property The property to check for.
  1113. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  1114. * @example
  1115. *
  1116. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1117. * // => true
  1118. */
  1119. function has(object, property) {
  1120. return hasOwnProperty.call(object, property);
  1121. }
  1122. /**
  1123. * Checks if `value` is a boolean (`true` or `false`) value.
  1124. *
  1125. * @static
  1126. * @memberOf _
  1127. * @category Objects
  1128. * @param {Mixed} value The value to check.
  1129. * @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1130. * @example
  1131. *
  1132. * _.isBoolean(null);
  1133. * // => false
  1134. */
  1135. function isBoolean(value) {
  1136. return value === true || value === false || toString.call(value) == boolClass;
  1137. }
  1138. /**
  1139. * Checks if `value` is a date.
  1140. *
  1141. * @static
  1142. * @memberOf _
  1143. * @category Objects
  1144. * @param {Mixed} value The value to check.
  1145. * @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
  1146. * @example
  1147. *
  1148. * _.isDate(new Date);
  1149. * // => true
  1150. */
  1151. function isDate(value) {
  1152. return toString.call(value) == dateClass;
  1153. }
  1154. /**
  1155. * Checks if `value` is a DOM element.
  1156. *
  1157. * @static
  1158. * @memberOf _
  1159. * @category Objects
  1160. * @param {Mixed} value The value to check.
  1161. * @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1162. * @example
  1163. *
  1164. * _.isElement(document.body);
  1165. * // => true
  1166. */
  1167. function isElement(value) {
  1168. return !!(value && value.nodeType == 1);
  1169. }
  1170. /**
  1171. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1172. * length of `0` and objects with no own enumerable properties are considered
  1173. * "empty".
  1174. *
  1175. * @static
  1176. * @memberOf _
  1177. * @category Objects
  1178. * @param {Array|Object|String} value The value to inspect.
  1179. * @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
  1180. * @example
  1181. *
  1182. * _.isEmpty([1, 2, 3]);
  1183. * // => false
  1184. *
  1185. * _.isEmpty({});
  1186. * // => true
  1187. *
  1188. * _.isEmpty('');
  1189. * // => true
  1190. */
  1191. var isEmpty = createIterator({
  1192. 'args': 'value',
  1193. 'init': 'true',
  1194. 'top':
  1195. 'var className = toString.call(value),\n' +
  1196. ' length = value.length;\n' +
  1197. 'if (arrayLikeClasses[className]' +
  1198. (noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
  1199. ' (className == objectClass && length > -1 && length === length >>> 0 &&\n' +
  1200. ' isFunction(value.splice))' +
  1201. ') return !length',
  1202. 'inLoop': {
  1203. 'object': 'return false'
  1204. }
  1205. });
  1206. /**
  1207. * Performs a deep comparison between two values to determine if they are
  1208. * equivalent to each other. If a value has an `isEqual` method it will be
  1209. * used to perform the comparison.
  1210. *
  1211. * @static
  1212. * @memberOf _
  1213. * @category Objects
  1214. * @param {Mixed} a The value to compare.
  1215. * @param {Mixed} b The other value to compare.
  1216. * @param {Array} [stack=[]] Internally used to keep track of traversed objects
  1217. * to avoid circular references.
  1218. * @param {Object} thorough Internally used to indicate whether or not to perform
  1219. * a more thorough comparison of non-object values.
  1220. * @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
  1221. * @example
  1222. *
  1223. * var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1224. * var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1225. *
  1226. * moe == clone;
  1227. * // => false
  1228. *
  1229. * _.isEqual(moe, clone);
  1230. * // => true
  1231. */
  1232. function isEqual(a, b, stack, thorough) {
  1233. // a strict comparison is necessary because `null == undefined`
  1234. if (a == null || b == null) {
  1235. return a === b;
  1236. }
  1237. // avoid slower checks on non-objects
  1238. thorough || (thorough = { 'value': null });
  1239. if (thorough.value == null) {
  1240. // primitives passed from iframes use the primary document's native prototypes
  1241. thorough.value = !!(BoolProto.isEqual || NumberProto.isEqual || StringProto.isEqual);
  1242. }
  1243. if (objectTypes[typeof a] || objectTypes[typeof b] || thorough.value) {
  1244. // unwrap any LoDash wrapped values
  1245. if (a._chain) {
  1246. a = a._wrapped;
  1247. }
  1248. if (b._chain) {
  1249. b = b._wrapped;
  1250. }
  1251. // use custom `isEqual` method if available
  1252. if (a.isEqual && isFunction(a.isEqual)) {
  1253. thorough.value = null;
  1254. return a.isEqual(b);
  1255. }
  1256. if (b.isEqual && isFunction(b.isEqual)) {
  1257. thorough.value = null;
  1258. return b.isEqual(a);
  1259. }
  1260. }
  1261. // exit early for identical values
  1262. if (a === b) {
  1263. // treat `+0` vs. `-0` as not equal
  1264. return a !== 0 || (1 / a == 1 / b);
  1265. }
  1266. // compare [[Class]] names
  1267. var className = toString.call(a);
  1268. if (className != toString.call(b)) {
  1269. return false;
  1270. }
  1271. switch (className) {
  1272. case boolClass:
  1273. case dateClass:
  1274. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  1275. // to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
  1276. return +a == +b;
  1277. case numberClass:
  1278. // treat `NaN` vs. `NaN` as equal
  1279. return a != +a
  1280. ? b != +b
  1281. // but treat `+0` vs. `-0` as not equal
  1282. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  1283. case regexpClass:
  1284. case stringClass:
  1285. // coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
  1286. // treat string primitives and their corresponding object instances as equal
  1287. return a == b + '';
  1288. }
  1289. // exit early, in older browsers, if `a` is array-like but not `b`
  1290. var isArr = arrayLikeClasses[className];
  1291. if (noArgsClass && !isArr && (isArr = isArguments(a)) && !isArguments(b)) {
  1292. return false;
  1293. }
  1294. // exit for functions and DOM nodes
  1295. if (!isArr && (className != objectClass || (noNodeClass && (
  1296. (typeof a.toString != 'function' && typeof (a + '') == 'string') ||
  1297. (typeof b.toString != 'function' && typeof (b + '') == 'string'))))) {
  1298. return false;
  1299. }
  1300. // assume cyclic structures are equal
  1301. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  1302. // section 15.12.3, abstract operation `JO` (http://es5.github.com/#x15.12.3)
  1303. stack || (stack = []);
  1304. var length = stack.length;
  1305. while (length--) {
  1306. if (stack[length] == a) {
  1307. return true;
  1308. }
  1309. }
  1310. var index = -1,
  1311. result = true,
  1312. size = 0;
  1313. // add `a` to the stack of traversed objects
  1314. stack.push(a);
  1315. // recursively compare objects and arrays (susceptible to call stack limits)
  1316. if (isArr) {
  1317. // compare lengths to determine if a deep comparison is necessary
  1318. size = a.length;
  1319. result = size == b.length;
  1320. if (result) {
  1321. // deep compare the contents, ignoring non-numeric properties
  1322. while (size--) {
  1323. if (!(result = isEqual(a[size], b[size], stack, thorough))) {
  1324. break;
  1325. }
  1326. }
  1327. }
  1328. return result;
  1329. }
  1330. var ctorA = a.constructor,
  1331. ctorB = b.constructor;
  1332. // non `Object` object instances with different constructors are not equal
  1333. if (ctorA != ctorB && !(
  1334. isFunction(ctorA) && ctorA instanceof ctorA &&
  1335. isFunction(ctorB) && ctorB instanceof ctorB
  1336. )) {
  1337. return false;
  1338. }
  1339. // deep compare objects
  1340. for (var prop in a) {
  1341. if (hasOwnProperty.call(a, prop)) {
  1342. // count the number of properties.
  1343. size++;
  1344. // deep compare each property value.
  1345. if (!(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], stack, thorough))) {
  1346. return false;
  1347. }
  1348. }
  1349. }
  1350. // ensure both objects have the same number of properties
  1351. for (prop in b) {
  1352. // The JS engine in Adobe products, like InDesign, has a bug that causes
  1353. // `!size--` to throw an error so it must be wrapped in parentheses.
  1354. // https://github.com/documentcloud/underscore/issues/355
  1355. if (hasOwnProperty.call(b, prop) && !(size--)) {
  1356. // `size` will be `-1` if `b` has more properties than `a`
  1357. return false;
  1358. }
  1359. }
  1360. // handle JScript [[DontEnum]] bug
  1361. if (hasDontEnumBug) {
  1362. while (++index < 7) {
  1363. prop = shadowed[index];
  1364. if (hasOwnProperty.call(a, prop) &&
  1365. !(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], stack, thorough))) {
  1366. return false;
  1367. }
  1368. }
  1369. }
  1370. return true;
  1371. }
  1372. /**
  1373. * Checks if `value` is a finite number.
  1374. *
  1375. * Note: This is not the same as native `isFinite`, which will return true for
  1376. * booleans and other values. See http://es5.github.com/#x15.1.2.5.
  1377. *
  1378. * @deprecated
  1379. * @static
  1380. * @memberOf _
  1381. * @category Objects
  1382. * @param {Mixed} value The value to check.
  1383. * @returns {Boolean} Retur