-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
executable file
·107 lines (107 loc) · 3 KB
/
utils.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
/**
* setter()
* sets the values
* Take three arguments, 'data' being the state of data at the time,
* 'key' being the key to be set
* 'val' ids the value to be assigned to hey
*/
export let setter = (data, key, val) => {
let keys = key.split('.');
let objThres = [];
for (let i = 0; i < keys.length; i++) {
if (i === keys.length - 1) {
objThres[keys[i]] = val;
} else if (i === 0) {
objThres = data[keys[0]];
} else {
objThres[keys[i]] = {};
objThres = objThres[keys[i]];
}
}
return data;
};
/**
* getter()
* get the value of the
* Take two argument, 'data' being the state of data at the time,
* 'key' being the key whose value has to be retrieved
*/
export let getter = (data, key) => {
let keys = key.split('.'), error = false, val, objThres;
for (let i = 0; i < keys.length; i++) {
if (i === keys.length - 1) {
val = objThres[keys[i]] ? objThres[keys[i]] : 'error';
} else if (i === 0) {
objThres = data[keys[0]];
} else if (objThres[keys[i]]) {
objThres = objThres[keys[i]];
} else {
error = true;
break;
}
}
return error ? 'key not found' : val;
};
/**
* isSameOrPartOfKey()
* Take three arguments, 'displayParam', 'changedPropParam', 'newdata'
* it matches both the params and sees if its good on and prop affect the same param
*/
export let isSameOrPartOfKey = (changedPropParam, displayParam, newdata) => {
if (changedPropParam === displayParam) return true;
let keyParam1, keyParam2, lengthkKeyParam1, lengthkKeyParam2, length, error = false;
keyParam1 = changedPropParam.split('.');
keyParam2 = displayParam.split('.');
lengthkKeyParam1 = keyParam1.length;
lengthkKeyParam2 = keyParam2.length;
if (lengthkKeyParam1 === lengthkKeyParam2) return false;
if (lengthkKeyParam1 > lengthkKeyParam2) {
for (let i = 0; i < lengthkKeyParam2; i++) {
if (keyParam1[i] !== keyParam2[i]) {
error = true;
break;
}
}
if (error) return false;
return true;
}
for (let i = 0; i < lengthkKeyParam1; i++) {
if (keyParam1[i] !== keyParam2[i]) {
error = true;
break;
}
}
if (error) return false;
if (getter(newdata, displayParam) === 'error') return false;
return true;
};
/**
* deepCopyObject()
* Take one argument, 'param' and returns a new copy of the object
* it follows a recursive approach to copy the function
*/
export const deepCopyObject = (param)=>{
let copiedObj,i;
if (typeof param !== 'object') {
return param;
}
if (!param) {
return param;
}
//copying the array inside an object
if ('[object Array]' === Object.prototype.toString.apply(param)) {
copiedObj = [];
for (i = 0; i < param.length; i += 1) {
copiedObj[i] = deepCopyObject(param[i]);
}
return copiedObj;
}
copiedObj = {};
//rest all kind of keys
for (i in param) {
if (param.hasOwnProperty(i)) {
copiedObj[i] = deepCopyObject(param[i]);
}
}
return copiedObj;
}