-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
getMapValuesDeep.js
44 lines (38 loc) · 1.06 KB
/
getMapValuesDeep.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
40
41
42
43
44
'use strict';
var getEachDeep = require('./getEachDeep.js');
function getMapValuesDeep(_) {
var eachDeep = getEachDeep(_);
function mapValuesDeep(obj, iteratee, options) {
iteratee = _.iteratee(iteratee);
var res = Array.isArray(obj) ? [] : _.isObject(obj) ? {} : _.clone(obj);
var skipChildren;
eachDeep(
obj,
function (value, key, parent, context) {
// if (!context.skipChildren) {
context.skipChildren = function (skip) {
skipChildren = skip;
};
// }
skipChildren = undefined;
var r = iteratee(value, key, parent, context);
if (!context.isLeaf && skipChildren === undefined) {
skipChildren =
value !== r && Array.isArray(value) != Array.isArray(r);
}
if (context.path !== undefined) {
_.set(res, context.path, r);
} else {
res = r;
}
if (skipChildren) {
return false;
}
},
options
);
return res;
}
return mapValuesDeep;
}
module.exports = getMapValuesDeep;