@the-w1zard/dx 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.html +823 -121
- package/README.md +288 -107
- package/dist/index.js +12967 -0
- package/package.json +42 -26
- package/src/dx.d.ts +6 -11
- package/src/array.js +0 -221
- package/src/array.ts +0 -301
- package/src/boolean_byte.js +0 -22
- package/src/boolean_byte.ts +0 -23
- package/src/class.js +0 -1
- package/src/class.ts +0 -0
- package/src/date.js +0 -649
- package/src/date.ts +0 -660
- package/src/error.js +0 -60
- package/src/error.ts +0 -81
- package/src/index.js +0 -18
- package/src/index.ts +0 -18
- package/src/iterator.js +0 -386
- package/src/iterator.ts +0 -407
- package/src/map.js +0 -495
- package/src/map.ts +0 -513
- package/src/map_byte.js +0 -483
- package/src/map_byte.ts +0 -422
- package/src/number.js +0 -178
- package/src/number.ts +0 -212
- package/src/object.js +0 -217
- package/src/object.ts +0 -291
- package/src/promise.js +0 -206
- package/src/promise.ts +0 -309
- package/src/reg_ex.js +0 -479
- package/src/reg_ex.ts +0 -423
- package/src/set.js +0 -227
- package/src/set.ts +0 -198
- package/src/set_byte.js +0 -261
- package/src/set_byte.ts +0 -223
- package/src/soa_codec.js +0 -28
- package/src/soa_codec.ts +0 -27
- package/src/string.js +0 -133
- package/src/string.ts +0 -175
- package/src/unit8array.js +0 -272
- package/src/unit8array.ts +0 -374
- package/src/url_search_params.js +0 -172
- package/src/url_search_params.ts +0 -187
- package/src/weak_map.js +0 -136
- package/src/weak_map.ts +0 -155
- package/src/weak_set.js +0 -92
- package/src/weak_set.ts +0 -101
package/src/iterator.ts
DELETED
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export class I2I<T = A> implements I2<T> {
|
|
4
|
-
private source: Iterator<T>;
|
|
5
|
-
private index = 0;
|
|
6
|
-
private buffer: T[] = [];
|
|
7
|
-
private done = false;
|
|
8
|
-
|
|
9
|
-
constructor(source: Iterable<T> | Iterator<T>) {
|
|
10
|
-
this.source = Symbol.iterator in Object(source)
|
|
11
|
-
? (source as Iterable<T>)[Symbol.iterator]()
|
|
12
|
-
: source as Iterator<T>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
n = this.next;
|
|
16
|
-
r = this.return;
|
|
17
|
-
f = this.throw;
|
|
18
|
-
|
|
19
|
-
next(): IteratorResult<T> {
|
|
20
|
-
if (this.buffer.length) {
|
|
21
|
-
this.index++;
|
|
22
|
-
return { done: false, value: this.buffer.shift() as T };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (this.done) return { done: true, value: undefined };
|
|
26
|
-
|
|
27
|
-
const result = this.source.next();
|
|
28
|
-
if (result.done) this.done = true;
|
|
29
|
-
else this.index++;
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return(value?: A): IteratorResult<T> {
|
|
34
|
-
this.done = true;
|
|
35
|
-
this.buffer.length = 0;
|
|
36
|
-
return this.source.return
|
|
37
|
-
? this.source.return(value)
|
|
38
|
-
: { done: true, value: value as T };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
throw(error?: A): IteratorResult<T> {
|
|
42
|
-
this.done = true;
|
|
43
|
-
this.buffer.length = 0;
|
|
44
|
-
if (this.source.throw) return this.source.throw(error);
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
[Symbol.iterator](): this {
|
|
49
|
-
return this;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
toArray(): AR<T> {
|
|
53
|
-
return Array.from(this);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
first(): T | UD {
|
|
57
|
-
return this.next().value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
last(): T | UD {
|
|
61
|
-
let result: T | UD;
|
|
62
|
-
for (const value of this) result = value;
|
|
63
|
-
return result!;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
at(index: N): T | UD {
|
|
67
|
-
if (index < 0) {
|
|
68
|
-
const values = this.toArray();
|
|
69
|
-
return values[values.length + index];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let i = 0;
|
|
73
|
-
for (const value of this) {
|
|
74
|
-
if (i++ === index) return value;
|
|
75
|
-
}
|
|
76
|
-
return undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
peek(): T | UD {
|
|
80
|
-
if (!this.buffer.length && !this.done) {
|
|
81
|
-
const result = this.source.next();
|
|
82
|
-
if (result.done) this.done = true;
|
|
83
|
-
else this.buffer.push(result.value);
|
|
84
|
-
}
|
|
85
|
-
return this.buffer[0];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
nextValue(): T | UD {
|
|
89
|
-
return this.next().value;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
nextOr(value: T): T {
|
|
93
|
-
const result = this.next();
|
|
94
|
-
return result.done ? value : result.value;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
nextOrM(make: () => T): T {
|
|
98
|
-
const result = this.next();
|
|
99
|
-
return result.done ? make() : result.value;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
nextOrThrow(error: Error = new Error("Iterator exhausted")): T {
|
|
103
|
-
const result = this.next();
|
|
104
|
-
if (result.done) throw error;
|
|
105
|
-
return result.value;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
map<G>(fn: (value: T, index: N) => G): I2I<G> {
|
|
109
|
-
const source = this;
|
|
110
|
-
let index = 0;
|
|
111
|
-
return new I2I<G>({
|
|
112
|
-
next() {
|
|
113
|
-
const result = source.next();
|
|
114
|
-
return result.done ? result : { done: false, value: fn(result.value, index++) };
|
|
115
|
-
},
|
|
116
|
-
return: (value?: A): IteratorResult<G> => {
|
|
117
|
-
source.return?.();
|
|
118
|
-
return { done: true, value: value as G };
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
filter<S extends T>(pred: (value: T, index: N) => value is S): I2I<S>;
|
|
124
|
-
filter(pred: (value: T, index: N) => B): I2I<T>;
|
|
125
|
-
filter(pred: (value: T, index: N) => B): I2I<T> {
|
|
126
|
-
const source = this;
|
|
127
|
-
let index = 0;
|
|
128
|
-
return new I2I<T>({
|
|
129
|
-
next() {
|
|
130
|
-
let result: IteratorResult<T>;
|
|
131
|
-
while (!(result = source.next()).done) {
|
|
132
|
-
if (pred(result.value, index++)) return result;
|
|
133
|
-
}
|
|
134
|
-
return result;
|
|
135
|
-
},
|
|
136
|
-
return: (value?: A) => source.return(value),
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
flatMap<G>(fn: (value: T, index: N) => Iterable<G> | Iterator<G>): I2I<G> {
|
|
141
|
-
const source = this;
|
|
142
|
-
let index = 0;
|
|
143
|
-
let inner: Iterator<G> | undefined;
|
|
144
|
-
|
|
145
|
-
return new I2I<G>({
|
|
146
|
-
next() {
|
|
147
|
-
while (true) {
|
|
148
|
-
if (inner) {
|
|
149
|
-
const result = inner.next();
|
|
150
|
-
if (!result.done) return result;
|
|
151
|
-
inner = undefined;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const outer = source.next();
|
|
155
|
-
if (outer.done) return { done: true, value: undefined };
|
|
156
|
-
|
|
157
|
-
const mapped = fn(outer.value, index++);
|
|
158
|
-
inner = Symbol.iterator in Object(mapped)
|
|
159
|
-
? (mapped as Iterable<G>)[Symbol.iterator]()
|
|
160
|
-
: mapped as Iterator<G>;
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
return: (value?: A): IteratorResult<G> => {
|
|
164
|
-
source.return?.();
|
|
165
|
-
return { done: true, value: value as G };
|
|
166
|
-
},
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
flat<G>(this: I2I<Iterable<G> | Iterator<G>>): I2I<G> {
|
|
171
|
-
return this.flatMap(value => value);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
take(limit: N): I2I<T> {
|
|
175
|
-
const source = this;
|
|
176
|
-
let count = 0;
|
|
177
|
-
return new I2I<T>({
|
|
178
|
-
next() {
|
|
179
|
-
if (count >= limit) return { done: true, value: undefined };
|
|
180
|
-
const result = source.next();
|
|
181
|
-
if (!result.done) count++;
|
|
182
|
-
return result;
|
|
183
|
-
},
|
|
184
|
-
return: (value?: A) => source.return(value),
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
drop(limit: N): I2I<T> {
|
|
189
|
-
const source = this;
|
|
190
|
-
let dropped = false;
|
|
191
|
-
return new I2I<T>({
|
|
192
|
-
next() {
|
|
193
|
-
if (!dropped) {
|
|
194
|
-
for (let i = 0; i < limit; i++) {
|
|
195
|
-
if (source.next().done) return { done: true, value: undefined };
|
|
196
|
-
}
|
|
197
|
-
dropped = true;
|
|
198
|
-
}
|
|
199
|
-
return source.next();
|
|
200
|
-
},
|
|
201
|
-
return: (value?: A) => source.return(value),
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
unique<G = T>(fn?: (value: T, index: N) => G): I2I<T> {
|
|
206
|
-
const source = this;
|
|
207
|
-
const seen = new Set<G>();
|
|
208
|
-
let index = 0;
|
|
209
|
-
return new I2I<T>({
|
|
210
|
-
next() {
|
|
211
|
-
let result: IteratorResult<T>;
|
|
212
|
-
while (!(result = source.next()).done) {
|
|
213
|
-
const key = fn ? fn(result.value, index++) : result.value as unknown as G;
|
|
214
|
-
if (!seen.has(key)) {
|
|
215
|
-
seen.add(key);
|
|
216
|
-
return result;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return result;
|
|
220
|
-
},
|
|
221
|
-
return: (value?: A) => source.return(value),
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
chunk<G>(size: N): I2I<AR<T>> {
|
|
226
|
-
if (!Number.isInteger(size) || size < 1) throw new RangeError("Chunk size must be a positive integer");
|
|
227
|
-
const source = this;
|
|
228
|
-
return new I2I<AR<T>>({
|
|
229
|
-
next() {
|
|
230
|
-
const chunk: T[] = [];
|
|
231
|
-
while (chunk.length < size) {
|
|
232
|
-
const result = source.next();
|
|
233
|
-
if (result.done) break;
|
|
234
|
-
chunk.push(result.value);
|
|
235
|
-
}
|
|
236
|
-
return chunk.length ? { done: false, value: chunk as AR<T> } : { done: true, value: undefined };
|
|
237
|
-
},
|
|
238
|
-
return: (value?: A): IteratorResult<AR<T>> => {
|
|
239
|
-
source.return?.();
|
|
240
|
-
return { done: true, value: value as AR<T> };
|
|
241
|
-
},
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
zip<G>(other: Iterable<G> | Iterator<G>): I2I<[T, G]> {
|
|
246
|
-
const left = this;
|
|
247
|
-
const right = new I2I(other);
|
|
248
|
-
return new I2I<[T, G]>({
|
|
249
|
-
next() {
|
|
250
|
-
const a = left.next();
|
|
251
|
-
const b = right.next();
|
|
252
|
-
if (a.done || b.done) {
|
|
253
|
-
left.return();
|
|
254
|
-
right.return();
|
|
255
|
-
return { done: true, value: undefined };
|
|
256
|
-
}
|
|
257
|
-
return { done: false, value: [a.value, b.value] };
|
|
258
|
-
},
|
|
259
|
-
return: (value?: A) => {
|
|
260
|
-
left.return();
|
|
261
|
-
right.return();
|
|
262
|
-
return { done: true, value: value as [T, G] };
|
|
263
|
-
},
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
concat(...others: (Iterable<T> | Iterator<T>)[]): I2I<T> {
|
|
268
|
-
const source = this;
|
|
269
|
-
let current: Iterator<T> = source;
|
|
270
|
-
let index = 0;
|
|
271
|
-
return new I2I<T>({
|
|
272
|
-
next() {
|
|
273
|
-
while (true) {
|
|
274
|
-
const result = current.next();
|
|
275
|
-
if (!result.done) return result;
|
|
276
|
-
if (index >= others.length) return { done: true, value: undefined };
|
|
277
|
-
const other = others[index++];
|
|
278
|
-
current = Symbol.iterator in Object(other)
|
|
279
|
-
? (other as Iterable<T>)[Symbol.iterator]()
|
|
280
|
-
: other as Iterator<T>;
|
|
281
|
-
}
|
|
282
|
-
},
|
|
283
|
-
return: (value?: A) => {
|
|
284
|
-
current.return?.(value);
|
|
285
|
-
return { done: true, value: value as T };
|
|
286
|
-
},
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
enumerate<G>(): I2I<[N, T]> {
|
|
291
|
-
const source = this;
|
|
292
|
-
let index = 0;
|
|
293
|
-
return new I2I<[N, T]>({
|
|
294
|
-
next() {
|
|
295
|
-
const result = source.next();
|
|
296
|
-
return result.done ? result : { done: false, value: [index++, result.value] };
|
|
297
|
-
},
|
|
298
|
-
return: (value?: A): IteratorResult<[N, T]> => {
|
|
299
|
-
source.return?.();
|
|
300
|
-
return { done: true, value: value as [N, T] };
|
|
301
|
-
},
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
find(pred: (value: T, index: N) => B): T | UD {
|
|
306
|
-
let index = 0;
|
|
307
|
-
for (const value of this) if (pred(value, index++)) return value;
|
|
308
|
-
return undefined;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
findIndex(pred: (value: T, index: N) => B): N {
|
|
312
|
-
let index = 0;
|
|
313
|
-
for (const value of this) {
|
|
314
|
-
if (pred(value, index)) return index;
|
|
315
|
-
index++;
|
|
316
|
-
}
|
|
317
|
-
return -1 as N;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
some(pred: (value: T, index: N) => B): B {
|
|
321
|
-
let index = 0;
|
|
322
|
-
for (const value of this) if (pred(value, index++)) return true;
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
every(pred: (value: T, index: N) => B): B {
|
|
327
|
-
let index = 0;
|
|
328
|
-
for (const value of this) if (!pred(value, index++)) return false;
|
|
329
|
-
return true;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
reduce<G>(fn: (acc: G, value: T, index: N) => G, initial: G): G {
|
|
333
|
-
let acc = initial;
|
|
334
|
-
let index = 0;
|
|
335
|
-
for (const value of this) acc = fn(acc, value, index++);
|
|
336
|
-
return acc;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
forEach(fn: (value: T, index: N) => void): void {
|
|
340
|
-
let index = 0;
|
|
341
|
-
for (const value of this) fn(value, index++);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
count(): N {
|
|
345
|
-
let count = 0;
|
|
346
|
-
for (const _ of this) count++;
|
|
347
|
-
return count as N;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
sum(fn?: (value: T) => N): N {
|
|
351
|
-
let sum = 0;
|
|
352
|
-
for (const value of this) sum += fn ? fn(value) : value as N;
|
|
353
|
-
return sum as N;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
join(separator: S = ","): S {
|
|
357
|
-
return this.toArray().join(separator) as S;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
partition(pred: (value: T, index: N) => B): [AR<T>, AR<T>] {
|
|
361
|
-
const yes: T[] = [];
|
|
362
|
-
const no: T[] = [];
|
|
363
|
-
let index = 0;
|
|
364
|
-
for (const value of this) (pred(value, index++) ? yes : no).push(value);
|
|
365
|
-
return [yes as AR<T>, no as AR<T>];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
group<G>(fn: (value: T, index: N) => G): Map<G, AR<T>> {
|
|
369
|
-
const out = new Map<G, T[]>();
|
|
370
|
-
let index = 0;
|
|
371
|
-
for (const value of this) {
|
|
372
|
-
const key = fn(value, index++);
|
|
373
|
-
let group = out.get(key);
|
|
374
|
-
if (!group) out.set(key, group = []);
|
|
375
|
-
group.push(value);
|
|
376
|
-
}
|
|
377
|
-
return out as Map<G, AR<T>>;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
toSet(): Set<T> {
|
|
381
|
-
return new Set(this);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
toMap<K, V = T>(keyFn: (value: T, index: N) => K, valueFn?: (value: T, index: N) => V): Map<K, V> {
|
|
385
|
-
const out = new Map<K, V>();
|
|
386
|
-
let index = 0;
|
|
387
|
-
for (const value of this) {
|
|
388
|
-
out.set(keyFn(value, index), valueFn ? valueFn(value, index) : value as unknown as V);
|
|
389
|
-
index++;
|
|
390
|
-
}
|
|
391
|
-
return out;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
isEmpty(): B {
|
|
395
|
-
return this.peek() === undefined;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
exhaust(): void {
|
|
399
|
-
for (const _ of this) {}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
clone(): I2I<T> {
|
|
403
|
-
return new I2I(this.toArray());
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
globalThis.I2 = I2I;
|