-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
111 lines (97 loc) · 2.97 KB
/
lib.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
module.exports = rename;
/**
* Check whether an object is Array or not
*
* @type Boolean
* @param {object} subject is the variable that is
* tested for Array identity check
* @see http://www.shamasis.net/2011/08/infinite-ways-to-detect-array-in-javascript/
*/
var isArray = (function () {
// Use compiler's own isArray when available
if (Array.isArray) {
return Array.isArray;
}
// Retain references to variables for performance
// optimization
var objectToStringFn = Object.prototype.toString,
arrayToStringResult = objectToStringFn.call([]);
return function (subject) {
return objectToStringFn.call(subject) === arrayToStringResult;
};
}());
/**
* Actually rename object's keys
*
* @param {obj} object whose keys are to be renamed
* @param {level} maximum depth within the object the renaming is done
* @param {current} depth level from the top
* @param {fn} function to be applied for renaming
* @return {Object} new object with renamed keys
*/
function renameObject(obj, level, current, fn) {
var newObj = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (level === -1 || current < level) {
if (typeof obj[key] !== "object") {
newObj[fn(key) || key] = obj[key];
} else {
newObj[fn(key) || key] = renameAnyObject(obj[key],
level, current+1,
fn);
}
} else {
newObj[fn(key) || key] = obj[key];
}
}
}
return newObj;
}
/**
* Manage the type of action to be taken on object. The type of object (array
* or object) is kept.
*
* @param {obj} object whose keys are to be renamed
* @param {level} maximum depth within the object the renaming is done
* @param {current} depth level from the top
* @param {fn} function to be applied for renaming
* @return {Object} new object with renamed keys
*/
function renameAnyObject(obj, level, current, fn) {
if (typeof obj !== "object") {
throw new Error('Expects an object (or array object)');
}
if (isArray(obj)) {
// Treat as array
var res = [];
for (let s of obj) {
if (typeof s != "object") {
res.push(s);
} else {
var newObj = renameObject(s, level, current, fn);
res.push(newObj);
}
}
return res;
} else {
// Treat as object
return renameObject(obj, level, current, fn);
}
}
/**
* Rename object's keys recursively up to a depth level
* @param {obj} object whose keys are to be renamed
* @param {fn} function to be applied for renaming
* @param {level} maximum depth within the object the renaming is done. Default
* value recursively rename the keys in an object.
* @return {Object} new object with renamed keys
*/
function rename(obj, fn, level) {
if (typeof fn !== 'function') {
return obj;
}
level = typeof level !== 'undefined' ? level : -1;
return renameAnyObject(obj, level, 0, fn);
}