-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.js
More file actions
393 lines (315 loc) · 11.2 KB
/
MyArray.js
File metadata and controls
393 lines (315 loc) · 11.2 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
class MyArray {
constructor() {
[this.length, this.data] = this.#init(arguments);
}
// PRIVATE METHODS
#init(args) {
const data = {};
const length = args.length;
for (let i = 0; i < length; i++) {
data[i] = args[i]
}
return [length, data] ;
}
#absIndex(index) {
return index >= 0 ? index : this.length + index
}
/** Returns a positive index such that 0 < index < this.length - 1 */
#forceIndexToBoundaries(index) {
return index >= 0 ? Math.min(this.length - 1, index) : Math.max(0, index += this.length)
}
/**
* Reassigns the values starting at index and replaces them with the value n positions forward (n = positions).
* If ShiftAndDelete = true, delete the last duplicate elements
*/
#shiftLeft(index, options = {}) {
const {
positions = 1,
shiftAndDelete = false
} = options;
if (positions < 1 || index + positions >= this.length) return;
this.forEach((currentValue, i) => {
this.data[i] = this.data[i + positions];
}, { fromIndex: index, toIndex: this.length - 1 - positions })
if (shiftAndDelete) {
this.forEach((currentValue, i) => {
delete this.data[i];
this.length--;
}, { reverse: true, toIndex: this.length - positions })
}
}
#shiftRight(index, options = {}) {
const { positions = 1 } = options;
if (positions < 1 || positions - index > this.length) return;
this.length += positions;
this.forEach((currentValue, i) => {
this.data[i] = this.data[i - positions];
}, { reverse: true, toIndex: index + positions })
}
// STATIC METHODS
static isMyArray(myArrayObj) {
return myArrayObj instanceof MyArray
}
static from(data) {
if (Symbol.iterator in Object(data)) {
return new MyArray(...data);
} else {
return new MyArray();
}
}
// INSTANCE METHODS
/**
*
* @param {Function} callback A function that accepts up to three arguments. forEach calls the callback function one time for each element in the array.
* @options
* @param {boolean} options.reverse Indicates whether the array should be traversed from the end to the beginning
* @param {number} options.fromIndex The index where traversing the array begins
* @param {number} options.toIndex The index where traversing the array stops
* @param {number} options.escapeIfTrue Indicates whether to stop traversing the array if callback returns true
* @returns {void}
*/
forEach(callback, options = {}) {
let currentValue;
// forEach
if (Object.keys(options).length === 0) {
for (let i = 0; i < this.length; i++) {
currentValue = this.data[i]
callback(currentValue, i, this)
}
return;
}
// forEach Pro
const {
reverse = false,
fromIndex = reverse ? this.length - 1 : 0,
toIndex = reverse ? 0 : this.length - 1,
escapeIfTrue = false
} = options
const limitIndex = (index, limit, callback) => {
const positiveIndex = index >= 0 ? Math.min(index, this.length - 1) : Math.max(0, this.length + index);
if (typeof callback === 'function') {
return callback(positiveIndex, limit)
}
return positiveIndex
}
const from = limitIndex(fromIndex);
const to = limitIndex(toIndex, from, (positiveIndex, limit) => {
return reverse ? Math.min(positiveIndex, limit) : Math.max(positiveIndex, limit)
})
if (reverse) {
for (let i = from; i >= to; i--) {
currentValue = this.data[i]
if (callback(currentValue, i, this) && escapeIfTrue) return true;
}
} else {
for (let i = from; i <= to; i++) {
currentValue = this.data[i]
if (callback(currentValue, i, this) && escapeIfTrue) return true;
}
}
return escapeIfTrue ? false : undefined;
}
at(index = 0) {
// This is to emulate behavior of javascript's native array method
if (typeof index === 'boolean') index = index ? 1 : 0;
if (typeof index !== 'number' || isNaN(index)) index = 0;
if (index >= 0 && index <= this.length - 1) {
return this.get(index)
}
if (index < 0 && Math.abs(index) <= this.length) {
return this.get(this.length + index)
}
return undefined;
}
concat() {
const newMyArr = new MyArray(...this); // This is possible because the *[Symbol.iterator]() method is defined.
for (let i = 0; i < arguments.length; i++) {
// TODO Check this
if (arguments[i] instanceof MyArray) {
arguments[i].forEach(currentValue => {
newMyArr.push(currentValue);
})
} else {
newMyArr.push(arguments[i])
}
}
return newMyArr;
}
copyWithin(target, start = 0, end = this.length) {
target = this.#absIndex(target);
start = this.#absIndex(start);
if (end >= this.length) end = this.length;
end = this.#absIndex(end);
if (start >= end) return this;
if (target < 0 || target >= this.length - 1) return this;
for (let i = 0; i < end - start; i++) {
this.data[target + i] = this.data[start + i]
}
return this;
}
filter(callback, options = {}) {
const newMyArr = new MyArray();
this.forEach((currentValue, index, myArrayObj) => {
if (callback(currentValue, index, myArrayObj)) {
newMyArr.push(currentValue)
}
}, options)
return newMyArr;
}
find(callback, options = {}) {
options["escapeIfTrue"] = true;
let element;
this.forEach((currentValue, index, myArrayObj) => {
element = currentValue;
return callback(currentValue, index, myArrayObj)
}, options)
return element;
}
findIndex(callback, options = {}) {
options["escapeIfTrue"] = true
let itemIndex;
this.forEach((currentValue, index, myArrayObj) => {
if (callback(currentValue, index, myArrayObj)) {
itemIndex = index;
return true;
}
}, options)
return itemIndex || - 1;
}
flat(depth = 1) {
const newMyArr = new MyArray();
const deepFlat = (item, depth) => {
if (MyArray.isMyArray(item) && depth > 0) {
item.forEach(subItem => deepFlat(subItem, depth - 1))
return;
}
newMyArr.push(item) // side effect
return item;
}
this.forEach(currentValue => deepFlat(currentValue, depth))
return newMyArr
}
includes(item, options = {}) {
options["escapeIfTrue"] = true
return this.forEach(currentValue => currentValue === item, options);
}
every(callback) {
if (this.length === 0) return true; //
return !this.forEach((currentValue, index, myArrayObj) => {
return !callback(currentValue, index, myArrayObj);
}, { escapeIfTrue: true })
}
join(separator = ',') {
let string = '';
this.forEach(currentValue => string += (string && separator) + currentValue)
return string;
}
toString() {
return this.join()
}
indexOf(searchElement, fromIndex = 0) {
let itemIndex;
this.forEach((currentValue, index) => {
if (currentValue === searchElement) {
itemIndex = index;
return true;
}
}, { escapeIfTrue: true, fromIndex })
return itemIndex || -1;
}
*keys() {
let index = 0;
while( index < this.length) {
yield index++
}
}
*values() {
let index = 0;
while (index < this.length) {
yield this.get(index++)
}
}
*entries() {
let index = 0;
while (index < this.length) {
yield [index, this.get(index++)]
}
}
// Allows the following to be applied to instances of MyArray:
// 1. for...of
// 2. Spread operator (spread operator)
// 3. Destructuring assignment
// 4. The yield* expression
*[Symbol.iterator]() {
yield* this.values();
}
get(index) {
return this.data[index];
}
push(item) {
this.data[this.length] = item;
this.length++;
return this.data;
}
pop() {
const item = this.data[this.length - 1]
delete this.data[this.length - 1]
this.length--;
return item;
}
shift() {
return this.delete(0);
}
unshift() {
this.#shiftRight(0, { positions: arguments.length })
for (let i = 0; i < arguments.length; i++) {
this.data[i] = arguments[i]
}
return this.length;
}
delete(index) {
const item = this.data[index];
this.#shiftLeft(index, { shiftAndDelete: true })
return item;
}
splice(start, deleteCount, ...newItems) {
const newMyarr = new MyArray();
if (start === undefined) return newMyarr;
newItems ??= [];
if (deleteCount < 0) deleteCount = 0
// This behavior is to emulate the splice method of a standard array.
if (start > this.length - 1 && deleteCount === 0 && newItems.length > 0 ) {
for (let i = 0; i < newItems.length; i++) {
this.push(newItems[i])
}
return newMyarr;
}
start = this.#forceIndexToBoundaries(start);
if (deleteCount === undefined || deleteCount > this.length - start) {
deleteCount = this.length - start
}
// Save items to be deleted/overwritten
for (let i = 0; i < deleteCount; i++) {
newMyarr.push(this.data[start + i])
}
// shift array to the right from start, only if newItems.length - deleteCount > 0
this.#shiftRight(start, { positions: newItems.length - deleteCount });
// newItems values are assigned
for (let i = start; i < newItems.length; i++) {
this.data[start + i] = newItems[i];
}
return newMyarr;
}
reduce(callback, initialValue) {
// Edge cases
if (this.length === 1 && initialValue === undefined) return this.get(0);
if (this.length === 0 && initialValue !== undefined) return initialValue;
let acc = initialValue ?? this.get(0);
let fromIndex = initialValue === undefined ? 1 : 0;
this.forEach((currentValue, currentIndex) => {
acc = callback(acc, currentValue, currentIndex, this)
}, {fromIndex})
return acc
}
}
module.exports = MyArray;