PageRenderTime 76ms CodeModel.GetById 1ms RepoModel.GetById 8ms app.codeStats 0ms

/ajax/libs//0.9.1/lodash.js

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