PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs//0.8.1/lodash.js

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