-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathpluck.js
37 lines (34 loc) · 1.13 KB
/
pluck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @module 101/pluck
*/
var isObject = require('./is-object');
var exists = require('./exists');
var keypather = require('keypather')();
/**
* Functional version of obj[key], returns the value of the key from obj.
* When only a key is specified pluck returns a partial-function which accepts obj.
* @function module:101/pluck
* @param {object} [obj] - object from which the value is plucked
* @param {string|array} key - key of the value from obj which is returned
* @param {boolean} [isKeypath=true] - specifies whether the key is a keypath or key
* @return {*|function} The value of the key from obj or Partial-function pluck (which accepts obj) and returns the value of the key from obj
*/
module.exports = function (obj, key, isKeypath) {
if (!isObject(obj)) {
isKeypath = key;
key = obj;
return function (obj) {
return pluck(obj, key, isKeypath);
};
}
else {
return pluck(obj, key, isKeypath);
}
};
function pluck (obj, key, isKeypath) {
key = Array.isArray(key) ? key[0] : key;
isKeypath = exists(isKeypath) ? isKeypath : true;
return isKeypath ?
keypather.get(obj, key):
obj[key];
}