PageRenderTime 73ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs//0.9.0/lodash.js

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