PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/lodash.js/2.0.0/lodash.underscore.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 1653 lines | 1190 code | 76 blank | 387 comment | 140 complexity | 81a50b9de39bf41b7d232eaf496e2e3c MD5 | raw file
  1. /**
  2. * @license
  3. * Lo-Dash 2.0.0 (Custom Build) <http://lodash.com/>
  4. * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js`
  5. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
  7. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. * Available under MIT license <http://lodash.com/license>
  9. */
  10. ;(function() {
  11. /** Used as a safe reference for `undefined` in pre ES5 environments */
  12. var undefined;
  13. /** Used to generate unique IDs */
  14. var idCounter = 0;
  15. /** Used internally to indicate various things */
  16. var indicatorObject = {};
  17. /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
  18. var keyPrefix = +new Date + '';
  19. /** Used to match "interpolate" template delimiters */
  20. var reInterpolate = /<%=([\s\S]+?)%>/g;
  21. /** Used to ensure capturing order of template delimiters */
  22. var reNoMatch = /($^)/;
  23. /** Used to match unescaped characters in compiled string literals */
  24. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  25. /** `Object#toString` result shortcuts */
  26. var argsClass = '[object Arguments]',
  27. arrayClass = '[object Array]',
  28. boolClass = '[object Boolean]',
  29. dateClass = '[object Date]',
  30. funcClass = '[object Function]',
  31. numberClass = '[object Number]',
  32. objectClass = '[object Object]',
  33. regexpClass = '[object RegExp]',
  34. stringClass = '[object String]';
  35. /** Used to determine if values are of the language type Object */
  36. var objectTypes = {
  37. 'boolean': false,
  38. 'function': true,
  39. 'object': true,
  40. 'number': false,
  41. 'string': false,
  42. 'undefined': false
  43. };
  44. /** Used to escape characters for inclusion in compiled string literals */
  45. var stringEscapes = {
  46. '\\': '\\',
  47. "'": "'",
  48. '\n': 'n',
  49. '\r': 'r',
  50. '\t': 't',
  51. '\u2028': 'u2028',
  52. '\u2029': 'u2029'
  53. };
  54. /** Used as a reference to the global object */
  55. var root = (objectTypes[typeof window] && window) || this;
  56. /** Detect free variable `exports` */
  57. var freeExports = objectTypes[typeof exports] && exports;
  58. /** Detect free variable `module` */
  59. var freeModule = objectTypes[typeof module] && module && module.exports == freeExports && module;
  60. /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
  61. var freeGlobal = objectTypes[typeof global] && global;
  62. if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
  63. root = freeGlobal;
  64. }
  65. /*--------------------------------------------------------------------------*/
  66. /**
  67. * The base implementation of `_.indexOf` without support for binary searches
  68. * or `fromIndex` constraints.
  69. *
  70. * @private
  71. * @param {Array} array The array to search.
  72. * @param {*} value The value to search for.
  73. * @param {number} [fromIndex=0] The index to search from.
  74. * @returns {number} Returns the index of the matched value or `-1`.
  75. */
  76. function baseIndexOf(array, value, fromIndex) {
  77. var index = (fromIndex || 0) - 1,
  78. length = array ? array.length : 0;
  79. while (++index < length) {
  80. if (array[index] === value) {
  81. return index;
  82. }
  83. }
  84. return -1;
  85. }
  86. /**
  87. * Used by `sortBy` to compare transformed `collection` elements, stable sorting
  88. * them in ascending order.
  89. *
  90. * @private
  91. * @param {Object} a The object to compare to `b`.
  92. * @param {Object} b The object to compare to `a`.
  93. * @returns {number} Returns the sort order indicator of `1` or `-1`.
  94. */
  95. function compareAscending(a, b) {
  96. var ac = a.criteria,
  97. bc = b.criteria;
  98. // ensure a stable sort in V8 and other engines
  99. // http://code.google.com/p/v8/issues/detail?id=90
  100. if (ac !== bc) {
  101. if (ac > bc || typeof ac == 'undefined') {
  102. return 1;
  103. }
  104. if (ac < bc || typeof bc == 'undefined') {
  105. return -1;
  106. }
  107. }
  108. // The JS engine embedded in Adobe applications like InDesign has a buggy
  109. // `Array#sort` implementation that causes it, under certain circumstances,
  110. // to return the same value for `a` and `b`.
  111. // See https://github.com/jashkenas/underscore/pull/1247
  112. return a.index - b.index;
  113. }
  114. /**
  115. * Used by `template` to escape characters for inclusion in compiled
  116. * string literals.
  117. *
  118. * @private
  119. * @param {string} match The matched character to escape.
  120. * @returns {string} Returns the escaped character.
  121. */
  122. function escapeStringChar(match) {
  123. return '\\' + stringEscapes[match];
  124. }
  125. /**
  126. * A no-operation function.
  127. *
  128. * @private
  129. */
  130. function noop() {
  131. // no operation performed
  132. }
  133. /*--------------------------------------------------------------------------*/
  134. /**
  135. * Used for `Array` method references.
  136. *
  137. * Normally `Array.prototype` would suffice, however, using an array literal
  138. * avoids issues in Narwhal.
  139. */
  140. var arrayRef = [];
  141. /** Used for native method references */
  142. var objectProto = Object.prototype;
  143. /** Used to restore the original `_` reference in `noConflict` */
  144. var oldDash = root._;
  145. /** Used to detect if a method is native */
  146. var reNative = RegExp('^' +
  147. String(objectProto.valueOf)
  148. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  149. .replace(/valueOf|for [^\]]+/g, '.+?') + '$'
  150. );
  151. /** Native method shortcuts */
  152. var ceil = Math.ceil,
  153. floor = Math.floor,
  154. hasOwnProperty = objectProto.hasOwnProperty,
  155. push = arrayRef.push,
  156. toString = objectProto.toString,
  157. unshift = arrayRef.unshift;
  158. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  159. var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind,
  160. nativeCreate = reNative.test(nativeCreate = Object.create) && nativeCreate,
  161. nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
  162. nativeIsFinite = root.isFinite,
  163. nativeIsNaN = root.isNaN,
  164. nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
  165. nativeMax = Math.max,
  166. nativeMin = Math.min,
  167. nativeRandom = Math.random,
  168. nativeSlice = arrayRef.slice;
  169. /** Detect various environments */
  170. var isIeOpera = reNative.test(root.attachEvent),
  171. isV8 = nativeBind && !/\n|true/.test(nativeBind + isIeOpera);
  172. /*--------------------------------------------------------------------------*/
  173. /**
  174. * Creates a `lodash` object which wraps the given value to enable method
  175. * chaining.
  176. *
  177. * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
  178. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
  179. * and `unshift`
  180. *
  181. * Chaining is supported in custom builds as long as the `value` method is
  182. * implicitly or explicitly included in the build.
  183. *
  184. * The chainable wrapper functions are:
  185. * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
  186. * `compose`, `concat`, `countBy`, `createCallback`, `curry`, `debounce`,
  187. * `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`, `forEach`,
  188. * `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `functions`,
  189. * `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, `invoke`, `keys`,
  190. * `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`, `once`, `pairs`,
  191. * `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`, `range`, `reject`,
  192. * `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
  193. * `tap`, `throttle`, `times`, `toArray`, `transform`, `union`, `uniq`, `unshift`,
  194. * `unzip`, `values`, `where`, `without`, `wrap`, and `zip`
  195. *
  196. * The non-chainable wrapper functions are:
  197. * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`,
  198. * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`,
  199. * `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
  200. * `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`,
  201. * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`,
  202. * `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`,
  203. * `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`,
  204. * `template`, `unescape`, `uniqueId`, and `value`
  205. *
  206. * The wrapper functions `first` and `last` return wrapped values when `n` is
  207. * provided, otherwise they return unwrapped values.
  208. *
  209. * @name _
  210. * @constructor
  211. * @category Chaining
  212. * @param {*} value The value to wrap in a `lodash` instance.
  213. * @returns {Object} Returns a `lodash` instance.
  214. * @example
  215. *
  216. * var wrapped = _([1, 2, 3]);
  217. *
  218. * // returns an unwrapped value
  219. * wrapped.reduce(function(sum, num) {
  220. * return sum + num;
  221. * });
  222. * // => 6
  223. *
  224. * // returns a wrapped value
  225. * var squares = wrapped.map(function(num) {
  226. * return num * num;
  227. * });
  228. *
  229. * _.isArray(squares);
  230. * // => false
  231. *
  232. * _.isArray(squares.value());
  233. * // => true
  234. */
  235. function lodash(value) {
  236. return (value instanceof lodash)
  237. ? value
  238. : new lodashWrapper(value);
  239. }
  240. /**
  241. * A fast path for creating `lodash` wrapper objects.
  242. *
  243. * @private
  244. * @param {*} value The value to wrap in a `lodash` instance.
  245. * @param {boolean} chainAll A flag to enable chaining for all methods
  246. * @returns {Object} Returns a `lodash` instance.
  247. */
  248. function lodashWrapper(value, chainAll) {
  249. this.__chain__ = !!chainAll;
  250. this.__wrapped__ = value;
  251. }
  252. // ensure `new lodashWrapper` is an instance of `lodash`
  253. lodashWrapper.prototype = lodash.prototype;
  254. /**
  255. * An object used to flag environments features.
  256. *
  257. * @static
  258. * @memberOf _
  259. * @type Object
  260. */
  261. var support = {};
  262. (function() {
  263. var object = { '0': 1, 'length': 1 };
  264. /**
  265. * Detect if `Function#bind` exists and is inferred to be fast (all but V8).
  266. *
  267. * @memberOf _.support
  268. * @type boolean
  269. */
  270. support.fastBind = nativeBind && !isV8;
  271. /**
  272. * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly.
  273. *
  274. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  275. * and `splice()` functions that fail to remove the last element, `value[0]`,
  276. * of array-like objects even though the `length` property is set to `0`.
  277. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  278. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  279. *
  280. * @memberOf _.support
  281. * @type boolean
  282. */
  283. support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]);
  284. }(1));
  285. /**
  286. * By default, the template delimiters used by Lo-Dash are similar to those in
  287. * embedded Ruby (ERB). Change the following template settings to use alternative
  288. * delimiters.
  289. *
  290. * @static
  291. * @memberOf _
  292. * @type Object
  293. */
  294. lodash.templateSettings = {
  295. /**
  296. * Used to detect `data` property values to be HTML-escaped.
  297. *
  298. * @memberOf _.templateSettings
  299. * @type RegExp
  300. */
  301. 'escape': /<%-([\s\S]+?)%>/g,
  302. /**
  303. * Used to detect code to be evaluated.
  304. *
  305. * @memberOf _.templateSettings
  306. * @type RegExp
  307. */
  308. 'evaluate': /<%([\s\S]+?)%>/g,
  309. /**
  310. * Used to detect `data` property values to inject.
  311. *
  312. * @memberOf _.templateSettings
  313. * @type RegExp
  314. */
  315. 'interpolate': reInterpolate,
  316. /**
  317. * Used to reference the data object in the template text.
  318. *
  319. * @memberOf _.templateSettings
  320. * @type string
  321. */
  322. 'variable': ''
  323. };
  324. /*--------------------------------------------------------------------------*/
  325. /**
  326. * The base implementation of `_.createCallback` without support for creating
  327. * "_.pluck" or "_.where" style callbacks.
  328. *
  329. * @private
  330. * @param {*} [func=identity] The value to convert to a callback.
  331. * @param {*} [thisArg] The `this` binding of the created callback.
  332. * @param {number} [argCount] The number of arguments the callback accepts.
  333. * @returns {Function} Returns a callback function.
  334. */
  335. function baseCreateCallback(func, thisArg, argCount) {
  336. if (typeof func != 'function') {
  337. return identity;
  338. }
  339. // exit early if there is no `thisArg`
  340. if (typeof thisArg == 'undefined') {
  341. return func;
  342. }
  343. switch (argCount) {
  344. case 1: return function(value) {
  345. return func.call(thisArg, value);
  346. };
  347. case 2: return function(a, b) {
  348. return func.call(thisArg, a, b);
  349. };
  350. case 3: return function(value, index, collection) {
  351. return func.call(thisArg, value, index, collection);
  352. };
  353. case 4: return function(accumulator, value, index, collection) {
  354. return func.call(thisArg, accumulator, value, index, collection);
  355. };
  356. }
  357. return bind(func, thisArg);
  358. }
  359. /**
  360. * The base implementation of `_.flatten` without support for callback
  361. * shorthands or `thisArg` binding.
  362. *
  363. * @private
  364. * @param {Array} array The array to flatten.
  365. * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
  366. * @param {boolean} [isArgArrays=false] A flag to restrict flattening to arrays and `arguments` objects.
  367. * @param {number} [fromIndex=0] The index to start from.
  368. * @returns {Array} Returns a new flattened array.
  369. */
  370. function baseFlatten(array, isShallow, isArgArrays, fromIndex) {
  371. var index = (fromIndex || 0) - 1,
  372. length = array ? array.length : 0,
  373. result = [];
  374. while (++index < length) {
  375. var value = array[index];
  376. // recursively flatten arrays (susceptible to call stack limits)
  377. if (value && typeof value == 'object' && (isArray(value) || isArguments(value))) {
  378. push.apply(result, isShallow ? value : baseFlatten(value, isShallow, isArgArrays));
  379. } else if (!isArgArrays) {
  380. result.push(value);
  381. }
  382. }
  383. return result;
  384. }
  385. /**
  386. * The base implementation of `_.isEqual`, without support for `thisArg` binding,
  387. * that allows partial "_.where" style comparisons.
  388. *
  389. * @private
  390. * @param {*} a The value to compare.
  391. * @param {*} b The other value to compare.
  392. * @param {Function} [callback] The function to customize comparing values.
  393. * @param {Function} [isWhere=false] A flag to indicate performing partial comparisons.
  394. * @param {Array} [stackA=[]] Tracks traversed `a` objects.
  395. * @param {Array} [stackB=[]] Tracks traversed `b` objects.
  396. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  397. */
  398. function baseIsEqual(a, b, stackA, stackB) {
  399. if (a === b) {
  400. return a !== 0 || (1 / a == 1 / b);
  401. }
  402. var type = typeof a,
  403. otherType = typeof b;
  404. if (a === a &&
  405. !(a && objectTypes[type]) &&
  406. !(b && objectTypes[otherType])) {
  407. return false;
  408. }
  409. if (a == null || b == null) {
  410. return a === b;
  411. }
  412. var className = toString.call(a),
  413. otherClass = toString.call(b);
  414. if (className != otherClass) {
  415. return false;
  416. }
  417. switch (className) {
  418. case boolClass:
  419. case dateClass:
  420. return +a == +b;
  421. case numberClass:
  422. return a != +a
  423. ? b != +b
  424. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  425. case regexpClass:
  426. case stringClass:
  427. return a == String(b);
  428. }
  429. var isArr = className == arrayClass;
  430. if (!isArr) {
  431. if (hasOwnProperty.call(a, '__wrapped__ ') || b instanceof lodash) {
  432. return baseIsEqual(a.__wrapped__ || a, b.__wrapped__ || b, stackA, stackB);
  433. }
  434. if (className != objectClass) {
  435. return false;
  436. }
  437. var ctorA = a.constructor,
  438. ctorB = b.constructor;
  439. if (ctorA != ctorB && !(
  440. isFunction(ctorA) && ctorA instanceof ctorA &&
  441. isFunction(ctorB) && ctorB instanceof ctorB
  442. )) {
  443. return false;
  444. }
  445. }
  446. stackA || (stackA = []);
  447. stackB || (stackB = []);
  448. var length = stackA.length;
  449. while (length--) {
  450. if (stackA[length] == a) {
  451. return stackB[length] == b;
  452. }
  453. }
  454. var result = true,
  455. size = 0;
  456. stackA.push(a);
  457. stackB.push(b);
  458. if (isArr) {
  459. size = b.length;
  460. result = size == a.length;
  461. if (result) {
  462. while (size--) {
  463. if (!(result = baseIsEqual(a[size], b[size], stackA, stackB))) {
  464. break;
  465. }
  466. }
  467. }
  468. return result;
  469. }
  470. forIn(b, function(value, key, b) {
  471. if (hasOwnProperty.call(b, key)) {
  472. size++;
  473. return !(result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, stackA, stackB)) && indicatorObject;
  474. }
  475. });
  476. if (result) {
  477. forIn(a, function(value, key, a) {
  478. if (hasOwnProperty.call(a, key)) {
  479. return !(result = --size > -1) && indicatorObject;
  480. }
  481. });
  482. }
  483. return result;
  484. }
  485. /**
  486. * The base implementation of `_.uniq` without support for callback shorthands
  487. * or `thisArg` binding.
  488. *
  489. * @private
  490. * @param {Array} array The array to process.
  491. * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
  492. * @param {Function} [callback] The function called per iteration.
  493. * @returns {Array} Returns a duplicate-value-free array.
  494. */
  495. function baseUniq(array, isSorted, callback) {
  496. var index = -1,
  497. indexOf = getIndexOf(),
  498. length = array ? array.length : 0,
  499. result = [],
  500. seen = callback ? [] : result;
  501. while (++index < length) {
  502. var value = array[index],
  503. computed = callback ? callback(value, index, array) : value;
  504. if (isSorted
  505. ? !index || seen[seen.length - 1] !== computed
  506. : indexOf(seen, computed) < 0
  507. ) {
  508. if (callback) {
  509. seen.push(computed);
  510. }
  511. result.push(value);
  512. }
  513. }
  514. return result;
  515. }
  516. /**
  517. * Creates a function that aggregates a collection, creating an object composed
  518. * of keys generated from the results of running each element of the collection
  519. * through a callback. The given `setter` function sets the keys and values
  520. * of the composed object.
  521. *
  522. * @private
  523. * @param {Function} setter The setter function.
  524. * @returns {Function} Returns the new aggregator function.
  525. */
  526. function createAggregator(setter) {
  527. return function(collection, callback, thisArg) {
  528. var result = {};
  529. callback = createCallback(callback, thisArg, 3);
  530. var index = -1,
  531. length = collection ? collection.length : 0;
  532. if (typeof length == 'number') {
  533. while (++index < length) {
  534. var value = collection[index];
  535. setter(result, value, callback(value, index, collection), collection);
  536. }
  537. } else {
  538. forOwn(collection, function(value, key, collection) {
  539. setter(result, value, callback(value, key, collection), collection);
  540. });
  541. }
  542. return result;
  543. };
  544. }
  545. /**
  546. * Creates a function that, when called, either curries or invokes `func`
  547. * with an optional `this` binding and partially applied arguments.
  548. *
  549. * @private
  550. * @param {Function|string} func The function or method name to reference.
  551. * @param {number} bitmask The bitmask of method flags to compose.
  552. * The bitmask may be composed of the following flags:
  553. * 1 - `_.bind`
  554. * 2 - `_.bindKey`
  555. * 4 - `_.curry`
  556. * 8 - `_.curry` (bound)
  557. * 16 - `_.partial`
  558. * 32 - `_.partialRight`
  559. * @param {Array} [partialArgs] An array of arguments to prepend to those
  560. * provided to the new function.
  561. * @param {Array} [partialRightArgs] An array of arguments to append to those
  562. * provided to the new function.
  563. * @param {*} [thisArg] The `this` binding of `func`.
  564. * @param {number} [arity] The arity of `func`.
  565. * @returns {Function} Returns the new bound function.
  566. */
  567. function createBound(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
  568. var isBind = bitmask & 1,
  569. isBindKey = bitmask & 2,
  570. isCurry = bitmask & 4,
  571. isCurryBound = bitmask & 8,
  572. isPartial = bitmask & 16,
  573. isPartialRight = bitmask & 32,
  574. key = func;
  575. if (!isBindKey && !isFunction(func)) {
  576. throw new TypeError;
  577. }
  578. if (isPartial && !partialArgs.length) {
  579. bitmask &= ~16;
  580. isPartial = partialArgs = false;
  581. }
  582. if (isPartialRight && !partialRightArgs.length) {
  583. bitmask &= ~32;
  584. isPartialRight = partialRightArgs = false;
  585. }
  586. // use `Function#bind` if it exists and is fast
  587. // (in V8 `Function#bind` is slower except when partially applied)
  588. if (isBind && !(isBindKey || isCurry || isPartialRight) &&
  589. (support.fastBind || (nativeBind && isPartial))) {
  590. if (isPartial) {
  591. var args = [thisArg];
  592. push.apply(args, partialArgs);
  593. }
  594. var bound = isPartial
  595. ? nativeBind.apply(func, args)
  596. : nativeBind.call(func, thisArg);
  597. }
  598. else {
  599. bound = function() {
  600. // `Function#bind` spec
  601. // http://es5.github.io/#x15.3.4.5
  602. var args = arguments,
  603. thisBinding = isBind ? thisArg : this;
  604. if (isCurry || isPartial || isPartialRight) {
  605. args = nativeSlice.call(args);
  606. if (isPartial) {
  607. unshift.apply(args, partialArgs);
  608. }
  609. if (isPartialRight) {
  610. push.apply(args, partialRightArgs);
  611. }
  612. if (isCurry && args.length < arity) {
  613. bitmask |= 16 & ~32;
  614. return createBound(func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity);
  615. }
  616. }
  617. if (isBindKey) {
  618. func = thisBinding[key];
  619. }
  620. if (this instanceof bound) {
  621. // ensure `new bound` is an instance of `func`
  622. thisBinding = createObject(func.prototype);
  623. // mimic the constructor's `return` behavior
  624. // http://es5.github.io/#x13.2.2
  625. var result = func.apply(thisBinding, args);
  626. return isObject(result) ? result : thisBinding;
  627. }
  628. return func.apply(thisBinding, args);
  629. };
  630. }
  631. return bound;
  632. }
  633. /**
  634. * Creates a new object with the specified `prototype`.
  635. *
  636. * @private
  637. * @param {Object} prototype The prototype object.
  638. * @returns {Object} Returns the new object.
  639. */
  640. function createObject(prototype) {
  641. return isObject(prototype) ? nativeCreate(prototype) : {};
  642. }
  643. // fallback for browsers without `Object.create`
  644. if (!nativeCreate) {
  645. createObject = function(prototype) {
  646. if (isObject(prototype)) {
  647. noop.prototype = prototype;
  648. var result = new noop;
  649. noop.prototype = null;
  650. }
  651. return result || {};
  652. };
  653. }
  654. /**
  655. * Used by `escape` to convert characters to HTML entities.
  656. *
  657. * @private
  658. * @param {string} match The matched character to escape.
  659. * @returns {string} Returns the escaped character.
  660. */
  661. function escapeHtmlChar(match) {
  662. return htmlEscapes[match];
  663. }
  664. /**
  665. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  666. * customized, this method returns the custom method, otherwise it returns
  667. * the `baseIndexOf` function.
  668. *
  669. * @private
  670. * @returns {Function} Returns the "indexOf" function.
  671. */
  672. function getIndexOf() {
  673. var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result;
  674. return result;
  675. }
  676. /**
  677. * Used by `unescape` to convert HTML entities to characters.
  678. *
  679. * @private
  680. * @param {string} match The matched character to unescape.
  681. * @returns {string} Returns the unescaped character.
  682. */
  683. function unescapeHtmlChar(match) {
  684. return htmlUnescapes[match];
  685. }
  686. /*--------------------------------------------------------------------------*/
  687. /**
  688. * Checks if `value` is an `arguments` object.
  689. *
  690. * @static
  691. * @memberOf _
  692. * @category Objects
  693. * @param {*} value The value to check.
  694. * @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  695. * @example
  696. *
  697. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  698. * // => true
  699. *
  700. * _.isArguments([1, 2, 3]);
  701. * // => false
  702. */
  703. function isArguments(value) {
  704. return (value && typeof value == 'object') ? toString.call(value) == argsClass : false;
  705. }
  706. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  707. if (!isArguments(arguments)) {
  708. isArguments = function(value) {
  709. return (value && typeof value == 'object') ? hasOwnProperty.call(value, 'callee') : false;
  710. };
  711. }
  712. /**
  713. * Checks if `value` is an array.
  714. *
  715. * @static
  716. * @memberOf _
  717. * @type Function
  718. * @category Objects
  719. * @param {*} value The value to check.
  720. * @returns {boolean} Returns `true` if the `value` is an array, else `false`.
  721. * @example
  722. *
  723. * (function() { return _.isArray(arguments); })();
  724. * // => false
  725. *
  726. * _.isArray([1, 2, 3]);
  727. * // => true
  728. */
  729. var isArray = nativeIsArray || function(value) {
  730. return (value && typeof value == 'object') ? toString.call(value) == arrayClass : false;
  731. };
  732. /**
  733. * A fallback implementation of `Object.keys` which produces an array of the
  734. * given object's own enumerable property names.
  735. *
  736. * @private
  737. * @type Function
  738. * @param {Object} object The object to inspect.
  739. * @returns {Array} Returns an array of property names.
  740. */
  741. var shimKeys = function(object) {
  742. var index, iterable = object, result = [];
  743. if (!iterable) return result;
  744. if (!(objectTypes[typeof object])) return result;
  745. for (index in iterable) {
  746. if (hasOwnProperty.call(iterable, index)) {
  747. result.push(index);
  748. }
  749. }
  750. return result
  751. };
  752. /**
  753. * Creates an array composed of the own enumerable property names of an object.
  754. *
  755. * @static
  756. * @memberOf _
  757. * @category Objects
  758. * @param {Object} object The object to inspect.
  759. * @returns {Array} Returns an array of property names.
  760. * @example
  761. *
  762. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  763. * // => ['one', 'two', 'three'] (property order is not guaranteed across environments)
  764. */
  765. var keys = !nativeKeys ? shimKeys : function(object) {
  766. if (!isObject(object)) {
  767. return [];
  768. }
  769. return nativeKeys(object);
  770. };
  771. /**
  772. * Used to convert characters to HTML entities:
  773. *
  774. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  775. * don't require escaping in HTML and have no special meaning unless they're part
  776. * of a tag or an unquoted attribute value.
  777. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  778. */
  779. var htmlEscapes = {
  780. '&': '&amp;',
  781. '<': '&lt;',
  782. '>': '&gt;',
  783. '"': '&quot;',
  784. "'": '&#x27;'
  785. };
  786. /** Used to convert HTML entities to characters */
  787. var htmlUnescapes = invert(htmlEscapes);
  788. /** Used to match HTML entities and HTML characters */
  789. var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'),
  790. reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g');
  791. /*--------------------------------------------------------------------------*/
  792. /**
  793. * Assigns own enumerable properties of source object(s) to the destination
  794. * object. Subsequent sources will overwrite property assignments of previous
  795. * sources. If a callback is provided it will be executed to produce the
  796. * assigned values. The callback is bound to `thisArg` and invoked with two
  797. * arguments; (objectValue, sourceValue).
  798. *
  799. * @static
  800. * @memberOf _
  801. * @type Function
  802. * @alias extend
  803. * @category Objects
  804. * @param {Object} object The destination object.
  805. * @param {...Object} [source] The source objects.
  806. * @param {Function} [callback] The function to customize assigning values.
  807. * @param {*} [thisArg] The `this` binding of `callback`.
  808. * @returns {Object} Returns the destination object.
  809. * @example
  810. *
  811. * _.assign({ 'name': 'moe' }, { 'age': 40 });
  812. * // => { 'name': 'moe', 'age': 40 }
  813. *
  814. * var defaults = _.partialRight(_.assign, function(a, b) {
  815. * return typeof a == 'undefined' ? b : a;
  816. * });
  817. *
  818. * var food = { 'name': 'apple' };
  819. * defaults(food, { 'name': 'banana', 'type': 'fruit' });
  820. * // => { 'name': 'apple', 'type': 'fruit' }
  821. */
  822. function assign(object) {
  823. if (!object) {
  824. return object;
  825. }
  826. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  827. var iterable = arguments[argsIndex];
  828. if (iterable) {
  829. for (var key in iterable) {
  830. object[key] = iterable[key];
  831. }
  832. }
  833. }
  834. return object;
  835. }
  836. /**
  837. * Creates a clone of `value`. If `deep` is `true` nested objects will also
  838. * be cloned, otherwise they will be assigned by reference. If a callback
  839. * is provided it will be executed to produce the cloned values. If the
  840. * callback returns `undefined` cloning will be handled by the method instead.
  841. * The callback is bound to `thisArg` and invoked with one argument; (value).
  842. *
  843. * @static
  844. * @memberOf _
  845. * @category Objects
  846. * @param {*} value The value to clone.
  847. * @param {boolean} [deep=false] A flag to indicate a deep clone.
  848. * @param {Function} [callback] The function to customize cloning values.
  849. * @param {*} [thisArg] The `this` binding of `callback`.
  850. * @returns {*} Returns the cloned `value`.
  851. * @example
  852. *
  853. * var stooges = [
  854. * { 'name': 'moe', 'age': 40 },
  855. * { 'name': 'larry', 'age': 50 }
  856. * ];
  857. *
  858. * var shallow = _.clone(stooges);
  859. * shallow[0] === stooges[0];
  860. * // => true
  861. *
  862. * var deep = _.clone(stooges, true);
  863. * deep[0] === stooges[0];
  864. * // => false
  865. *
  866. * _.mixin({
  867. * 'clone': _.partialRight(_.clone, function(value) {
  868. * return _.isElement(value) ? value.cloneNode(false) : undefined;
  869. * })
  870. * });
  871. *
  872. * var clone = _.clone(document.body);
  873. * clone.childNodes.length;
  874. * // => 0
  875. */
  876. function clone(value) {
  877. return isObject(value)
  878. ? (isArray(value) ? nativeSlice.call(value) : assign({}, value))
  879. : value;
  880. }
  881. /**
  882. * Assigns own enumerable properties of source object(s) to the destination
  883. * object for all destination properties that resolve to `undefined`. Once a
  884. * property is set, additional defaults of the same property will be ignored.
  885. *
  886. * @static
  887. * @memberOf _
  888. * @type Function
  889. * @category Objects
  890. * @param {Object} object The destination object.
  891. * @param {...Object} [source] The source objects.
  892. * @param- {Object} [guard] Allows working with `_.reduce` without using its
  893. * `key` and `object` arguments as sources.
  894. * @returns {Object} Returns the destination object.
  895. * @example
  896. *
  897. * var food = { 'name': 'apple' };
  898. * _.defaults(food, { 'name': 'banana', 'type': 'fruit' });
  899. * // => { 'name': 'apple', 'type': 'fruit' }
  900. */
  901. function defaults(object) {
  902. if (!object) {
  903. return object;
  904. }
  905. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  906. var iterable = arguments[argsIndex];
  907. if (iterable) {
  908. for (var key in iterable) {
  909. if (typeof object[key] == 'undefined') {
  910. object[key] = iterable[key];
  911. }
  912. }
  913. }
  914. }
  915. return object;
  916. }
  917. /**
  918. * Iterates over own and inherited enumerable properties of an object,
  919. * executing the callback for each property. The callback is bound to `thisArg`
  920. * and invoked with three arguments; (value, key, object). Callbacks may exit
  921. * iteration early by explicitly returning `false`.
  922. *
  923. * @static
  924. * @memberOf _
  925. * @type Function
  926. * @category Objects
  927. * @param {Object} object The object to iterate over.
  928. * @param {Function} [callback=identity] The function called per iteration.
  929. * @param {*} [thisArg] The `this` binding of `callback`.
  930. * @returns {Object} Returns `object`.
  931. * @example
  932. *
  933. * function Dog(name) {
  934. * this.name = name;
  935. * }
  936. *
  937. * Dog.prototype.bark = function() {
  938. * console.log('Woof, woof!');
  939. * };
  940. *
  941. * _.forIn(new Dog('Dagny'), function(value, key) {
  942. * console.log(key);
  943. * });
  944. * // => logs 'bark' and 'name' (property order is not guaranteed across environments)
  945. */
  946. var forIn = function(collection, callback) {
  947. var index, iterable = collection, result = iterable;
  948. if (!iterable) return result;
  949. if (!objectTypes[typeof iterable]) return result;
  950. for (index in iterable) {
  951. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  952. }
  953. return result
  954. };
  955. /**
  956. * Iterates over own enumerable properties of an object, executing the callback
  957. * for each property. The callback is bound to `thisArg` and invoked with three
  958. * arguments; (value, key, object). Callbacks may exit iteration early by
  959. * explicitly returning `false`.
  960. *
  961. * @static
  962. * @memberOf _
  963. * @type Function
  964. * @category Objects
  965. * @param {Object} object The object to iterate over.
  966. * @param {Function} [callback=identity] The function called per iteration.
  967. * @param {*} [thisArg] The `this` binding of `callback`.
  968. * @returns {Object} Returns `object`.
  969. * @example
  970. *
  971. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  972. * console.log(key);
  973. * });
  974. * // => logs '0', '1', and 'length' (property order is not guaranteed across environments)
  975. */
  976. var forOwn = function(collection, callback) {
  977. var index, iterable = collection, result = iterable;
  978. if (!iterable) return result;
  979. if (!objectTypes[typeof iterable]) return result;
  980. for (index in iterable) {
  981. if (hasOwnProperty.call(iterable, index)) {
  982. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  983. }
  984. }
  985. return result
  986. };
  987. /**
  988. * Creates a sorted array of property names of all enumerable properties,
  989. * own and inherited, of `object` that have function values.
  990. *
  991. * @static
  992. * @memberOf _
  993. * @alias methods
  994. * @category Objects
  995. * @param {Object} object The object to inspect.
  996. * @returns {Array} Returns an array of property names that have function values.
  997. * @example
  998. *
  999. * _.functions(_);
  1000. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1001. */
  1002. function functions(object) {
  1003. var result = [];
  1004. forIn(object, function(value, key) {
  1005. if (isFunction(value)) {
  1006. result.push(key);
  1007. }
  1008. });
  1009. return result.sort();
  1010. }
  1011. /**
  1012. * Checks if the specified object `property` exists and is a direct property,
  1013. * instead of an inherited property.
  1014. *
  1015. * @static
  1016. * @memberOf _
  1017. * @category Objects
  1018. * @param {Object} object The object to check.
  1019. * @param {string} property The property to check for.
  1020. * @returns {boolean} Returns `true` if key is a direct property, else `false`.
  1021. * @example
  1022. *
  1023. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1024. * // => true
  1025. */
  1026. function has(object, property) {
  1027. return object ? hasOwnProperty.call(object, property) : false;
  1028. }
  1029. /**
  1030. * Creates an object composed of the inverted keys and values of the given object.
  1031. *
  1032. * @static
  1033. * @memberOf _
  1034. * @category Objects
  1035. * @param {Object} object The object to invert.
  1036. * @returns {Object} Returns the created inverted object.
  1037. * @example
  1038. *
  1039. * _.invert({ 'first': 'moe', 'second': 'larry' });
  1040. * // => { 'moe': 'first', 'larry': 'second' }
  1041. */
  1042. function invert(object) {
  1043. var index = -1,
  1044. props = keys(object),
  1045. length = props.length,
  1046. result = {};
  1047. while (++index < length) {
  1048. var key = props[index];
  1049. result[object[key]] = key;
  1050. }
  1051. return result;
  1052. }
  1053. /**
  1054. * Checks if `value` is a boolean value.
  1055. *
  1056. * @static
  1057. * @memberOf _
  1058. * @category Objects
  1059. * @param {*} value The value to check.
  1060. * @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1061. * @example
  1062. *
  1063. * _.isBoolean(null);
  1064. * // => false
  1065. */
  1066. function isBoolean(value) {
  1067. return value === true || value === false || toString.call(value) == boolClass;
  1068. }
  1069. /**
  1070. * Checks if `value` is a date.
  1071. *
  1072. * @static
  1073. * @memberOf _
  1074. * @category Objects
  1075. * @param {*} value The value to check.
  1076. * @returns {boolean} Returns `true` if the `value` is a date, else `false`.
  1077. * @example
  1078. *
  1079. * _.isDate(new Date);
  1080. * // => true
  1081. */
  1082. function isDate(value) {
  1083. return value ? (typeof value == 'object' && toString.call(value) == dateClass) : false;
  1084. }
  1085. /**
  1086. * Checks if `value` is a DOM element.
  1087. *
  1088. * @static
  1089. * @memberOf _
  1090. * @category Objects
  1091. * @param {*} value The value to check.
  1092. * @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1093. * @example
  1094. *
  1095. * _.isElement(document.body);
  1096. * // => true
  1097. */
  1098. function isElement(value) {
  1099. return value ? value.nodeType === 1 : false;
  1100. }
  1101. /**
  1102. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1103. * length of `0` and objects with no own enumerable properties are considered
  1104. * "empty".
  1105. *
  1106. * @static
  1107. * @memberOf _
  1108. * @category Objects
  1109. * @param {Array|Object|string} value The value to inspect.
  1110. * @returns {boolean} Returns `true` if the `value` is empty, else `false`.
  1111. * @example
  1112. *
  1113. * _.isEmpty([1, 2, 3]);
  1114. * // => false
  1115. *
  1116. * _.isEmpty({});
  1117. * // => true
  1118. *
  1119. * _.isEmpty('');
  1120. * // => true
  1121. */
  1122. function isEmpty(value) {
  1123. if (!value) {
  1124. return true;
  1125. }
  1126. if (isArray(value) || isString(value)) {
  1127. return !value.length;
  1128. }
  1129. for (var key in value) {
  1130. if (hasOwnProperty.call(value, key)) {
  1131. return false;
  1132. }
  1133. }
  1134. return true;
  1135. }
  1136. /**
  1137. * Performs a deep comparison between two values to determine if they are
  1138. * equivalent to each other. If a callback is provided it will be executed
  1139. * to compare values. If the callback returns `undefined` comparisons will
  1140. * be handled by the method instead. The callback is bound to `thisArg` and
  1141. * invoked with two arguments; (a, b).
  1142. *
  1143. * @static
  1144. * @memberOf _
  1145. * @category Objects
  1146. * @param {*} a The value to compare.
  1147. * @param {*} b The other value to compare.
  1148. * @param {Function} [callback] The function to customize comparing values.
  1149. * @param {*} [thisArg] The `this` binding of `callback`.
  1150. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1151. * @example
  1152. *
  1153. * var moe = { 'name': 'moe', 'age': 40 };
  1154. * var copy = { 'name': 'moe', 'age': 40 };
  1155. *
  1156. * moe == copy;
  1157. * // => false
  1158. *
  1159. * _.isEqual(moe, copy);
  1160. * // => true
  1161. *
  1162. * var words = ['hello', 'goodbye'];
  1163. * var otherWords = ['hi', 'goodbye'];
  1164. *
  1165. * _.isEqual(words, otherWords, function(a, b) {
  1166. * var reGreet = /^(?:hello|hi)$/i,
  1167. * aGreet = _.isString(a) && reGreet.test(a),
  1168. * bGreet = _.isString(b) && reGreet.test(b);
  1169. *
  1170. * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
  1171. * });
  1172. * // => true
  1173. */
  1174. function isEqual(a, b) {
  1175. return baseIsEqual(a, b);
  1176. }
  1177. /**
  1178. * Checks if `value` is, or can be coerced to, a finite number.
  1179. *
  1180. * Note: This is not the same as native `isFinite` which will return true for
  1181. * booleans and empty strings. See http://es5.github.io/#x15.1.2.5.
  1182. *
  1183. * @static
  1184. * @memberOf _
  1185. * @category Objects
  1186. * @param {*} value The value to check.
  1187. * @returns {boolean} Returns `true` if the `value` is finite, else `false`.
  1188. * @example
  1189. *
  1190. * _.isFinite(-101);
  1191. * // => true
  1192. *
  1193. * _.isFinite('10');
  1194. * // => true
  1195. *
  1196. * _.isFinite(true);
  1197. * // => false
  1198. *
  1199. * _.isFinite('');
  1200. * // => false
  1201. *
  1202. * _.isFinite(Infinity);
  1203. * // => false
  1204. */
  1205. function isFinite(value) {
  1206. return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
  1207. }
  1208. /**
  1209. * Checks if `value` is a function.
  1210. *
  1211. * @static
  1212. * @memberOf _
  1213. * @category Objects
  1214. * @param {*} value The value to check.
  1215. * @returns {boolean} Returns `true` if the `value` is a function, else `false`.
  1216. * @example
  1217. *
  1218. * _.isFunction(_);
  1219. * // => true
  1220. */
  1221. function isFunction(value) {
  1222. return typeof value == 'function';
  1223. }
  1224. // fallback for older versions of Chrome and Safari
  1225. if (isFunction(/x/)) {
  1226. isFunction = function(value) {
  1227. return typeof value == 'function' && toString.call(value) == funcClass;
  1228. };
  1229. }
  1230. /**
  1231. * Checks if `value` is the language type of Object.
  1232. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1233. *
  1234. * @static
  1235. * @memberOf _
  1236. * @category Objects
  1237. * @param {*} value The value to check.
  1238. * @returns {boolean} Returns `true` if the `value` is an object, else `false`.
  1239. * @example
  1240. *
  1241. * _.isObject({});
  1242. * // => true
  1243. *
  1244. * _.isObject([1, 2, 3]);
  1245. * // => true
  1246. *
  1247. * _.isObject(1);
  1248. * // => false
  1249. */
  1250. function isObject(value) {
  1251. // check if the value is the ECMAScript language type of Object
  1252. // http://es5.github.io/#x8
  1253. // and avoid a V8 bug
  1254. // http://code.google.com/p/v8/issues/detail?id=2291
  1255. return !!(value && objectTypes[typeof value]);
  1256. }
  1257. /**
  1258. * Checks if `value` is `NaN`.
  1259. *
  1260. * Note: This is not the same as native `isNaN` which will return `true` for
  1261. * `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4.
  1262. *
  1263. * @static
  1264. * @memberOf _
  1265. * @category Objects
  1266. * @param {*} value The value to check.
  1267. * @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`.
  1268. * @example
  1269. *
  1270. * _.isNaN(NaN);
  1271. * // => true
  1272. *
  1273. * _.isNaN(new Number(NaN));
  1274. * // => true
  1275. *
  1276. * isNaN(undefined);
  1277. * // => true
  1278. *
  1279. * _.isNaN(undefined);
  1280. * // => false
  1281. */
  1282. function isNaN(value) {
  1283. // `NaN` as a primitive is the only value that is not equal to itself
  1284. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  1285. return isNumber(value) && value != +value;
  1286. }
  1287. /**
  1288. * Checks if `value` is `null`.
  1289. *
  1290. * @static
  1291. * @memberOf _
  1292. * @category Objects
  1293. * @param {*} value The value to check.
  1294. * @returns {boolean} Returns `true` if the `value` is `null`, else `false`.
  1295. * @example
  1296. *
  1297. * _.isNull(null);
  1298. * // => true
  1299. *
  1300. * _.isNull(undefined);
  1301. * // => false
  1302. */
  1303. function isNull(value) {
  1304. return value === null;
  1305. }
  1306. /**
  1307. * Checks if `value` is a number.
  1308. *
  1309. * Note: `NaN` is considered a number. See http://es5.github.io/#x8.5.
  1310. *
  1311. * @static
  1312. * @memberOf _
  1313. * @category Objects
  1314. * @param {*} value The value to check.
  1315. * @returns {boolean} Returns `true` if the `value` is a number, else `false`.
  1316. * @example
  1317. *
  1318. * _.isNumber(8.4 * 5);
  1319. * // => true
  1320. */
  1321. function isNumber(value) {
  1322. return typeof value == 'number' || toString.call(value) == numberClass;
  1323. }
  1324. /**
  1325. * Checks if `value` is a regular expression.
  1326. *
  1327. * @static
  1328. * @memberOf _
  1329. * @category Objects
  1330. * @param {*} value The value to check.
  1331. * @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`.
  1332. * @example
  1333. *
  1334. * _.isRegExp(/moe/);
  1335. * // => true
  1336. */
  1337. function isRegExp(value) {
  1338. return (value && objectTypes[typeof value]) ? toString.call(value) == regexpClass : false;
  1339. }
  1340. /**
  1341. * Checks if `value` is a string.
  1342. *
  1343. * @static
  1344. * @memberOf _
  1345. * @category Objects
  1346. * @param {*} value The value to check.
  1347. * @returns {boolean} Returns `true` if the `value` is a string, else `false`.
  1348. * @example
  1349. *
  1350. * _.isString('moe');
  1351. * // => true
  1352. */
  1353. function isString(value) {
  1354. return typeof value == 'string' || toString.call(value) == stringClass;
  1355. }
  1356. /**
  1357. * Checks if `value` is `undefined`.
  1358. *
  1359. * @static
  1360. * @memberOf _
  1361. * @category Objects
  1362. * @param {*} value The value to check.
  1363. * @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`.
  1364. * @example
  1365. *
  1366. * _.isUndefined(void 0);
  1367. * // => true
  1368. */
  1369. function isUndefined(value) {
  1370. return typeof value == 'undefined';
  1371. }
  1372. /**
  1373. * Creates a shallow clone of `object` excluding the specified properties.
  1374. * Property names may be specified as individual arguments or as arrays of
  1375. * property names. If a callback is provided it will be executed for each
  1376. * property of `object` omitting the properties the callback returns truey
  1377. * for. The callback is bound to `thisArg` and invoked with three arguments;
  1378. * (value, key, object).
  1379. *
  1380. * @static
  1381. * @memberOf _
  1382. * @category Objects
  1383. * @param {Object} object The source object.
  1384. * @param {Function|...string|string[]} [callback] The properties to omit or the
  1385. * function called per iteration.
  1386. * @param {*} [thisArg] The `this` binding of `callback`.
  1387. * @returns {Object} Returns an object without the omitted properties.
  1388. * @example
  1389. *
  1390. * _.omit({ 'name': 'moe', 'age': 40 }, 'age');
  1391. * // => { 'name': 'moe' }
  1392. *
  1393. * _.omit({ 'name': 'moe', 'age': 40 }, function(value) {
  1394. * return typeof value == 'number';
  1395. * });
  1396. * // => { 'name': 'moe' }
  1397. */
  1398. function omit(object) {
  1399. var indexOf = getIndexOf(),
  1400. props = baseFlatten(arguments, true, false, 1),
  1401. result = {};
  1402. forIn(object, function(value, key) {
  1403. if (indexOf(props, key) < 0) {
  1404. result[key] = value;
  1405. }
  1406. });
  1407. return result;
  1408. }
  1409. /**
  1410. * Creates a two dimensional array of an object's key-value pairs,
  1411. * i.e. `[[key1, value1], [key2, value2]]`.
  1412. *
  1413. * @static
  1414. * @memberOf _
  1415. * @category Objects
  1416. * @param {Object} object The object to inspect.
  1417. * @returns {Array} Returns new array of key-value pairs.
  1418. * @example
  1419. *
  1420. * _.pairs({ 'moe': 30, 'larry': 40 });
  1421. * // => [['moe', 30], ['larry', 40]] (property order is not guaranteed across environments)
  1422. */
  1423. function pairs(object) {
  1424. var index = -1,
  1425. props = keys(object),
  1426. length = props.length,
  1427. result = Array(length);
  1428. while (++index < length) {
  1429. var key = props[index];
  1430. result[index] = [key, object[key]];
  1431. }
  1432. return result;
  1433. }
  1434. /**
  1435. * Creates a shallow clone of `object` composed of the specified properties.
  1436. * Property names may be specified as individual arguments or as arrays of
  1437. * property names. If a callback is provided it will be executed for each
  1438. * property of `object` picking the properties the callback returns truey
  1439. * for. The callback is bound to `thisArg` and invoked with three arguments;
  1440. * (value, key, object).
  1441. *
  1442. * @static
  1443. * @memberOf _
  1444. * @category Objects
  1445. * @param {Object} object The source object.
  1446. * @param {Function|...string|string[]} [callback] The function called per
  1447. * iteration or property names to pick, specified as individual property
  1448. * names or arrays of property names.
  1449. * @param {*} [thisArg] The `this` binding of `callback`.
  1450. * @returns {Object} Returns an object composed of the picked properties.
  1451. * @example
  1452. *
  1453. * _.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name');
  1454. * // => { 'name': 'moe' }
  1455. *
  1456. * _.pick({ 'name': 'moe', '_userid': 'moe1' }, function(value, key) {
  1457. * return key.charAt(0) != '_';
  1458. * });
  1459. * // => { 'name': 'moe' }
  1460. */
  1461. function pick(object) {
  1462. var index = -1,
  1463. props = baseFlatten(arguments, true, false, 1),
  1464. length = props.length,
  1465. result = {};
  1466. while (++index < length) {
  1467. var prop = props[index];
  1468. if (prop in object) {
  1469. result[prop] = object[prop];
  1470. }
  1471. }
  1472. return result;
  1473. }
  1474. /**
  1475. * Creates an array composed of the own enumerable property values of `object`.
  1476. *
  1477. * @static
  1478. * @memberOf _
  1479. * @category Objects
  1480. * @param {Object} object The object to inspect.
  1481. * @returns {Array} Returns an array of property values.
  1482. * @example
  1483. *
  1484. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  1485. * // => [1, 2, 3] (property order is not guaranteed across environments)
  1486. */
  1487. function values(object) {
  1488. var index = -1,
  1489. props = keys(object),
  1490. length = props.length,
  1491. result = Array(length);
  1492. while (++index < length) {
  1493. result[index] = object[props[index]];
  1494. }
  1495. return result;
  1496. }
  1497. /*--------------------------------------------------------------------------*/
  1498. /**
  1499. * Checks if a given value is present in a collection using strict equality
  1500. * for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the
  1501. * offset from the end of the collection.
  1502. *
  1503. * @static
  1504. * @memberOf _
  1505. * @alias include
  1506. * @category Collections
  1507. * @param {Array|Object|string} collection The collection to iterate over.
  1508. * @param {*} target The value to check for.
  1509. * @param {number} [fromIndex=0] The index to search from.
  1510. * @returns {boolean} Returns `true` if the `target` element is found, else `false`.
  1511. * @example
  1512. *
  1513. * _.contains([1, 2, 3], 1);
  1514. * // => true
  1515. *
  1516. * _.contains([1, 2, 3], 1, 2);
  1517. * // => false
  1518. *
  1519. * _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
  1520. * // => true
  1521. *
  1522. * _.contains('curly', 'ur');
  1523. * // => true
  1524. */
  1525. function contains(collection, target) {
  1526. var indexOf = getIndexOf(),
  1527. length = collection ? collection.length : 0,
  1528. result = false;
  1529. if (length && typeof length == 'number') {
  1530. result = indexOf(collection, target) > -1;
  1531. } else {
  1532. forOwn(collection, function(value) {
  1533. return (result = value === target) && indicatorObject;
  1534. });
  1535. }
  1536. return result;
  1537. }
  1538. /**
  1539. * Creates an object composed of keys generated from the results of running
  1540. * each element of `collection` through the callback. The corresponding value