@the-w1zard/dx 1.0.11 → 1.0.13

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.
@@ -0,0 +1,374 @@
1
+
2
+ export class U82I<T = A> extends Array<T> implements U82<T> {
3
+ constructor(...items: T[]) {
4
+ super(...items);
5
+ Object.setPrototypeOf(this, new.target.prototype);
6
+ }
7
+
8
+ static get [Symbol.species](): ArrayConstructor {
9
+ return Array;
10
+ }
11
+
12
+ static override from<T>(arrayLike: ArrayLike<T>): U82I<T>;
13
+ static override from<T, U>(
14
+ arrayLike: ArrayLike<T>,
15
+ mapFn: (value: T, index: number) => U,
16
+ thisArg?: A,
17
+ ): U82I<U>;
18
+ static override from<T>(iterable: Iterable<T> | ArrayLike<T>): U82I<T>;
19
+ static override from<T, U>(
20
+ iterable: Iterable<T> | ArrayLike<T>,
21
+ mapFn?: (value: T, index: number) => U,
22
+ thisArg?: A,
23
+ ): U82I<T | U> {
24
+ const values: T[] =
25
+ Symbol.iterator in Object(iterable)
26
+ ? Array.from(iterable as Iterable<T>)
27
+ : Array.from(iterable as ArrayLike<T>);
28
+
29
+ const result: (T | U)[] = mapFn
30
+ ? values.map((value, index) => mapFn.call(thisArg, value, index))
31
+ : values;
32
+
33
+ return new U82I<T | U>(...result);
34
+ }
35
+
36
+ static override of<T>(...items: T[]): U82I<T> {
37
+ return new U82I(...items);
38
+ }
39
+
40
+ first(): T | undefined {
41
+ return this[0];
42
+ }
43
+
44
+ last(): T | undefined {
45
+ return this[this.length - 1];
46
+ }
47
+
48
+ second(): T | undefined {
49
+ return this[1];
50
+ }
51
+
52
+ third(): T | undefined {
53
+ return this[2];
54
+ }
55
+
56
+ random(): T | undefined {
57
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
58
+ }
59
+
60
+ isEmpty(): boolean {
61
+ return this.length === 0;
62
+ }
63
+
64
+ isNotEmpty(): boolean {
65
+ return this.length > 0;
66
+ }
67
+
68
+ isNotEmptyAndDefined(): boolean {
69
+ return this.some(value => value !== undefined && value !== null);
70
+ }
71
+
72
+ clear(): this {
73
+ this.length = 0;
74
+ return this;
75
+ }
76
+
77
+ clone(): U82I<T> {
78
+ return new U82I(...this);
79
+ }
80
+
81
+ unique(): U82I<T> {
82
+ return new U82I(...new Set(this));
83
+ }
84
+
85
+ compact(): U82I<NonNullable<T>> {
86
+ return new U82I(...this.filter((value): value is NonNullable<T> => value != null));
87
+ }
88
+
89
+ chunk(size: N): U82I<U82I<T>> {
90
+ if (size <= 0) throw new RangeError("Chunk size must be greater than zero");
91
+
92
+ const result = new U82I<U82I<T>>();
93
+
94
+ for (let i = 0; i < this.length; i += size) {
95
+ result.push(new U82I(...this.slice(i, i + size)));
96
+ }
97
+
98
+ return result;
99
+ }
100
+
101
+ flatten<D extends N = 1>(depth?: D): U82I<A> {
102
+ return new U82I(...(this.flat(depth) as A[]));
103
+ }
104
+
105
+ shuffle(): this {
106
+ return this.shuffleInPlace();
107
+ }
108
+
109
+ sample(count = 1 as N): U82I<T> {
110
+ return this.pick(count);
111
+ }
112
+
113
+ count(predicate?: (value: T, index: N, array: T[]) => unknown): N {
114
+ return (predicate ? this.filter(predicate).length : this.length) as N;
115
+ }
116
+
117
+ sum(this: U82I<number>): number {
118
+ return this.reduce((total, value) => total + value, 0);
119
+ }
120
+
121
+ average(this: U82I<number>): number {
122
+ return this.length ? this.sum() / this.length : 0;
123
+ }
124
+
125
+ min(this: U82I<number>): number | undefined {
126
+ return this.length ? Math.min(...this) : undefined;
127
+ }
128
+
129
+ max(this: U82I<number>): number | undefined {
130
+ return this.length ? Math.max(...this) : undefined;
131
+ }
132
+
133
+ groupBy<K extends PropertyKey>(keyFn: (value: T, index: N) => K): Map<K, U82I<T>> {
134
+ const result = new Map<K, U82I<T>>();
135
+
136
+ this.forEach((value, index) => {
137
+ const key = keyFn(value, index as N);
138
+
139
+ if (!result.has(key)) result.set(key, new U82I<T>());
140
+ result.get(key)!.push(value);
141
+ });
142
+
143
+ return result;
144
+ }
145
+
146
+ keyBy<K extends PropertyKey>(keyFn: (value: T, index: N) => K): Map<K, T> {
147
+ const result = new Map<K, T>();
148
+
149
+ this.forEach((value, index) => result.set(keyFn(value, index as N), value));
150
+
151
+ return result;
152
+ }
153
+
154
+ partition(predicate: (value: T, index: N, array: T[]) => unknown): [U82I<T>, U82I<T>] {
155
+ const yes = new U82I<T>();
156
+ const no = new U82I<T>();
157
+
158
+ this.forEach((value, index, array) => {
159
+ (predicate(value, index as N, array) ? yes : no).push(value);
160
+ });
161
+
162
+ return [yes, no];
163
+ }
164
+
165
+ difference(other: Iterable<T>): U82I<T> {
166
+ const values = new Set(other);
167
+ return new U82I(...this.filter(value => !values.has(value)));
168
+ }
169
+
170
+ intersection(other: Iterable<T>): U82I<T> {
171
+ const values = new Set(other);
172
+ return new U82I(...this.filter(value => values.has(value)));
173
+ }
174
+
175
+ union(other: Iterable<T>): U82I<T> {
176
+ return new U82I(...new Set([...this, ...other]));
177
+ }
178
+
179
+ symmetricDifference(other: Iterable<T>): U82I<T> {
180
+ const values = new Set(other);
181
+ const own = new Set(this);
182
+
183
+ return new U82I(
184
+ ...[...this.filter(value => !values.has(value)), ...Array.from(values).filter(value => !own.has(value))],
185
+ );
186
+ }
187
+
188
+ take(count: N): U82I<T> {
189
+ return new U82I(...this.slice(0, Math.max(0, count)));
190
+ }
191
+
192
+ takeLast(count: N): U82I<T> {
193
+ return new U82I(...this.slice(-Math.max(0, count)));
194
+ }
195
+
196
+ drop(count: N): U82I<T> {
197
+ return new U82I(...this.slice(Math.max(0, count)));
198
+ }
199
+
200
+ dropLast(count: N): U82I<T> {
201
+ return new U82I(...this.slice(0, Math.max(0, this.length - count)));
202
+ }
203
+
204
+ pluck<K extends keyof T>(key: K): U82I<T[K]> {
205
+ return new U82I(...this.map(value => value[key]));
206
+ }
207
+
208
+ flattenDeep(): U82I<A> {
209
+ return new U82I(...(this.flat(Infinity) as A[]));
210
+ }
211
+
212
+ insert(index: N, ...items: T[]): this {
213
+ this.splice(index, 0, ...items);
214
+ return this;
215
+ }
216
+
217
+ removeAt(index: N): T | undefined {
218
+ if (index < 0 || index >= this.length) return undefined;
219
+ return this.splice(index, 1)[0];
220
+ }
221
+
222
+ remove(value: T): boolean {
223
+ const index = this.indexOf(value);
224
+
225
+ if (index === -1) return false;
226
+
227
+ this.splice(index, 1);
228
+ return true;
229
+ }
230
+
231
+ removeWhere(predicate: (value: T, index: N, array: T[]) => unknown): this {
232
+ for (let i = this.length - 1; i >= 0; i--) {
233
+ if (predicate(this[i], i as N, this)) this.splice(i, 1);
234
+ }
235
+
236
+ return this;
237
+ }
238
+
239
+ replace(oldValue: T, newValue: T): boolean {
240
+ const index = this.indexOf(oldValue);
241
+
242
+ if (index === -1) return false;
243
+
244
+ this[index] = newValue;
245
+ return true;
246
+ }
247
+
248
+ move(from: N, to: N): this {
249
+ if (from < 0 || from >= this.length || to < 0 || to >= this.length) return this;
250
+
251
+ const [value] = this.splice(from, 1);
252
+ this.splice(to, 0, value);
253
+
254
+ return this;
255
+ }
256
+
257
+ swap(i: N, j: N): this {
258
+ if (i < 0 || j < 0 || i >= this.length || j >= this.length) return this;
259
+
260
+ [this[i], this[j]] = [this[j], this[i]];
261
+
262
+ return this;
263
+ }
264
+
265
+ shuffleInPlace(): this {
266
+ for (let i = this.length - 1; i > 0; i--) {
267
+ const j = Math.floor(Math.random() * (i + 1));
268
+ [this[i], this[j]] = [this[j], this[i]];
269
+ }
270
+
271
+ return this;
272
+ }
273
+
274
+ pick(count = 1 as N): U82I<T> {
275
+ const values = [...this];
276
+ const result = new U82I<T>();
277
+
278
+ for (let i = 0; i < Math.min(Math.max(0, count), values.length); i++) {
279
+ const j = Math.floor(Math.random() * values.length);
280
+ result.push(values.splice(j, 1)[0]);
281
+ }
282
+
283
+ return result;
284
+ }
285
+
286
+ toSet(): Set<T> {
287
+ return new Set(this);
288
+ }
289
+
290
+ toMap<K extends PropertyKey>(keyFn: (value: T, index: N) => K): Map<K, T> {
291
+ return this.keyBy(keyFn);
292
+ }
293
+
294
+ equals(other: readonly T[]): boolean {
295
+ return this.length === other.length && this.every((value, index) => Object.is(value, other[index]));
296
+ }
297
+
298
+ startsWith(other: readonly T[]): boolean {
299
+ return other.length <= this.length && other.every((value, index) => Object.is(this[index], value));
300
+ }
301
+
302
+ endsWith(other: readonly T[]): boolean {
303
+ return (
304
+ other.length <= this.length &&
305
+ other.every((value, index) => Object.is(this[this.length - other.length + index], value))
306
+ );
307
+ }
308
+
309
+ isSorted(compareFn: (a: T, b: T) => N = ((a, b) => (a < b ? -1 : a > b ? 1 : 0) as N)): boolean {
310
+ for (let i = 1; i < this.length; i++) {
311
+ if (compareFn(this[i - 1], this[i]) > 0) return false;
312
+ }
313
+
314
+ return true;
315
+ }
316
+
317
+ frequencies(): Map<T, N> {
318
+ const result = new Map<T, N>();
319
+
320
+ for (const value of this) {
321
+ result.set(value, ((result.get(value) ?? 0) + 1) as N);
322
+ }
323
+
324
+ return result;
325
+ }
326
+ findLast<S extends T>(
327
+ predicate: (value: T, index: N, obj: T[]) => value is S,
328
+ thisArg?: A,
329
+ ): S | undefined;
330
+ findLast(
331
+ predicate: (value: T, index: N, obj: T[]) => unknown,
332
+ thisArg?: A,
333
+ ): T | undefined {
334
+ for (let i = this.length - 1; i >= 0; i--) {
335
+ if (predicate.call(thisArg, this[i], i as N, this)) return this[i];
336
+ }
337
+ return undefined;
338
+ }
339
+
340
+ findLastIndex(predicate: (value: T, index: N, obj: T[]) => unknown, thisArg?: A): N {
341
+ for (let i = this.length - 1; i >= 0; i--) {
342
+ if (predicate.call(thisArg, this[i], i as N, this)) return i as N;
343
+ }
344
+ return -1 as N;
345
+ }
346
+
347
+ toReversed(): T[] {
348
+ return [...this].reverse();
349
+ }
350
+
351
+ toSorted(compareFn?: (a: T, b: T) => N): T[] {
352
+ return [...this].sort(compareFn);
353
+ }
354
+
355
+ toSpliced(start: N, deleteCount: N, ...items: T[]): T[] {
356
+ const result = [...this];
357
+ result.splice(start, deleteCount, ...items);
358
+ return result;
359
+ }
360
+
361
+ with(index: N, value: T): T[] {
362
+ const result = [...this];
363
+ const actualIndex = index < 0 ? result.length + index : index;
364
+
365
+ if (actualIndex < 0 || actualIndex >= result.length) {
366
+ throw new RangeError("Invalid index");
367
+ }
368
+
369
+ result[actualIndex] = value;
370
+ return result;
371
+ }
372
+ }
373
+
374
+ globalThis.U82 = U82I;
@@ -0,0 +1,187 @@
1
+
2
+
3
+ export class USP2I extends URLSearchParams implements USP2 {
4
+ constructor(init?: S | URLSearchParams | R<S, S> | S[][]) {
5
+ super(init as S | URLSearchParams | R<S, S> | S[][] | UD);
6
+ }
7
+
8
+ get size(): N {
9
+ return Array.from(this).length as N;
10
+ }
11
+
12
+ getOr(name: S, fallback: S): S {
13
+ return this.get(name) ?? fallback;
14
+ }
15
+
16
+ getOrNull(name: S): S | NU {
17
+ return this.get(name);
18
+ }
19
+
20
+ getOrThrow(name: S, error: A = new Error(`Missing URL parameter: ${name}`)): string {
21
+ const value = this.get(name);
22
+ if (value === null) throw error;
23
+ return value;
24
+ }
25
+
26
+ getNumber(name: S, fallback?: N): N | UD {
27
+ const value = this.get(name);
28
+ if (value === null || value.trim() === "") return fallback;
29
+ const number = Number(value);
30
+ return Number.isFinite(number) ? number : fallback;
31
+ }
32
+
33
+ getInt(name: S, radix: N = 10 as N): N | UD {
34
+ const value = this.get(name);
35
+ if (value === null || value.trim() === "") return undefined;
36
+ const number = Number.parseInt(value, radix);
37
+ return Number.isNaN(number) ? undefined : number;
38
+ }
39
+
40
+ getFloat(name: S): N | UD {
41
+ const value = this.get(name);
42
+ if (value === null || value.trim() === "") return undefined;
43
+ const number = Number.parseFloat(value);
44
+ return Number.isNaN(number) ? undefined : number;
45
+ }
46
+
47
+ getBoolean(name: S, fallback = false): B {
48
+ const value = this.get(name);
49
+ if (value === null) return fallback;
50
+ switch (value.trim().toLowerCase()) {
51
+ case "true":
52
+ case "1":
53
+ case "yes":
54
+ case "on":
55
+ return true;
56
+ case "false":
57
+ case "0":
58
+ case "no":
59
+ case "off":
60
+ return false;
61
+ default:
62
+ return fallback;
63
+ }
64
+ }
65
+
66
+ getJSON<T = A>(name: S): T | UD {
67
+ const value = this.get(name);
68
+ if (value === null) return undefined;
69
+ try {
70
+ return JSON.parse(value) as T;
71
+ } catch {
72
+ return undefined;
73
+ }
74
+ }
75
+
76
+ getArray(name: S, separator = ","): S[] {
77
+ const value = this.get(name);
78
+ return value === null || value === "" ? [] : value.split(separator);
79
+ }
80
+
81
+ getObject<T extends R<S, A> = R<S, A>>(): T {
82
+ const result: R<S, A> = {};
83
+ for (const key of new Set(this.keys())) {
84
+ const values = this.getAll(key);
85
+ result[key] = values.length > 1 ? values : values[0];
86
+ }
87
+ return result as T;
88
+ }
89
+
90
+ setNumber(name: S, value: N): this {
91
+ this.set(name, String(value));
92
+ return this;
93
+ }
94
+
95
+ setBoolean(name: S, value: B): this {
96
+ this.set(name, String(value));
97
+ return this;
98
+ }
99
+
100
+ setJSON(name: S, value: A): this {
101
+ this.set(name, JSON.stringify(value));
102
+ return this;
103
+ }
104
+
105
+ setArray(name: S, values: Iterable<A>, separator = ","): this {
106
+ this.set(name, Array.from(values, String).join(separator));
107
+ return this;
108
+ }
109
+
110
+ setIfAbsent(name: S, value: S): this {
111
+ if (!this.has(name)) this.set(name, value);
112
+ return this;
113
+ }
114
+
115
+ setIfPresent(name: S, value: S): this {
116
+ if (this.has(name)) this.set(name, value);
117
+ return this;
118
+ }
119
+
120
+ appendAll(params: URLSearchParams | R<S, A>): this {
121
+ if (params instanceof URLSearchParams) {
122
+ params.forEach((value, key) => this.append(key, value));
123
+ } else {
124
+ for (const [key, value] of Object.entries(params)) {
125
+ if (value != null) {
126
+ if (Array.isArray(value)) {
127
+ for (const item of value) this.append(key, String(item));
128
+ } else {
129
+ this.append(key, String(value));
130
+ }
131
+ }
132
+ }
133
+ }
134
+ return this;
135
+ }
136
+
137
+ merge(params: URLSearchParams | R<S, A>, overwrite = true): this {
138
+ if (params instanceof URLSearchParams) {
139
+ params.forEach((value, key) => {
140
+ if (overwrite || !this.has(key)) this.set(key, value);
141
+ });
142
+ } else {
143
+ for (const [key, value] of Object.entries(params)) {
144
+ if (value != null && (overwrite || !this.has(key))) {
145
+ this.set(key, String(value));
146
+ }
147
+ }
148
+ }
149
+ return this;
150
+ }
151
+
152
+ clone(): USP2I {
153
+ return new USP2I(this);
154
+ }
155
+
156
+ clear(): V {
157
+ for (const key of new Set(this.keys())) this.delete(key);
158
+ }
159
+
160
+ isEmpty(): B {
161
+ return this.size === 0;
162
+ }
163
+
164
+ isNotEmpty(): B {
165
+ return this.size > 0;
166
+ }
167
+
168
+ toObject(): R<S, S | S[]> {
169
+ return this.getObject();
170
+ }
171
+
172
+ toJSON(): R<S, S | S[]> {
173
+ return this.toObject();
174
+ }
175
+
176
+ toURLSearchParams(): URLSearchParams {
177
+ return new URLSearchParams(this);
178
+ }
179
+
180
+ toURL(url: S | URL): URL {
181
+ const result = new URL(url);
182
+ this.forEach((value, key) => result.searchParams.append(key, value));
183
+ return result;
184
+ }
185
+ }
186
+
187
+ globalThis.USP2 = USP2I
@@ -0,0 +1,155 @@
1
+
2
+ export class WM2I<H extends WeakKey = A, T = A> extends WeakMap<H, T> implements WM2<H, T> {
3
+ g = this.get;
4
+ h = this.has;
5
+ d = this.delete;
6
+
7
+ private _tracked?: Set<H>;
8
+
9
+ s(k: H, v: T | (() => T)): this {
10
+ const n = typeof v === "function" ? (v as () => T)() : v;
11
+ super.set(k, n);
12
+ return this;
13
+ }
14
+
15
+ swap(key: H, value: T): T | UD {
16
+ const previous = this.h(key) ? this.g(key) : undefined;
17
+ this.s(key, value);
18
+ return previous as T | UD;
19
+ }
20
+
21
+ take(key: H): T | UD {
22
+ const value = this.g(key);
23
+ this.d(key);
24
+ return value;
25
+ }
26
+
27
+ getAndDelete(key: H): T | UD {
28
+ return this.take(key);
29
+ }
30
+
31
+ peek(key: H): T | false {
32
+ return this.h(key) ? (this.g(key) as T) : false;
33
+ }
34
+
35
+ getOrI(key: H, value: T): T {
36
+ if (this.h(key)) return this.g(key) as T;
37
+ this.s(key, value);
38
+ return value;
39
+ }
40
+
41
+ getOrM(key: H, make: () => T): T {
42
+ if (this.h(key)) return this.g(key) as T;
43
+ const value = make();
44
+ this.s(key, value);
45
+ return value;
46
+ }
47
+
48
+ getOr<G>(key: H, fallback: G): T | G {
49
+ return this.h(key) ? (this.g(key) as T) : fallback;
50
+ }
51
+
52
+ getOrThrow(key: H, error: Error = new Error("WeakMap key not found")): T {
53
+ if (!this.h(key)) throw error;
54
+ return this.g(key) as T;
55
+ }
56
+
57
+ getOrCreate(key: H, make: () => T): T {
58
+ return this.getOrM(key, make);
59
+ }
60
+
61
+ ensure(key: H, make: () => T): T {
62
+ return this.getOrM(key, make);
63
+ }
64
+
65
+ update(key: H, fn: (old: T, has: B) => T, initial: T): T {
66
+ const has = this.h(key);
67
+ const value = fn(has ? (this.g(key) as T) : initial, has);
68
+ this.s(key, value);
69
+ return value;
70
+ }
71
+
72
+ setIfAbsent(key: H, value: T): B {
73
+ if (this.h(key)) return false;
74
+ this.s(key, value);
75
+ return true;
76
+ }
77
+
78
+ setIfPresent(key: H, value: T): B {
79
+ if (!this.h(key)) return false;
80
+ this.s(key, value);
81
+ return true;
82
+ }
83
+
84
+ hasOrSet(key: H, value: T): B {
85
+ if (this.h(key)) return true;
86
+ this.s(key, value);
87
+ return false;
88
+ }
89
+
90
+ replace(key: H, value: T): B {
91
+ return this.setIfPresent(key, value);
92
+ }
93
+
94
+ touch(key: H): B {
95
+ return this.setIfAbsent(key, undefined as unknown as T);
96
+ }
97
+
98
+ get tracked(): B {
99
+ return this._tracked !== undefined;
100
+ }
101
+
102
+ get trackedSize(): N {
103
+ return this._tracked?.size ?? 0;
104
+ }
105
+
106
+ track(key: H): this {
107
+ (this._tracked ??= new Set()).add(key);
108
+ return this;
109
+ }
110
+
111
+ untrack(key: H): B {
112
+ return this._tracked?.delete(key) ?? false;
113
+ }
114
+
115
+ trackedKeys(): AR<H> {
116
+ return this._tracked ? Array.from(this._tracked) : [];
117
+ }
118
+
119
+ trackedValues(): AR<T> {
120
+ const out: T[] = [];
121
+ if (this._tracked) {
122
+ for (const key of this._tracked) {
123
+ if (this.h(key)) out.push(this.g(key) as T);
124
+ }
125
+ }
126
+ return out as AR<T>;
127
+ }
128
+
129
+ trackedEntries(): AR<[H, T]> {
130
+ const out: [H, T][] = [];
131
+ if (this._tracked) {
132
+ for (const key of this._tracked) {
133
+ if (this.h(key)) out.push([key, this.g(key) as T]);
134
+ }
135
+ }
136
+ return out as AR<[H, T]>;
137
+ }
138
+
139
+ forEachTracked(fn: (value: T, key: H, map: this) => void): void {
140
+ if (this._tracked) {
141
+ for (const key of this._tracked) {
142
+ if (this.h(key)) fn(this.g(key) as T, key, this);
143
+ }
144
+ }
145
+ }
146
+
147
+ clearTracked(): void {
148
+ if (this._tracked) {
149
+ for (const key of this._tracked) this.d(key);
150
+ this._tracked.clear();
151
+ }
152
+ }
153
+ }
154
+
155
+ globalThis.WM2 = WM2I;