-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
39 lines (33 loc) · 858 Bytes
/
index.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
38
39
/*!
* deep-pick <https://github.com/jonschlinkert/deep-pick>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Licensed under the MIT License
*/
'use strict';
var reduce = require('reduce-object');
var isObject = require('isobject');
var mixinDeep = require('mixin-deep');
var pick = require('object-pick');
module.exports = function flattenObject(obj, prop, options) {
var value = pick(flatten(obj, prop), prop);
if (options && options.value === true) {
return value[prop];
}
return value;
};
function flatten(obj, prop) {
return reduce(obj, function (acc, value, key) {
if (key === prop) {
acc[prop] = value;
}
if (isObject(value)) {
if (value.hasOwnProperty(prop) && isObject(value[prop])) {
mixinDeep(acc, value);
}
} else {
acc[key] = value;
}
return acc;
}, {});
}