-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
32 lines (28 loc) · 932 Bytes
/
main.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
'use strict';
const get = require('@strikeentco/get');
const set = require('@strikeentco/set');
const isObject = val => Object.prototype.toString.call(val) === '[object Object]';
const pick = (obj, paths, length, sep) => {
const picked = {};
for (let i = 0; i < length; i++) {
const path = paths[i];
const val = get(obj, path, sep);
if (val === undefined) {
continue; // eslint-disable-line no-continue
}
set(picked, path, val, sep);
}
return picked;
};
module.exports = (obj, paths, sep = '.') => {
if (!isObject(obj) || !paths || !(Array.isArray(paths) || typeof paths === 'string')) {
return {};
}
const { length } = paths;
if (typeof paths === 'string' || length < 2) {
const path = typeof paths === 'string' ? paths : paths[0];
const val = get(obj, path, sep);
return val !== undefined ? set({}, path, val, sep) : {};
}
return pick(obj, paths, length, sep);
};