-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-refactor.js
172 lines (161 loc) · 4.58 KB
/
array-refactor.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*!
* array-refactor <https://github.com/iamsrujal/array-refactor>
*
* Copyright (c) 2019-2020, Srujal Patel.
* Licensed under the MIT License.
*/
'use strict';
// Remove key from object function for array refactor
/**
* Remove key inside object or array of objects
* @param {Object[]} arr - The element to search for.
* @param {(string|string[])} arrayOfKeys - The key in this array of objects or object to remove.
*/
let pop = (arr, arrayOfKeys) => {
let temp = [];
// check given varibale is type of array or object
if (arr && typeof arr === 'object') {
let newArr = JSON.parse(JSON.stringify(arr));
if (!arrayOfKeys) {
return newArr;
}
if (Array.isArray(newArr)) {
temp = newArr.map(d => {
return deleteKey(d, arrayOfKeys);
});
} else {
checkProperty(newArr, arrayOfKeys);
removeKey(newArr, arrayOfKeys)
temp = newArr;
}
}
if ((arr && typeof arr === 'object') || temp.length > 0 || Object.keys(temp).length > 0) {
return temp;
} else {
if (!arrayOfKeys) {
throw new Error("arrayRefactorPop Expects The Second Parameter To Be A String Or An Array Of Strings");
} else {
throw new Error("arrayRefactorPop Expects The First Argument To Be An Object Or Array Of Objects");
}
}
}
// Push object from object function for array refactor
/**
* Push key with value inside object or array of objects and remove other key value pair
* @param {Object[]} arr - The element to search for.
* @param {(string|string[])} arrayOfKeys - The key in this array of objects or object to remain.
*/
let push = (arr, arrayOfKeys) => {
let temp = [];
// check given varibale is type of array or object
if (arr && typeof arr === 'object') {
let newArr = JSON.parse(JSON.stringify(arr));
if (!arrayOfKeys) {
return newArr;
}
if (Array.isArray(newArr)) {
temp = newArr.map(d => {
return deleteKeyForPush(d, arrayOfKeys);
})
} else {
checkPropertyForPush(newArr, arrayOfKeys);
removeOtherKeyForPush(newArr, arrayOfKeys);
temp = newArr;
}
}
if ((arr && typeof arr === 'object') || temp.length > 0 || Object.keys(temp).length > 0) {
return temp;
} else {
if (!arrayOfKeys) {
throw new Error("arrayRefactorPop Expects The Second Parameter To Be A String Or An Array Of Strings");
} else {
throw new Error("arrayRefactorPop Expects The First Argument To Be An Object Or Array Of Objects");
}
}
}
// This recursion function is heart of array-refactor
function deleteKey(insideArr, arrayOfKeys) {
if (Array.isArray(insideArr)) {
return insideArr.map(d => {
checkProperty(d, arrayOfKeys);
if (typeof d === 'object') {
removeKey(d, arrayOfKeys);
}
return d;
})
} else {
checkProperty(insideArr, arrayOfKeys);
removeKey(insideArr, arrayOfKeys)
return insideArr;
}
}
// remove key from object
function removeKey(insideArr, arrayOfKeys) {
if (Array.isArray(arrayOfKeys)) {
arrayOfKeys.forEach(k => {
if (insideArr) {
if (insideArr.hasOwnProperty(k)) {
delete insideArr[k];
}
}
})
} else {
if (insideArr) {
if (insideArr.hasOwnProperty(arrayOfKeys)) {
delete insideArr[arrayOfKeys];
}
}
}
}
// find out property is having an array or object
function checkProperty(arrObj, arrayOfKeys) {
for (let prop in arrObj) {
if (typeof arrObj[prop] === 'object' || Array.isArray(arrObj[prop])) {
deleteKey(arrObj[prop], arrayOfKeys);
}
}
}
function deleteKeyForPush(insideArr, arrayOfKeys) {
if (Array.isArray(insideArr)) {
return insideArr.map(d => {
checkPropertyForPush(d, arrayOfKeys);
if (typeof d === 'object') {
removeOtherKeyForPush(d, arrayOfKeys);
}
return d;
})
} else {
checkPropertyForPush(insideArr, arrayOfKeys);
removeOtherKeyForPush(insideArr, arrayOfKeys);
return insideArr;
}
}
function removeOtherKeyForPush(insideArr, arrayOfKeys) {
if (insideArr) {
if (Array.isArray(arrayOfKeys)) {
for (let prop in insideArr) {
if (!arrayOfKeys.includes(prop)) {
delete insideArr[prop];
}
}
} else {
for (let prop in insideArr) {
if (prop !== arrayOfKeys) {
delete insideArr[prop];
}
}
}
}
}
function checkPropertyForPush(arrObj, arrayOfKeys) {
for (let prop in arrObj) {
if (typeof arrObj[prop] === 'object' || Array.isArray(arrObj[prop])) {
deleteKeyForPush(arrObj[prop], arrayOfKeys);
// removeKey(arrObj, arrayOfKeys);
}
}
}
module.exports = {
pop,
push
};