-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (73 loc) · 2.74 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var _ = require('underscore');
var _s = require('underscore.string');
var maxRecursionLevel = -1;
function changeCaseKeys(obj, methodName) {
var method = _s[methodName],
originalKeys = Object.keys(obj),
targetKeys;
if (!method) {
throw new Error("The method " + methodName + " doesn't exist in underscore.string");
};
// if obj is a function, regular expression or date, Object.keys will return an empty array
if (originalKeys.length === 0) {
return;
}
targetKeys = originalKeys.map(method);
for (var i = 0, l = originalKeys.length; i < l; i++) {
if (originalKeys[i] != targetKeys[i]) {
obj[targetKeys[i]] = obj[originalKeys[i]];
delete obj[originalKeys[i]];
}
}
}
function changeCase(obj, methodName, currRecursionLevel) {
if (!currRecursionLevel) {
currRecursionLevel = 0;
}
if (_.isObject(obj)) {
// first check the common case: arrays
if (_.isArray(obj)) {
// if we have reached the max level, don't make the recursive call
if(currRecursionLevel !== maxRecursionLevel){
for (var i = 0, l = obj.length; i < l; i++) {
changeCase(obj[i], methodName, currRecursionLevel + 1);
}
}
}
// if it's not array nor a "normal" object, exit early
else if (obj instanceof Function ||
obj instanceof Date ||
obj instanceof RegExp ||
obj instanceof Number ||
obj instanceof String ||
obj instanceof Boolean ||
obj instanceof Error) {
return;
}
// we have a "normal" object - change the case of the keys and
// make a recursive call for each value in the object
else {
// if we have reached the max level, don't make the recursive call
if (currRecursionLevel !== maxRecursionLevel) {
changeCaseKeys(obj, methodName);
Object.keys(obj).forEach(function(key) {
changeCase(obj[key], methodName, currRecursionLevel + 1);
});
}
}
}
if (currRecursionLevel === 0) {
return obj;
}
}
// NOTE: the idea is the same for objects and arrays; if we look at arrays as
// objects whose keys are "1", "2", etc, we conclude that there's no need to
// call changeCaseKeys for array; so for arrays we simply do the recursive call;
// as for objects, since the key can be anything, we have to call changeCaseKeys
// before the recursive call
module.exports = function(obj, methodName, maxLevel){
if(maxLevel){
maxRecursionLevel = maxLevel;
}
return changeCase(obj, methodName);
};