@the-w1zard/dx 1.0.12 → 1.0.14

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.
Files changed (51) hide show
  1. package/dist/array.d.ts +41 -0
  2. package/dist/boolean_byte.d.ts +12 -0
  3. package/dist/class.d.ts +0 -0
  4. package/dist/date.d.ts +146 -0
  5. package/dist/error.d.ts +16 -0
  6. package/dist/index.d.ts +25 -0
  7. package/dist/index.js +5697 -3907
  8. package/dist/iterator.d.ts +51 -0
  9. package/dist/map.d.ts +69 -0
  10. package/dist/map_bin.d.ts +34 -0
  11. package/dist/map_byte.d.ts +91 -0
  12. package/dist/map_dense.d.ts +33 -0
  13. package/dist/map_rows.d.ts +33 -0
  14. package/dist/number.d.ts +44 -0
  15. package/dist/object.d.ts +35 -0
  16. package/dist/promise.d.ts +62 -0
  17. package/dist/reg_ex.d.ts +72 -0
  18. package/dist/set.d.ts +41 -0
  19. package/dist/set_byte.d.ts +55 -0
  20. package/dist/soa_codec.d.ts +12 -0
  21. package/dist/string.d.ts +39 -0
  22. package/dist/unit8array.d.ts +64 -0
  23. package/dist/url_search_params.d.ts +30 -0
  24. package/dist/weak_map.d.ts +32 -0
  25. package/dist/weak_set.d.ts +20 -0
  26. package/package.json +10 -8
  27. package/src/array.ts +301 -0
  28. package/src/boolean_byte.ts +23 -0
  29. package/src/class.ts +0 -0
  30. package/src/date.ts +660 -0
  31. package/src/dx.d.ts +31 -11
  32. package/src/error.ts +81 -0
  33. package/src/index.ts +57 -0
  34. package/src/iterator.ts +407 -0
  35. package/src/map.ts +513 -0
  36. package/src/map_bin.ts +179 -0
  37. package/src/map_byte.ts +418 -0
  38. package/src/map_dense.ts +167 -0
  39. package/src/map_rows.ts +145 -0
  40. package/src/number.ts +212 -0
  41. package/src/object.ts +291 -0
  42. package/src/promise.ts +309 -0
  43. package/src/reg_ex.ts +423 -0
  44. package/src/set.ts +198 -0
  45. package/src/set_byte.ts +223 -0
  46. package/src/soa_codec.ts +30 -0
  47. package/src/string.ts +175 -0
  48. package/src/unit8array.ts +374 -0
  49. package/src/url_search_params.ts +187 -0
  50. package/src/weak_map.ts +155 -0
  51. package/src/weak_set.ts +101 -0
package/src/dx.d.ts CHANGED
@@ -4,21 +4,25 @@ import { M2BINI } from "./map_bin";
4
4
  import { B2BI } from "./boolean_byte";
5
5
  import { M2I } from "./map";
6
6
  import { M2BI } from "./map_byte";
7
- import { SET2I } from "./set";
8
- import { SET2BI } from "./set_byte";
9
- import { D2I, R2I, R2II } from "./date";
7
+
10
8
  import { A2I } from "./array";
11
- import { O2I } from "./object";
12
- import { WM2I } from "./weak_map";
9
+ import { D2I, R2I, R2II } from "./date";
10
+ import { ER2I } from './error'
13
11
  import { I2I } from "./iterator";
14
12
  import { N2I } from "./number";
13
+ import { O2I } from "./object";
15
14
  import { P2I } from "./promise";
15
+ import { REC2, RECC2 } from './reg_ex'
16
+ import { SET2I } from "./set";
17
+ import { SET2BI } from "./set_byte";
18
+ import { SOA_CODEC, soa_codeci } from './soa_codec'
16
19
  import { S2I } from "./string";
17
20
  import { U82I } from "./unit8array";
18
- import { WS2I } from "./weak_set";
19
21
  import { USP2I } from "./url_search_params";
20
- import { ER2I } from "./error";
21
- import {MaybePromise} from '../bag_of_tricks/maybe_promise'
22
+
23
+ import { WM2I } from "./weak_map";
24
+ import { WS2I } from "./weak_set";
25
+ import { MaybePromisei, type MaybePromise } from './index'
22
26
  export {};
23
27
 
24
28
 
@@ -31,7 +35,7 @@ declare global {
31
35
  it.
32
36
  ================================================================================== ⇦✦──⇦✦──✦──✦──← 'pew pew' 🧙 [UX/DX] */
33
37
  type MaybePromise<R = any> = R;
34
- var MaybePromise: MaybePromise;
38
+ var MaybePromise: MaybePromisei;
35
39
  var B2B: typeof B2BI; // BOOLEAN BINARY
36
40
 
37
41
  var M2: typeof M2I; // map
@@ -43,6 +47,9 @@ declare global {
43
47
  var SET2: typeof SET2I; // set
44
48
  var SET2B: typeof SET2BI; // set
45
49
 
50
+ var RES2: typeof RECC2; // reg ex static
51
+ var RE2: typeof REC2; // reg ex
52
+ var ER2: typeof ER2I; // error
46
53
  var D2: typeof D2I; // Date
47
54
  var A2: typeof A2I; // Array
48
55
  var O2: typeof O2I; // Object
@@ -52,17 +59,30 @@ declare global {
52
59
  var S2: typeof S2I; // String
53
60
  var N2: typeof N2I; // Number
54
61
  var USP2: typeof USP2I; // URLSearchParams
55
- var ER2: typeof ER2I; // Error
56
62
  var U82: typeof U82I; // Uint8Array
57
63
  var I2: typeof I2I; // Iterator
58
64
  var R2: typeof R2I;
59
65
  var R2FN: typeof R2II;
66
+ var soa_codec: typeof soa_codeci;
67
+ interface soa_codec {
68
+ readonly maxPids: number;
69
+ readonly channelMap: Int32Array;
70
+ readonly subChannelMap: Int32Array;
71
+ readonly tripChannelMap: Int32Array;
72
+ readonly keyMap: Int32Array;
73
+ readonly listeners: Array<((pid: number, val?: number) => void) | undefined>;
74
+ nextPid: number;
75
+
76
+ allocatePid(rId: number, mId: number, sId: number, kId: number): number;
77
+ }
78
+ interface soa_codeci extends soa_codec {}
79
+
60
80
  // [MAP]
61
81
  // #region ══════════════════════════════════════════════════════════════════════════════════════════════════════
62
82
  type M2Op = "set" | "del" | "upd" | "dirty";
63
83
  interface M2Event<H = A, T = A> {
64
84
  op: M2Op;
65
- pid: H;
85
+ pid?: H;
66
86
  k: H;
67
87
  v?: T;
68
88
  prev?: T;
package/src/error.ts ADDED
@@ -0,0 +1,81 @@
1
+
2
+
3
+ export class ER2I extends Error implements ER2 {
4
+ constructor(message = "", options?: ErrorOptions) {
5
+ super(message, options);
6
+ this.name = new.target.name;
7
+ Object.setPrototypeOf(this, new.target.prototype);
8
+ }
9
+
10
+ clone(): ER2I {
11
+ const error = new ER2I(this.message, { cause: this.cause });
12
+ error.name = this.name;
13
+ error.stack = this.stack;
14
+ return error;
15
+ }
16
+
17
+ toJSON(): {
18
+ name: string;
19
+ message: string;
20
+ stack?: string;
21
+ cause?: unknown;
22
+ } {
23
+ return {
24
+ name: this.name,
25
+ message: this.message,
26
+ stack: this.stack,
27
+ cause: this.cause,
28
+ };
29
+ }
30
+
31
+ override toString(): string {
32
+ return `${this.name}: ${this.message}`;
33
+ }
34
+
35
+ static is(value: unknown): value is Error {
36
+ return value instanceof Error;
37
+ }
38
+
39
+ static isER2(value: unknown): value is ER2I {
40
+ return value instanceof ER2I;
41
+ }
42
+
43
+ static from(value: unknown, fallback = "An unknown error occurred"): ER2I {
44
+ if (value instanceof ER2I) return value;
45
+
46
+ if (value instanceof Error) {
47
+ const error = new ER2I(value.message, { cause: value });
48
+ error.name = value.name;
49
+ error.stack = value.stack;
50
+ return error;
51
+ }
52
+
53
+ if (typeof value === "string") {
54
+ return new ER2I(value);
55
+ }
56
+
57
+ if (value === null || value === undefined) {
58
+ return new ER2I(fallback);
59
+ }
60
+
61
+ try {
62
+ return new ER2I(String(value));
63
+ } catch {
64
+ return new ER2I(fallback);
65
+ }
66
+ }
67
+
68
+ static wrap(value: unknown, message?: string): ER2I {
69
+ const cause = ER2I.from(value);
70
+ return new ER2I(message ?? cause.message, { cause });
71
+ }
72
+
73
+ static aggregate(errors: Iterable<unknown>, message = "Multiple errors occurred"): AggregateError {
74
+ return new AggregateError(
75
+ Array.from(errors, error => ER2I.from(error)),
76
+ message,
77
+ );
78
+ }
79
+ }
80
+
81
+ globalThis.ER2 = ER2I
package/src/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ import { M2RI } from "./map_rows";
2
+ import { M2DI } from "./map_dense";
3
+ import { M2BINI } from "./map_bin";
4
+ import { B2BI } from "./boolean_byte";
5
+ import { M2I } from "./map";
6
+ import { M2BI } from "./map_byte";
7
+ import { A2I } from './array'
8
+ import { D2I, R2I, R2II } from './date'
9
+ import { ER2I } from './error'
10
+ import { I2I } from './iterator'
11
+ import { N2I } from './number'
12
+ import { O2I } from './object'
13
+ import { P2I } from './promise'
14
+ import { REC2, RECC2 } from './reg_ex'
15
+ import { SET2I } from './set'
16
+ import { SET2BI } from './set_byte'
17
+ import { SOA_CODEC, soa_codeci } from './soa_codec'
18
+ import { S2I } from './string'
19
+ import { U82I } from './unit8array'
20
+ import { USP2I } from './url_search_params'
21
+ import { WM2I } from './weak_map'
22
+ import { WS2I } from './weak_set'
23
+ export type MaybePromise<R = any> = R;
24
+ export function MaybePromisei<R = any>(v: R): MaybePromise {
25
+ return v !== undefined ? v as R : Promise.resolve(v) as Promise<R>
26
+ }
27
+
28
+ globalThis.MaybePromise = MaybePromisei;
29
+
30
+ export {
31
+ M2RI as M2R,
32
+ M2DI as M2D,
33
+ M2BINI as M2BIN,
34
+ A2I as A2,
35
+ B2BI as B2B,
36
+ D2I as D2,
37
+ ER2I as ER2,
38
+ I2I as I2,
39
+ M2BI as M2B,
40
+ M2I as M2,
41
+ N2I as N2,
42
+ O2I as O2,
43
+ P2I as P2,
44
+ REC2 as RE2,
45
+ RECC2 as RES2,
46
+ SET2I as SET2,
47
+ SET2BI as SET2B,
48
+ SOA_CODEC ,
49
+ soa_codeci as soa_codec,
50
+ S2I as S2,
51
+ U82I as U82,
52
+ USP2I as USP2,
53
+ WM2I as WM2,
54
+ WS2I as WS2,
55
+ R2I as R2,
56
+ R2II as R2I
57
+ }
@@ -0,0 +1,407 @@
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;