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