You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. /** Used as references for various `Number` constants. */
  10. var INFINITY = 1 / 0,
  11. MAX_SAFE_INTEGER = 9007199254740991;
  12. /** `Object#toString` result references. */
  13. var argsTag = '[object Arguments]',
  14. funcTag = '[object Function]',
  15. genTag = '[object GeneratorFunction]',
  16. symbolTag = '[object Symbol]';
  17. /** Detect free variable `global` from Node.js. */
  18. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  19. /** Detect free variable `self`. */
  20. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  21. /** Used as a reference to the global object. */
  22. var root = freeGlobal || freeSelf || Function('return this')();
  23. /**
  24. * A faster alternative to `Function#apply`, this function invokes `func`
  25. * with the `this` binding of `thisArg` and the arguments of `args`.
  26. *
  27. * @private
  28. * @param {Function} func The function to invoke.
  29. * @param {*} thisArg The `this` binding of `func`.
  30. * @param {Array} args The arguments to invoke `func` with.
  31. * @returns {*} Returns the result of `func`.
  32. */
  33. function apply(func, thisArg, args) {
  34. switch (args.length) {
  35. case 0: return func.call(thisArg);
  36. case 1: return func.call(thisArg, args[0]);
  37. case 2: return func.call(thisArg, args[0], args[1]);
  38. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  39. }
  40. return func.apply(thisArg, args);
  41. }
  42. /**
  43. * A specialized version of `_.map` for arrays without support for iteratee
  44. * shorthands.
  45. *
  46. * @private
  47. * @param {Array} [array] The array to iterate over.
  48. * @param {Function} iteratee The function invoked per iteration.
  49. * @returns {Array} Returns the new mapped array.
  50. */
  51. function arrayMap(array, iteratee) {
  52. var index = -1,
  53. length = array ? array.length : 0,
  54. result = Array(length);
  55. while (++index < length) {
  56. result[index] = iteratee(array[index], index, array);
  57. }
  58. return result;
  59. }
  60. /**
  61. * Appends the elements of `values` to `array`.
  62. *
  63. * @private
  64. * @param {Array} array The array to modify.
  65. * @param {Array} values The values to append.
  66. * @returns {Array} Returns `array`.
  67. */
  68. function arrayPush(array, values) {
  69. var index = -1,
  70. length = values.length,
  71. offset = array.length;
  72. while (++index < length) {
  73. array[offset + index] = values[index];
  74. }
  75. return array;
  76. }
  77. /** Used for built-in method references. */
  78. var objectProto = Object.prototype;
  79. /** Used to check objects for own properties. */
  80. var hasOwnProperty = objectProto.hasOwnProperty;
  81. /**
  82. * Used to resolve the
  83. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  84. * of values.
  85. */
  86. var objectToString = objectProto.toString;
  87. /** Built-in value references. */
  88. var Symbol = root.Symbol,
  89. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  90. spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
  91. /* Built-in method references for those with the same name as other `lodash` methods. */
  92. var nativeMax = Math.max;
  93. /**
  94. * The base implementation of `_.flatten` with support for restricting flattening.
  95. *
  96. * @private
  97. * @param {Array} array The array to flatten.
  98. * @param {number} depth The maximum recursion depth.
  99. * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
  100. * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
  101. * @param {Array} [result=[]] The initial result value.
  102. * @returns {Array} Returns the new flattened array.
  103. */
  104. function baseFlatten(array, depth, predicate, isStrict, result) {
  105. var index = -1,
  106. length = array.length;
  107. predicate || (predicate = isFlattenable);
  108. result || (result = []);
  109. while (++index < length) {
  110. var value = array[index];
  111. if (depth > 0 && predicate(value)) {
  112. if (depth > 1) {
  113. // Recursively flatten arrays (susceptible to call stack limits).
  114. baseFlatten(value, depth - 1, predicate, isStrict, result);
  115. } else {
  116. arrayPush(result, value);
  117. }
  118. } else if (!isStrict) {
  119. result[result.length] = value;
  120. }
  121. }
  122. return result;
  123. }
  124. /**
  125. * The base implementation of `_.pick` without support for individual
  126. * property identifiers.
  127. *
  128. * @private
  129. * @param {Object} object The source object.
  130. * @param {string[]} props The property identifiers to pick.
  131. * @returns {Object} Returns the new object.
  132. */
  133. function basePick(object, props) {
  134. object = Object(object);
  135. return basePickBy(object, props, function(value, key) {
  136. return key in object;
  137. });
  138. }
  139. /**
  140. * The base implementation of `_.pickBy` without support for iteratee shorthands.
  141. *
  142. * @private
  143. * @param {Object} object The source object.
  144. * @param {string[]} props The property identifiers to pick from.
  145. * @param {Function} predicate The function invoked per property.
  146. * @returns {Object} Returns the new object.
  147. */
  148. function basePickBy(object, props, predicate) {
  149. var index = -1,
  150. length = props.length,
  151. result = {};
  152. while (++index < length) {
  153. var key = props[index],
  154. value = object[key];
  155. if (predicate(value, key)) {
  156. result[key] = value;
  157. }
  158. }
  159. return result;
  160. }
  161. /**
  162. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  163. *
  164. * @private
  165. * @param {Function} func The function to apply a rest parameter to.
  166. * @param {number} [start=func.length-1] The start position of the rest parameter.
  167. * @returns {Function} Returns the new function.
  168. */
  169. function baseRest(func, start) {
  170. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  171. return function() {
  172. var args = arguments,
  173. index = -1,
  174. length = nativeMax(args.length - start, 0),
  175. array = Array(length);
  176. while (++index < length) {
  177. array[index] = args[start + index];
  178. }
  179. index = -1;
  180. var otherArgs = Array(start + 1);
  181. while (++index < start) {
  182. otherArgs[index] = args[index];
  183. }
  184. otherArgs[start] = array;
  185. return apply(func, this, otherArgs);
  186. };
  187. }
  188. /**
  189. * Checks if `value` is a flattenable `arguments` object or array.
  190. *
  191. * @private
  192. * @param {*} value The value to check.
  193. * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
  194. */
  195. function isFlattenable(value) {
  196. return isArray(value) || isArguments(value) ||
  197. !!(spreadableSymbol && value && value[spreadableSymbol]);
  198. }
  199. /**
  200. * Converts `value` to a string key if it's not a string or symbol.
  201. *
  202. * @private
  203. * @param {*} value The value to inspect.
  204. * @returns {string|symbol} Returns the key.
  205. */
  206. function toKey(value) {
  207. if (typeof value == 'string' || isSymbol(value)) {
  208. return value;
  209. }
  210. var result = (value + '');
  211. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  212. }
  213. /**
  214. * Checks if `value` is likely an `arguments` object.
  215. *
  216. * @static
  217. * @memberOf _
  218. * @since 0.1.0
  219. * @category Lang
  220. * @param {*} value The value to check.
  221. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  222. * else `false`.
  223. * @example
  224. *
  225. * _.isArguments(function() { return arguments; }());
  226. * // => true
  227. *
  228. * _.isArguments([1, 2, 3]);
  229. * // => false
  230. */
  231. function isArguments(value) {
  232. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  233. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  234. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  235. }
  236. /**
  237. * Checks if `value` is classified as an `Array` object.
  238. *
  239. * @static
  240. * @memberOf _
  241. * @since 0.1.0
  242. * @category Lang
  243. * @param {*} value The value to check.
  244. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  245. * @example
  246. *
  247. * _.isArray([1, 2, 3]);
  248. * // => true
  249. *
  250. * _.isArray(document.body.children);
  251. * // => false
  252. *
  253. * _.isArray('abc');
  254. * // => false
  255. *
  256. * _.isArray(_.noop);
  257. * // => false
  258. */
  259. var isArray = Array.isArray;
  260. /**
  261. * Checks if `value` is array-like. A value is considered array-like if it's
  262. * not a function and has a `value.length` that's an integer greater than or
  263. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  264. *
  265. * @static
  266. * @memberOf _
  267. * @since 4.0.0
  268. * @category Lang
  269. * @param {*} value The value to check.
  270. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  271. * @example
  272. *
  273. * _.isArrayLike([1, 2, 3]);
  274. * // => true
  275. *
  276. * _.isArrayLike(document.body.children);
  277. * // => true
  278. *
  279. * _.isArrayLike('abc');
  280. * // => true
  281. *
  282. * _.isArrayLike(_.noop);
  283. * // => false
  284. */
  285. function isArrayLike(value) {
  286. return value != null && isLength(value.length) && !isFunction(value);
  287. }
  288. /**
  289. * This method is like `_.isArrayLike` except that it also checks if `value`
  290. * is an object.
  291. *
  292. * @static
  293. * @memberOf _
  294. * @since 4.0.0
  295. * @category Lang
  296. * @param {*} value The value to check.
  297. * @returns {boolean} Returns `true` if `value` is an array-like object,
  298. * else `false`.
  299. * @example
  300. *
  301. * _.isArrayLikeObject([1, 2, 3]);
  302. * // => true
  303. *
  304. * _.isArrayLikeObject(document.body.children);
  305. * // => true
  306. *
  307. * _.isArrayLikeObject('abc');
  308. * // => false
  309. *
  310. * _.isArrayLikeObject(_.noop);
  311. * // => false
  312. */
  313. function isArrayLikeObject(value) {
  314. return isObjectLike(value) && isArrayLike(value);
  315. }
  316. /**
  317. * Checks if `value` is classified as a `Function` object.
  318. *
  319. * @static
  320. * @memberOf _
  321. * @since 0.1.0
  322. * @category Lang
  323. * @param {*} value The value to check.
  324. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  325. * @example
  326. *
  327. * _.isFunction(_);
  328. * // => true
  329. *
  330. * _.isFunction(/abc/);
  331. * // => false
  332. */
  333. function isFunction(value) {
  334. // The use of `Object#toString` avoids issues with the `typeof` operator
  335. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  336. var tag = isObject(value) ? objectToString.call(value) : '';
  337. return tag == funcTag || tag == genTag;
  338. }
  339. /**
  340. * Checks if `value` is a valid array-like length.
  341. *
  342. * **Note:** This method is loosely based on
  343. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  344. *
  345. * @static
  346. * @memberOf _
  347. * @since 4.0.0
  348. * @category Lang
  349. * @param {*} value The value to check.
  350. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  351. * @example
  352. *
  353. * _.isLength(3);
  354. * // => true
  355. *
  356. * _.isLength(Number.MIN_VALUE);
  357. * // => false
  358. *
  359. * _.isLength(Infinity);
  360. * // => false
  361. *
  362. * _.isLength('3');
  363. * // => false
  364. */
  365. function isLength(value) {
  366. return typeof value == 'number' &&
  367. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  368. }
  369. /**
  370. * Checks if `value` is the
  371. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  372. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  373. *
  374. * @static
  375. * @memberOf _
  376. * @since 0.1.0
  377. * @category Lang
  378. * @param {*} value The value to check.
  379. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  380. * @example
  381. *
  382. * _.isObject({});
  383. * // => true
  384. *
  385. * _.isObject([1, 2, 3]);
  386. * // => true
  387. *
  388. * _.isObject(_.noop);
  389. * // => true
  390. *
  391. * _.isObject(null);
  392. * // => false
  393. */
  394. function isObject(value) {
  395. var type = typeof value;
  396. return !!value && (type == 'object' || type == 'function');
  397. }
  398. /**
  399. * Checks if `value` is object-like. A value is object-like if it's not `null`
  400. * and has a `typeof` result of "object".
  401. *
  402. * @static
  403. * @memberOf _
  404. * @since 4.0.0
  405. * @category Lang
  406. * @param {*} value The value to check.
  407. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  408. * @example
  409. *
  410. * _.isObjectLike({});
  411. * // => true
  412. *
  413. * _.isObjectLike([1, 2, 3]);
  414. * // => true
  415. *
  416. * _.isObjectLike(_.noop);
  417. * // => false
  418. *
  419. * _.isObjectLike(null);
  420. * // => false
  421. */
  422. function isObjectLike(value) {
  423. return !!value && typeof value == 'object';
  424. }
  425. /**
  426. * Checks if `value` is classified as a `Symbol` primitive or object.
  427. *
  428. * @static
  429. * @memberOf _
  430. * @since 4.0.0
  431. * @category Lang
  432. * @param {*} value The value to check.
  433. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  434. * @example
  435. *
  436. * _.isSymbol(Symbol.iterator);
  437. * // => true
  438. *
  439. * _.isSymbol('abc');
  440. * // => false
  441. */
  442. function isSymbol(value) {
  443. return typeof value == 'symbol' ||
  444. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  445. }
  446. /**
  447. * Creates an object composed of the picked `object` properties.
  448. *
  449. * @static
  450. * @since 0.1.0
  451. * @memberOf _
  452. * @category Object
  453. * @param {Object} object The source object.
  454. * @param {...(string|string[])} [props] The property identifiers to pick.
  455. * @returns {Object} Returns the new object.
  456. * @example
  457. *
  458. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  459. *
  460. * _.pick(object, ['a', 'c']);
  461. * // => { 'a': 1, 'c': 3 }
  462. */
  463. var pick = baseRest(function(object, props) {
  464. return object == null ? {} : basePick(object, arrayMap(baseFlatten(props, 1), toKey));
  465. });
  466. module.exports = pick;