forked from jonschlinkert/rename-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (32 loc) · 1.14 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
(function () {
'use strict';
function rename(obj, fn, isDeepParam) {
var isDeep = false || isDeepParam; // makes sure isDeep is defined, for old code that does not have third
// parameter. The default is shallow - to preserve old code's logic.
if (typeof fn !== 'function') {
return obj;
}
var res = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if(isDeep && typeof obj[key] === 'object'){ // if we have to do deep renaming, we call recursively
// until reach non-object leaves of the tree.
obj[key] = rename(obj[key], fn, isDeep);// passing on isDeep. TODO: make isDeep global
}
res[fn(key) || key] = obj[key];
}
}
return res;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = rename;
} else {
if (typeof define === 'function' && define.amd) {
define([], function () {
return rename;
});
} else {
window.rename = rename;
}
}
})();