PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/lodash.pick/index.js

https://gitlab.com/maudebo/tp-maudebaillyotis
JavaScript | 531 lines | 151 code | 39 blank | 341 comment | 60 complexity | 08b0b2d6015ca6d274b22f4eea820901 MD5 | raw file
  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. /**
  78. * The base implementation of `_.property` without support for deep paths.
  79. *
  80. * @private
  81. * @param {string} key The key of the property to get.
  82. * @returns {Function} Returns the new accessor function.
  83. */
  84. function baseProperty(key) {
  85. return function(object) {
  86. return object == null ? undefined : object[key];
  87. };
  88. }
  89. /** Used for built-in method references. */
  90. var objectProto = Object.prototype;
  91. /** Used to check objects for own properties. */
  92. var hasOwnProperty = objectProto.hasOwnProperty;
  93. /**
  94. * Used to resolve the
  95. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  96. * of values.
  97. */
  98. var objectToString = objectProto.toString;
  99. /** Built-in value references. */
  100. var Symbol = root.Symbol,
  101. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  102. spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
  103. /* Built-in method references for those with the same name as other `lodash` methods. */
  104. var nativeMax = Math.max;
  105. /**
  106. * The base implementation of `_.flatten` with support for restricting flattening.
  107. *
  108. * @private
  109. * @param {Array} array The array to flatten.
  110. * @param {number} depth The maximum recursion depth.
  111. * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
  112. * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
  113. * @param {Array} [result=[]] The initial result value.
  114. * @returns {Array} Returns the new flattened array.
  115. */
  116. function baseFlatten(array, depth, predicate, isStrict, result) {
  117. var index = -1,
  118. length = array.length;
  119. predicate || (predicate = isFlattenable);
  120. result || (result = []);
  121. while (++index < length) {
  122. var value = array[index];
  123. if (depth > 0 && predicate(value)) {
  124. if (depth > 1) {
  125. // Recursively flatten arrays (susceptible to call stack limits).
  126. baseFlatten(value, depth - 1, predicate, isStrict, result);
  127. } else {
  128. arrayPush(result, value);
  129. }
  130. } else if (!isStrict) {
  131. result[result.length] = value;
  132. }
  133. }
  134. return result;
  135. }
  136. /**
  137. * The base implementation of `_.pick` without support for individual
  138. * property identifiers.
  139. *
  140. * @private
  141. * @param {Object} object The source object.
  142. * @param {string[]} props The property identifiers to pick.
  143. * @returns {Object} Returns the new object.
  144. */
  145. function basePick(object, props) {
  146. object = Object(object);
  147. return basePickBy(object, props, function(value, key) {
  148. return key in object;
  149. });
  150. }
  151. /**
  152. * The base implementation of `_.pickBy` without support for iteratee shorthands.
  153. *
  154. * @private
  155. * @param {Object} object The source object.
  156. * @param {string[]} props The property identifiers to pick from.
  157. * @param {Function} predicate The function invoked per property.
  158. * @returns {Object} Returns the new object.
  159. */
  160. function basePickBy(object, props, predicate) {
  161. var index = -1,
  162. length = props.length,
  163. result = {};
  164. while (++index < length) {
  165. var key = props[index],
  166. value = object[key];
  167. if (predicate(value, key)) {
  168. result[key] = value;
  169. }
  170. }
  171. return result;
  172. }
  173. /**
  174. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  175. *
  176. * @private
  177. * @param {Function} func The function to apply a rest parameter to.
  178. * @param {number} [start=func.length-1] The start position of the rest parameter.
  179. * @returns {Function} Returns the new function.
  180. */
  181. function baseRest(func, start) {
  182. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  183. return function() {
  184. var args = arguments,
  185. index = -1,
  186. length = nativeMax(args.length - start, 0),
  187. array = Array(length);
  188. while (++index < length) {
  189. array[index] = args[start + index];
  190. }
  191. index = -1;
  192. var otherArgs = Array(start + 1);
  193. while (++index < start) {
  194. otherArgs[index] = args[index];
  195. }
  196. otherArgs[start] = array;
  197. return apply(func, this, otherArgs);
  198. };
  199. }
  200. /**
  201. * Gets the "length" property value of `object`.
  202. *
  203. * **Note:** This function is used to avoid a
  204. * [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792) that affects
  205. * Safari on at least iOS 8.1-8.3 ARM64.
  206. *
  207. * @private
  208. * @param {Object} object The object to query.
  209. * @returns {*} Returns the "length" value.
  210. */
  211. var getLength = baseProperty('length');
  212. /**
  213. * Checks if `value` is a flattenable `arguments` object or array.
  214. *
  215. * @private
  216. * @param {*} value The value to check.
  217. * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
  218. */
  219. function isFlattenable(value) {
  220. return isArray(value) || isArguments(value) ||
  221. !!(spreadableSymbol && value && value[spreadableSymbol])
  222. }
  223. /**
  224. * Converts `value` to a string key if it's not a string or symbol.
  225. *
  226. * @private
  227. * @param {*} value The value to inspect.
  228. * @returns {string|symbol} Returns the key.
  229. */
  230. function toKey(value) {
  231. if (typeof value == 'string' || isSymbol(value)) {
  232. return value;
  233. }
  234. var result = (value + '');
  235. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  236. }
  237. /**
  238. * Checks if `value` is likely an `arguments` object.
  239. *
  240. * @static
  241. * @memberOf _
  242. * @since 0.1.0
  243. * @category Lang
  244. * @param {*} value The value to check.
  245. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  246. * else `false`.
  247. * @example
  248. *
  249. * _.isArguments(function() { return arguments; }());
  250. * // => true
  251. *
  252. * _.isArguments([1, 2, 3]);
  253. * // => false
  254. */
  255. function isArguments(value) {
  256. // Safari 8.1 incorrectly makes `arguments.callee` enumerable in strict mode.
  257. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  258. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  259. }
  260. /**
  261. * Checks if `value` is classified as an `Array` object.
  262. *
  263. * @static
  264. * @memberOf _
  265. * @since 0.1.0
  266. * @category Lang
  267. * @param {*} value The value to check.
  268. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  269. * @example
  270. *
  271. * _.isArray([1, 2, 3]);
  272. * // => true
  273. *
  274. * _.isArray(document.body.children);
  275. * // => false
  276. *
  277. * _.isArray('abc');
  278. * // => false
  279. *
  280. * _.isArray(_.noop);
  281. * // => false
  282. */
  283. var isArray = Array.isArray;
  284. /**
  285. * Checks if `value` is array-like. A value is considered array-like if it's
  286. * not a function and has a `value.length` that's an integer greater than or
  287. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  288. *
  289. * @static
  290. * @memberOf _
  291. * @since 4.0.0
  292. * @category Lang
  293. * @param {*} value The value to check.
  294. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  295. * @example
  296. *
  297. * _.isArrayLike([1, 2, 3]);
  298. * // => true
  299. *
  300. * _.isArrayLike(document.body.children);
  301. * // => true
  302. *
  303. * _.isArrayLike('abc');
  304. * // => true
  305. *
  306. * _.isArrayLike(_.noop);
  307. * // => false
  308. */
  309. function isArrayLike(value) {
  310. return value != null && isLength(getLength(value)) && !isFunction(value);
  311. }
  312. /**
  313. * This method is like `_.isArrayLike` except that it also checks if `value`
  314. * is an object.
  315. *
  316. * @static
  317. * @memberOf _
  318. * @since 4.0.0
  319. * @category Lang
  320. * @param {*} value The value to check.
  321. * @returns {boolean} Returns `true` if `value` is an array-like object,
  322. * else `false`.
  323. * @example
  324. *
  325. * _.isArrayLikeObject([1, 2, 3]);
  326. * // => true
  327. *
  328. * _.isArrayLikeObject(document.body.children);
  329. * // => true
  330. *
  331. * _.isArrayLikeObject('abc');
  332. * // => false
  333. *
  334. * _.isArrayLikeObject(_.noop);
  335. * // => false
  336. */
  337. function isArrayLikeObject(value) {
  338. return isObjectLike(value) && isArrayLike(value);
  339. }
  340. /**
  341. * Checks if `value` is classified as a `Function` object.
  342. *
  343. * @static
  344. * @memberOf _
  345. * @since 0.1.0
  346. * @category Lang
  347. * @param {*} value The value to check.
  348. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  349. * @example
  350. *
  351. * _.isFunction(_);
  352. * // => true
  353. *
  354. * _.isFunction(/abc/);
  355. * // => false
  356. */
  357. function isFunction(value) {
  358. // The use of `Object#toString` avoids issues with the `typeof` operator
  359. // in Safari 8 which returns 'object' for typed array and weak map constructors,
  360. // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  361. var tag = isObject(value) ? objectToString.call(value) : '';
  362. return tag == funcTag || tag == genTag;
  363. }
  364. /**
  365. * Checks if `value` is a valid array-like length.
  366. *
  367. * **Note:** This function is loosely based on
  368. * [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  369. *
  370. * @static
  371. * @memberOf _
  372. * @since 4.0.0
  373. * @category Lang
  374. * @param {*} value The value to check.
  375. * @returns {boolean} Returns `true` if `value` is a valid length,
  376. * else `false`.
  377. * @example
  378. *
  379. * _.isLength(3);
  380. * // => true
  381. *
  382. * _.isLength(Number.MIN_VALUE);
  383. * // => false
  384. *
  385. * _.isLength(Infinity);
  386. * // => false
  387. *
  388. * _.isLength('3');
  389. * // => false
  390. */
  391. function isLength(value) {
  392. return typeof value == 'number' &&
  393. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  394. }
  395. /**
  396. * Checks if `value` is the
  397. * [language type](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
  398. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  399. *
  400. * @static
  401. * @memberOf _
  402. * @since 0.1.0
  403. * @category Lang
  404. * @param {*} value The value to check.
  405. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  406. * @example
  407. *
  408. * _.isObject({});
  409. * // => true
  410. *
  411. * _.isObject([1, 2, 3]);
  412. * // => true
  413. *
  414. * _.isObject(_.noop);
  415. * // => true
  416. *
  417. * _.isObject(null);
  418. * // => false
  419. */
  420. function isObject(value) {
  421. var type = typeof value;
  422. return !!value && (type == 'object' || type == 'function');
  423. }
  424. /**
  425. * Checks if `value` is object-like. A value is object-like if it's not `null`
  426. * and has a `typeof` result of "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 object-like, else `false`.
  434. * @example
  435. *
  436. * _.isObjectLike({});
  437. * // => true
  438. *
  439. * _.isObjectLike([1, 2, 3]);
  440. * // => true
  441. *
  442. * _.isObjectLike(_.noop);
  443. * // => false
  444. *
  445. * _.isObjectLike(null);
  446. * // => false
  447. */
  448. function isObjectLike(value) {
  449. return !!value && typeof value == 'object';
  450. }
  451. /**
  452. * Checks if `value` is classified as a `Symbol` primitive or object.
  453. *
  454. * @static
  455. * @memberOf _
  456. * @since 4.0.0
  457. * @category Lang
  458. * @param {*} value The value to check.
  459. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  460. * @example
  461. *
  462. * _.isSymbol(Symbol.iterator);
  463. * // => true
  464. *
  465. * _.isSymbol('abc');
  466. * // => false
  467. */
  468. function isSymbol(value) {
  469. return typeof value == 'symbol' ||
  470. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  471. }
  472. /**
  473. * Creates an object composed of the picked `object` properties.
  474. *
  475. * @static
  476. * @since 0.1.0
  477. * @memberOf _
  478. * @category Object
  479. * @param {Object} object The source object.
  480. * @param {...(string|string[])} [props] The property identifiers to pick.
  481. * @returns {Object} Returns the new object.
  482. * @example
  483. *
  484. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  485. *
  486. * _.pick(object, ['a', 'c']);
  487. * // => { 'a': 1, 'c': 3 }
  488. */
  489. var pick = baseRest(function(object, props) {
  490. return object == null ? {} : basePick(object, arrayMap(baseFlatten(props, 1), toKey));
  491. });
  492. module.exports = pick;