-
Notifications
You must be signed in to change notification settings - Fork 0
/
lodash.js
206 lines (156 loc) · 6.05 KB
/
lodash.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const obj = {
a: {
b: {
c: [1, 2, 3]
}
}
};
/*---------------------- get ----------------------*/
function get(source, path, defaultVal = undefined) {
/*
In replace function, if pattern (1st parameter) is a string, then only the first occurrence will be replaced
to replace all occurances of the pattern, we need to use a regex (regular expression) with g flag
*/
const pathArr = Array.isArray(path) ? path : path.replace(/\[/g, ".").replace(/\]/g, "").split(".");
const thisKey = pathArr[0];
if (pathArr.length === 1) {
if (source.hasOwnProperty(thisKey)) return source[thisKey]
return defaultVal;
} else {
if (source.hasOwnProperty(thisKey)) return get(source[thisKey], pathArr.slice(1), defaultVal); // pathArr.slice(1), removing first element from the path array as it has already been proccessed
return defaultVal;
}
}
// get(obj, 'a.b.c'); // [1,2,3]
// get(obj, 'a.b.c.0'); // 1
// get(obj, 'a.b.c[1]'); // 2
// get(obj, ['a', 'b', 'c', '2']); // 3
// get(obj, 'a.b.c[3]'); // undefined
// get(obj, 'a.c', 'bfe'); // 'bfe'
// const getAns = get(obj, []); // 3
// console.log("getAns", getAns);
/*---------------------- set ----------------------*/
function set(obj, path, value) {
/*
In replace function, if pattern (1st parameter) is a string, then only the first occurrence will be replaced
to replace all occurances of the pattern, we need to use a regex (regular expression) with g flag
*/
const pathArr = Array.isArray(path) ? path : path.replace(/\[/g, ".").replace(/\]/g, "").split(".");
const thisKey = pathArr[0], nextKey = pathArr[1];
if (pathArr.length === 1) {
obj[thisKey] = value;
} else {
if (!obj.hasOwnProperty(thisKey)) {
obj[thisKey] = String(Number(nextKey)) === nextKey ? [] : {};
}
set(obj[thisKey], pathArr.slice(1), value);
}
}
set(obj, 'a.c.d.01.e.0.f', 'BFE');
set(obj, 'a.b.c.4', 'BFE');
console.log("obj", JSON.stringify(obj));
/*---------------------- omit ----------------------*/
function omit(obj, path) {
const pathArr = Array.isArray(path) ? path : path.replace(/\[/g, ".").replace(/\]/g, "").split(".");
const thisKey = Number(pathArr[0]) || pathArr[0];
if (pathArr.length === 1) {
if (Array.isArray(obj)) obj.splice(thisKey, 1);
else delete obj[thisKey];
} else {
if (obj.hasOwnProperty(thisKey)) omit(obj[thisKey], pathArr.slice(1));
}
}
// omit(obj, "a.b.c.1");
// console.log("obj", JSON.stringify(obj));
/*---------------------- flatten ----------------------*/
let arr = [
[1, 2],
[3, 4],
[5, 6, [7, 8], 9],
[10, 11, [12, [13, [14, [15, [16]]]]], 17, [18, 19, 20]],
[[[{ name: "hi" }]], { roll: 1 }]
]
let arr2 = [[], [[[]]], [[]], []];
function flatten(arr, depth = 0) {
if (depth === 0) return arr;
let newArr = [];
arr.forEach(element => {
if (Array.isArray(element)) {
newArr.push(...flatten(element, depth - 1));
} else {
newArr.push(element);
}
});
return newArr;
}
const ans = flatten(arr2, 1);
// console.log("fltten", ans)
/*---------------------- curry ----------------------*/
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.call(this, ...args);
} else {
// if some more arguments are there in func than has already come in the curried, then we need to recursively call curried function with the missing arguments
return function(...missingArgs) {
return curried.call(this, ...args, ...missingArgs);
}
}
}
}
function join(a, b, c) {
return `${a}_${b}_${c}`;
}
// const curriedJoin = curry(join);
// console.log(curriedJoin(1, 2, 3)) // '1_2_3'
// console.log(curriedJoin(1)(2, 3)) // '1_2_3'
// console.log(curriedJoin(1)(2)(3)) // '1_2_3'
// console.log(curriedJoin(1, 2, 3, 4)) // '1_2_3'
/*---------------------- cloneDeep ----------------------*/
function cloneDeep(data) {
const map = new Map();
function clone(data) {
if ([null, undefined].includes(data)) return data;
if (typeof data === "object") {
const clonedObj = Array.isArray(data) ? [] : {};
if (map.has(data)) return map.get(data); // to overcome circular reference in the object
else map.set(data, clonedObj);
// Object.getOwnPropertySymbols can detect Symbols too as keys, but Object.keys can't
[...Object.keys(data), ...Object.getOwnPropertySymbols(data)].forEach(key => {
clonedObj[key] = clone(data[key]);
});
return clonedObj;
} else return data;
}
return clone(data);
}
// const sym = Symbol()
// const obj2 = { [sym]: 'bfe' }
// const clone = cloneDeep(obj2)
// console.log(clone); //.not.toBe(obj2)
// console.log(clone[sym]);
/*---------------------- isEqual ----------------------*/
function isEqual(a, b) {
let map = new Map();
function isEqualUtil(a, b) {
if (typeof a !== typeof b) return false;
// to overcome circular object
if (map.has(a) || map.has(b)) return true;
map.set(a, b);
// to overcome circular object
if (typeof a === "object") {
const keysA = [...Object.keys(a), ...Object.getOwnPropertySymbols(a)]; // symbols used as key be detected from Object.getOwnPropertySymbols only but not from Object.keys
const keysB = [...Object.keys(b), ...Object.getOwnPropertySymbols(b)]; // symbols used as key be detected from Object.getOwnPropertySymbols only but not from Object.keys
if (keysB.length !== keysA.length) return false;
for (let i = 0; i < keysA.length; i++) {
let key = keysA[i];
if (!isEqualUtil(a[key], b[key])) return false;
}
} else {
if (a !== b) return false;
}
return true;
}
return isEqualUtil(a, b)
}
// console.log(isEqual([1, 2, 3], [1, 2, 3, 4])); //.toBe(false)