-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
316 lines (278 loc) · 6.86 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* The functions exported from this module try keep the same signature as their lodash equivalents
* See: https://lodash.com/docs
*/
function sameValueZero(a, b) {
/**
* Equality method used by lodash. This is the comparison method used by `Map`, `Set`, and `Array.includes`
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Same-value-zero_equality
* See: http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero
*/
if (a === 0 && b === 0) {
// consider -0 and +0 to be equal
return true;
}
// same-value equality
return Object.is(a, b);
}
function eq(a, b) {
/**
* Similar to _.eq
* Performs a shallow value comparison using Same Value Zero algorithm
*/
return sameValueZero(a, b);
}
function identity(value) {
/**
* Similar to _.identity
*/
return value;
}
function has(object, property) {
/**
* Similar to _.has
* Checks whether a property exists on the given object
* Unlike _.has this implementation doesn't currently support checking nested paths
*/
if (object === null) {
return false;
}
return Object.prototype.hasOwnProperty.call(object, property);
}
const findChild = (root, path) => {
const pathArray = path.split('.');
let parent = root;
for (let p = 0; p < pathArray.length - 1; p += 1) {
if (parent) {
parent = parent[pathArray[p]];
} else {
return { parent: null, key: null };
}
}
return { parent: parent || null, key: pathArray[pathArray.length - 1] };
};
function get(object, path, defaultValue) {
/**
* Similar to _.get
* Gets the value of a property and optionally use a default value is the property doesn't exist
* Supports nested properties
*/
const { parent, key } = findChild(object, path);
let value;
if (parent) {
value = parent[key];
}
return value === undefined ? defaultValue : value;
}
function pick(object, propOrArray) {
/**
* Similar to _.pick, the opposite of `omit`
* Returns a subset of props from an object without mutating the original object
*/
if (object === null) {
return {};
}
const included = typeof propOrArray === 'string' ? [propOrArray] : propOrArray;
return included.reduce(
(accumulator, key) => (has(object, key) ? { ...accumulator, [key]: object[key] } : accumulator),
{}
);
}
function omit(object, propOrArray) {
/**
* Similar to _.omit
* Returns a new object excluding some props from the original object
*/
if (object === null) {
return {};
}
const excluded = typeof propOrArray === 'string' ? [propOrArray] : propOrArray;
return Object.keys(object).reduce(
(accumulator, key) => (excluded.indexOf(key) === -1 ? { ...accumulator, [key]: object[key] } : accumulator),
{}
);
}
function mapValues(object, iteratee = identity) {
/**
* Similar to _.mapValues
*/
if (object === null) {
return {};
}
return Object.keys(object).reduce(
(accumulator, key, index) => ({
...accumulator,
[key]: iteratee(object[key], key, index),
}),
{}
);
}
function groupBy(array, criteria) {
/**
* Similar to _.groupBy
*/
const predicate = typeof criteria === 'function' ? criteria : item => item[criteria];
return (array || []).reduce((accumulator, item) => {
const field = predicate(item);
return {
...accumulator,
[field]: [...(accumulator[field] || []), item],
};
}, {});
}
function includes(array, value, fromIndex = 0) {
/**
* Similar to _.includes
*/
for (let i = fromIndex; i < array.length; i += 1) {
if (eq(array[i], value)) {
return true;
}
}
return false;
}
function sortBy(array, iteratees = identity) {
/**
* Similar to _.sortBy
*/
const iterateesArray = Array.isArray(iteratees) ? iteratees : [iteratees];
const createSortFn = it => {
const getSortValue = typeof it === 'function' ? it : object => get(object, it);
return (element1, element2) => {
const sortValue1 = getSortValue(element1);
const sortValue2 = getSortValue(element2);
if (sortValue1 < sortValue2) {
return -1;
}
if (sortValue1 > sortValue2) {
return 1;
}
return 0;
};
};
return iterateesArray.reduce((partial, it) => partial.sort(createSortFn(it)), array);
}
function without(array, ...values) {
/**
* Similar to _.without
*/
return array.filter(element => !includes(values, element));
}
function difference(array1, array2) {
/**
* Similar to _.difference
*/
const remove = new Set(array2);
return array1.filter(item => !remove.has(item));
}
function union(...arrays) {
/**
* Similar to _.union
*/
const unionSet = new Set();
arrays.forEach(array => {
array.forEach(element => {
unionSet.add(element);
});
});
return [...unionSet];
}
function uniq(array) {
/**
* similar to _.uniq
*/
return [...new Set(array)];
}
function range(...args) {
/**
* Similar to _.range
* range([start=0], end, [step=1])
*/
let start = 0;
const end = args[args.length > 1 ? 1 : 0];
let step = 1;
if (args.length > 1) {
// eslint-disable-next-line prefer-destructuring
start = args[0];
}
if (args.length > 2) {
// eslint-disable-next-line prefer-destructuring
step = args[2];
}
const indexes = [];
for (let i = start; i < end; i += step) {
indexes.push(i);
}
return indexes;
}
function sumBy(array, iteratee = identity) {
/**
* Similar to _.sumBy
*/
const iterateeFn = typeof iteratee === 'string' ? value => get(value, iteratee) : iteratee;
return array.reduce((sum, value) => sum + iterateeFn(value), 0);
}
function minBy(array, iteratee = identity) {
/**
* Similar to _.minBy
*/
const iterateeFn = typeof iteratee === 'string' ? element => get(element, iteratee) : iteratee;
let minIndex = -1;
let minValue = Infinity;
for (let i = 0; i < array.length; i += 1) {
const value = iterateeFn(array[i]);
if (value < minValue) {
minIndex = i;
minValue = value;
}
}
return array[minIndex];
}
function maxBy(array, iteratee = identity) {
/**
* Similar to _.maxBy
*/
const iterateeFn = typeof iteratee === 'string' ? element => get(element, iteratee) : iteratee;
let maxIndex = -1;
let maxValue = -Infinity;
for (let i = 0; i < array.length; i += 1) {
const value = iterateeFn(array[i]);
if (value > maxValue) {
maxIndex = i;
maxValue = value;
}
}
return array[maxIndex];
}
function intersection(array1, array2) {
// similar to _.intersection
const set2 = new Set(array2);
const inBoth = new Set();
array1.forEach(item => {
if (set2.has(item)) {
inBoth.add(item);
}
});
return [...inBoth];
}
module.exports = {
difference,
eq,
get,
groupBy,
has,
identity,
includes,
intersection,
mapValues,
maxBy,
minBy,
omit,
pick,
range,
sortBy,
sumBy,
union,
uniq,
without,
};