-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.js
372 lines (328 loc) · 10 KB
/
array.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
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
/**
* @SEE https://github.com/tc39/proposal-relative-indexing-method#polyfill
* @SEE https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
*/
const SHIM_TARGETS = [Array, String, globalThis.Int8Array, globalThis.Uint8Array,
globalThis.Uint8ClampedArray, globalThis.Int16Array, globalThis.Uint16Array,
globalThis.Int32Array, globalThis.Uint32Array, globalThis.Float32Array,
globalThis.Float64Array, globalThis.BigInt64Array, globalThis.BigUint64Array,
];
if (! (Array.prototype.flat instanceof Function)) {
Array.prototype.flat = function(depth = 1) {
const result = [];
depth = Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, depth));
const flattenFn = (item, depth) => {
if (Array.isArray(item) && depth >= 0) {
item.forEach(i => flattenFn(i, depth - 1));
} else {
result.push(item);
}
};
flattenFn(this, Number.isNaN(depth) ? 0 : depth);
return result;
};
}
if (! (Array.prototype.flatMap instanceof Function)) {
Array.prototype.flatMap = function(cb, thisArg) {
return this.map(cb, thisArg).flat(1);
};
}
if (! (Array.prototype.findLast instanceof Function)) {
Array.prototype.findLast = function(callback, thisArg) {
let found = undefined;
this.forEach((item, index, arr) => {
if (callback.call(thisArg, item, index, arr)) {
found = item;
}
}, thisArg);
return found;
};
}
if(! ('lastIndex' in Array.prototype)) {
Object.defineProperty(Array.prototype, 'lastIndex', {
enumerable: false,
configurable: false,
get: function() {
return Math.max(0, this.length - 1);
}
});
}
if(! ('lastItem' in Array.prototype)) {
Object.defineProperty(Array.prototype, 'lastItem', {
enumerable: false,
configurable: false,
get: function() {
return this[this.lastIndex];
},
set: function(val) {
this[this.lastIndex] = val;
}
});
}
if (! (Array.prototype.findLastIndex instanceof Function)) {
Array.prototype.findLastIndex = function(callback, thisArg) {
let found = -1;
this.forEach((item, index, arr) => {
if (callback.call(thisArg, item, index, arr)) {
found = index;
}
}, thisArg);
return found;
};
}
if (! (Array.prototype.at instanceof Function)) {
const at = function at(n) {
n = Math.trunc(n) || 0;
if (n < 0) n += this.length;
if (n < 0 || n >= this.length) return undefined;
return this[n];
};
for (const C of SHIM_TARGETS) {
if (typeof C !== 'undefined') {
Object.defineProperty(C.prototype, 'at', {
value: at,
writable: true,
enumerable: false,
configurable: true
});
}
}
}
/**
* @deprecated [moved to `Object.groupBy()`]
* @see https://github.com/tc39/proposal-array-grouping
*/
if (! (Array.prototype.group instanceof Function) && Object.groupBy instanceof Function) {
Array.prototype.group = function group(callback) {
console.warn('`Array.group()` is deprecated. Please use `Object.groupBy()` instead.');
return Object.groupBy(this, callback);
};
}
/**
* @deprecated [moved to `Object.groupBy()`]
*/
if (! (Array.prototype.groupBy instanceof Function) && Object.groupBy instanceof Function) {
Array.prototype.groupBy = function groupBy(callback) {
console.warn('`Array.goupBy()` is deprecated. Please use `Object.groupBy()` instead.');
return Object.groupBy(this, callback);
};
}
/**
* @see https://github.com/tc39/proposal-array-grouping
* @deprecated [moved to `Map.groupBy()`]
* @requires `Map.prototype.emplace`
*/
if (! (Array.prototype.groupToMap instanceof Function) && (Map.groupBy instanceof Function)) {
Array.prototype.groupToMap = function groupToMap(callback) {
console.warn('`Array.groupToMap()` is deprecated. Please use `Map.groupBy()` instead.');
return Map.groupBy(this, callback);
};
}
/**
* @deprecated [moved to `Map.groupBy()`]
*/
if (Map.groupBy instanceof Function) {
Array.prototype.groupByToMap = function groupByToMap(callback) {
console.warn('`Array.groupByToMap()` is deprecated. Please use `Map.groupBy()` instead.');
return Map.groupBy(this, callback);
};
}
/**
* @see https://github.com/tc39/proposal-array-from-async
*/
if (! (Array.fromAsync instanceof Function)) {
Array.fromAsync = async function fromAsync(items, mapFn, thisArg = globalThis) {
let arr = [];
for await (const item of items) {
arr.push(await item);
}
return Array.from(arr, mapFn, thisArg);
};
}
/**
* @see https://github.com/tc39/proposal-array-equality/
*/
if (! (Array.prototype.equals instanceof Function)) {
Array.prototype.equals = function equals(arr) {
if (this === arr) {
return true;
} else if (! Array.isArray(arr)) {
return false;
} else if (this.length !== arr.length) {
return false;
} else {
return this.every((item, i) => {
const val = arr[i];
if (Array.isArray(item)) {
return Array.isArray(val) && item.equals(val);
} else {
return Object.is(item, val);
}
});
}
};
}
/**
* @see https://github.com/tc39/proposal-array-unique
*/
if (! (Array.prototype.uniqueBy instanceof Function)) {
Array.prototype.uniqueBy = function uniqueBy(arg) {
switch (typeof arg) {
case 'undefined':
return [...new Set(this)];
case 'string':
case 'number':
case 'symbol':
return this.uniqueBy(entry => entry[arg]);
case 'function': {
const found = new Set(); //Cannot use `WeakSet` in case of primative values
return this.filter((...args) => {
const key = arg.apply(this, args);
if (found.has(key)) {
return false;
} else {
found.add(key);
return true;
}
});
}
default:
throw new TypeError('Not a valid argument for uniqueBy');
}
};
}
/**
* Change Array by copy proposal
* @Note: Not clear if should use `structedClone` or `[...this]` for copies
* @see https://github.com/tc39/proposal-change-array-by-copy
*/
if (! (Array.prototype.toReversed instanceof Function)) {
Array.prototype.toReversed = function toReversed() {
return [...this].reverse();
};
}
if (! (Array.prototype.toSorted instanceof Function)) {
Array.prototype.toSorted = function toSorted(cmpFn) {
return [...this].sort(cmpFn);
};
}
if (! (Array.prototype.toSpliced instanceof Function)) {
Array.prototype.toSpliced = function toSpliced(start, deleteCount, ...items) {
const cpy = [...this];
cpy.splice(start, deleteCount, ...items);
return cpy;
};
}
if (! (Array.prototype.with instanceof Function)) {
Array.prototype.with = function (index, value) {
const cpy = [...this];
cpy[index] = value;
return cpy;
};
}
if (! (Array.isTemplateObject instanceof Function)) {
Array.isTemplateObject = function(target) {
if (! (
Array.isArray(target) && Array.isArray(target.raw)
&& Object.isFrozen(target) && Object.isFrozen(target.raw)
)) {
return false;
} else {
return target.length !== 0 && target.length === target.raw.length;
}
};
}
/**
* @see https://github.com/tc39/proposal-arraybuffer-base64
*/
if (! (Uint8Array.prototype.toHex instanceof Function)) {
Uint8Array.prototype.toHex = function toHex() {
return Array.from(
this,
byte => byte.toString(16).padStart(2, '0')
).join('');
};
}
if (! (Uint8Array.prototype.toBase64 instanceof Function)) {
Uint8Array.prototype.toBase64 = function toBase64({ alphabet = 'base64' } = {}) {
if (alphabet === 'base64') {
// @todo Figure out encoding specifics
//return btoa(String.fromCodePoint(...this));
const chunkSize = 0x8000; // 32,768 bytes per chunk
let str = '';
for (let i = 0; i < this.length; i += chunkSize) {
const chunk = this.subarray(i, i + chunkSize);
str += String.fromCharCode.apply(null, chunk);
}
return btoa(str);
} else if (alphabet === 'base64url') {
return this.toBase64({ alphabet: 'base64' }).replaceAll('+', '-').replaceAll('/', '_');
} else {
throw new TypeError('expected alphabet to be either "base64" or "base64url');
}
};
}
if (! (Uint8Array.fromHex instanceof Function)) {
Uint8Array.fromHex = function fromHex(str) {
if (typeof str !== 'string') {
throw new TypeError('expected input to be a string');
} else if (! (str.length & 1) === 0) {
throw new SyntaxError('string should be an even number of characters');
} else {
return Uint8Array.from(
globalThis.Iterator.range(0, str.length, { step: 2 }),
i => parseInt(str.substring(i, i + 2), 16)
);
}
};
}
if (! (Uint8Array.fromBase64 instanceof Function)) {
Uint8Array.fromBase64 = function fromBase64(str, {
alphabet = 'base64',
lastChunkHandling = 'loose',
} = {}) {
if (typeof str !== 'string') {
throw new TypeError('expected input to be a string');
} else if (! ['base64', 'base64url'].includes(alphabet)) {
throw new TypeError('expected alphabet to be either "base64" or "base64url');
} else if (! ['loose', 'strict', 'stop-before-partial'].includes(lastChunkHandling)) {
throw new TypeError(`Invalid \`lastChunkHandling\`: "${lastChunkHandling}".`);
} else if (alphabet === 'base64') {
const lastChunkLength = str.length % 4;
if (lastChunkLength === 1) {
throw new TypeError('Invalid string length.');
} else if (lastChunkLength !== 0) {
switch(lastChunkHandling) {
case 'strict':
throw new SyntaxError('Missing padding.');
case 'loose':
return Uint8Array.fromBase64(str.padEnd(str.length + (4 - lastChunkLength), '='), {
alphabet: 'base64',
lastChunkHandling: 'strict',
});
case 'stop-before-partial':
return Uint8Array.fromBase64(str.slice(0, str.length - lastChunkLength), {
alphabet: 'base64',
lastChunkHandling: 'strict',
});
// Already checked for valid `lastChunkHandling`
}
} else {
// return new TextEncoder().encode(atob(str));
return Uint8Array.from(atob(str), char => char.charCodeAt(0));
}
} else {
return Uint8Array.fromBase64(
str.replaceAll('-', '+').replaceAll('_', '/'),
{ alphabet: 'base64', }
);
}
};
}
// @todo Implement Uint8Array.fromBase64Into & Uint8Array.fromHexInto
// if (! (Uint8Array.fromBase64Into instanceof Function)) {
// Uint8Array.fromBase64Into = function fromBase64Into(str, target) {
// const { read, written } = new TextEncoder().encodeInto(atob(str), target);
// return { read, written, target };
// };
// }