-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlodash-deep.js
55 lines (51 loc) · 2 KB
/
lodash-deep.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
45
46
47
48
49
50
51
52
53
54
55
/**
* Lodash mixins for (deep) object accessing / manipulation.
* @author Mark Lagendijk <[email protected]>
* @license MIT
*/
(function(root, factory){
if(typeof define === 'function' && define.amd){
// AMD. Register as an anonymous module.
define(['lodash'], factory);
}
else if(typeof exports === 'object'){
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('lodash').runInContext());
}
else{
// Browser globals (root is window)
root._.mixin(factory(root._));
}
}(this, function(_, undefined){
'use strict';
var mixins = /** @lends _ */ {
/**
* Maps all values in an object tree and returns a new object with the same structure as the original.
* @param {Object} object - The object to map.
* @param {Function} callback - The function to be called per iteration on any non-object value in the tree.
* Callback is invoked with 2 arguments: (value, propertyPath)
* propertyPath is the path of the current property, in array format.
* @returns {Object}
*/
deepMapValues: function(object, callback, propertyPath){
propertyPath = propertyPath || '';
if(_.isArray(object)){
return _.map(object, deepMapValuesIteratee);
}
else if(_.isObject(object) && !_.isDate(object) && !_.isRegExp(object) && !_.isFunction(object)){
return _.extend({}, object, _.mapValues(object, deepMapValuesIteratee));
}
else{
return callback(object, propertyPath);
}
function deepMapValuesIteratee(value, key){
var valuePath = propertyPath ? propertyPath + '.' + key: key;
return _.deepMapValues(value, callback, valuePath);
}
}
};
_.mixin(mixins);
return mixins;
}));