-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 1.97 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
"use strict"
function changeProp(obj, oldProp, newProp){
if (oldProp !== newProp) {
Object.defineProperty(obj, newProp,
Object.getOwnPropertyDescriptor(obj, oldProp));
delete obj[oldProp];
}
}
function cloneDeep(obj){
let x = null;
function copyValue(value){
if (typeof value !== "object") {
return value;
} else if (value instanceof Date) {
return new Date(value);
} else {
return cloneDeep(value);
}
}
if(typeof obj === 'object'){
x = {};
if (!Array.isArray(obj)) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
x[key] = copyValue(obj[key]);
}
}
} else {
x = [];
for (let i = 0; i < obj.length; i++) {
x[i] = copyValue(obj[i]);
}
}
}
return x;
}
/**
* Applies String.replace in every Object's prop's name that contain the substring. It can be shallow copied or full cloned.
* @param {Object} objToReplace Object where we will search for the props that you want to replace
* @param {string} searchValue Will check for props that contain the searchValue
* @param {string} replaceValue Value that will replace the substring contained in "searchValue" in props
* @param {boolean} [clone=true] By default, it clones the original object so It cannot mutate. If you want to be shallow copied set this option to false
* @returns {Object} Object with the props replaced
*/
function propReplace(objToReplace, searchValue, replaceValue, clone = true){
let formatedObj = (clone === true) ? cloneDeep(objToReplace) : objToReplace;
for (let key in formatedObj) {
if (formatedObj.hasOwnProperty(key)) {
let value = formatedObj[key];
let newKey = (key.indexOf(searchValue) !== -1) ? key.replace(searchValue, replaceValue) : key;
changeProp(formatedObj, key, newKey);
if (typeof value === 'object') {
propReplace(value, searchValue, replaceValue, false);
}
}
}
return formatedObj;
}
module.exports = propReplace;