@the-w1zard/dx 1.0.7 → 1.0.9

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.
@@ -1,187 +0,0 @@
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
package/src/weak_map.js DELETED
@@ -1,136 +0,0 @@
1
- export class WM2I extends WeakMap {
2
- g = this.get;
3
- h = this.has;
4
- d = this.delete;
5
- _tracked;
6
- s(k, v) {
7
- const n = typeof v === "function" ? v() : v;
8
- super.set(k, n);
9
- return this;
10
- }
11
- swap(key, value) {
12
- const previous = this.h(key) ? this.g(key) : undefined;
13
- this.s(key, value);
14
- return previous;
15
- }
16
- take(key) {
17
- const value = this.g(key);
18
- this.d(key);
19
- return value;
20
- }
21
- getAndDelete(key) {
22
- return this.take(key);
23
- }
24
- peek(key) {
25
- return this.h(key) ? this.g(key) : false;
26
- }
27
- getOrI(key, value) {
28
- if (this.h(key))
29
- return this.g(key);
30
- this.s(key, value);
31
- return value;
32
- }
33
- getOrM(key, make) {
34
- if (this.h(key))
35
- return this.g(key);
36
- const value = make();
37
- this.s(key, value);
38
- return value;
39
- }
40
- getOr(key, fallback) {
41
- return this.h(key) ? this.g(key) : fallback;
42
- }
43
- getOrThrow(key, error = new Error("WeakMap key not found")) {
44
- if (!this.h(key))
45
- throw error;
46
- return this.g(key);
47
- }
48
- getOrCreate(key, make) {
49
- return this.getOrM(key, make);
50
- }
51
- ensure(key, make) {
52
- return this.getOrM(key, make);
53
- }
54
- update(key, fn, initial) {
55
- const has = this.h(key);
56
- const value = fn(has ? this.g(key) : initial, has);
57
- this.s(key, value);
58
- return value;
59
- }
60
- setIfAbsent(key, value) {
61
- if (this.h(key))
62
- return false;
63
- this.s(key, value);
64
- return true;
65
- }
66
- setIfPresent(key, value) {
67
- if (!this.h(key))
68
- return false;
69
- this.s(key, value);
70
- return true;
71
- }
72
- hasOrSet(key, value) {
73
- if (this.h(key))
74
- return true;
75
- this.s(key, value);
76
- return false;
77
- }
78
- replace(key, value) {
79
- return this.setIfPresent(key, value);
80
- }
81
- touch(key) {
82
- return this.setIfAbsent(key, undefined);
83
- }
84
- get tracked() {
85
- return this._tracked !== undefined;
86
- }
87
- get trackedSize() {
88
- return this._tracked?.size ?? 0;
89
- }
90
- track(key) {
91
- (this._tracked ??= new Set()).add(key);
92
- return this;
93
- }
94
- untrack(key) {
95
- return this._tracked?.delete(key) ?? false;
96
- }
97
- trackedKeys() {
98
- return this._tracked ? Array.from(this._tracked) : [];
99
- }
100
- trackedValues() {
101
- const out = [];
102
- if (this._tracked) {
103
- for (const key of this._tracked) {
104
- if (this.h(key))
105
- out.push(this.g(key));
106
- }
107
- }
108
- return out;
109
- }
110
- trackedEntries() {
111
- const out = [];
112
- if (this._tracked) {
113
- for (const key of this._tracked) {
114
- if (this.h(key))
115
- out.push([key, this.g(key)]);
116
- }
117
- }
118
- return out;
119
- }
120
- forEachTracked(fn) {
121
- if (this._tracked) {
122
- for (const key of this._tracked) {
123
- if (this.h(key))
124
- fn(this.g(key), key, this);
125
- }
126
- }
127
- }
128
- clearTracked() {
129
- if (this._tracked) {
130
- for (const key of this._tracked)
131
- this.d(key);
132
- this._tracked.clear();
133
- }
134
- }
135
- }
136
- globalThis.WM2 = WM2I;
package/src/weak_map.ts DELETED
@@ -1,155 +0,0 @@
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;
package/src/weak_set.js DELETED
@@ -1,92 +0,0 @@
1
- export class WS2I extends WeakSet {
2
- _tracked = new Set();
3
- constructor(values) {
4
- super(values == null ? undefined : Array.from(values));
5
- }
6
- add(value) {
7
- return super.add(value);
8
- }
9
- delete(value) {
10
- this._tracked.delete(value);
11
- return super.delete(value);
12
- }
13
- has(value) {
14
- return super.has(value);
15
- }
16
- addIfAbsent(value) {
17
- if (this.has(value))
18
- return false;
19
- this.add(value);
20
- return true;
21
- }
22
- toggle(value) {
23
- if (this.has(value)) {
24
- this.delete(value);
25
- return false;
26
- }
27
- this.add(value);
28
- return true;
29
- }
30
- deleteIfPresent(value) {
31
- return this.delete(value);
32
- }
33
- hasAll(values) {
34
- for (const value of values) {
35
- if (!this.has(value))
36
- return false;
37
- }
38
- return true;
39
- }
40
- hasAny(values) {
41
- for (const value of values) {
42
- if (this.has(value))
43
- return true;
44
- }
45
- return false;
46
- }
47
- addAll(values) {
48
- for (const value of values)
49
- this.add(value);
50
- return this;
51
- }
52
- deleteAll(values) {
53
- let count = 0;
54
- for (const value of values) {
55
- if (this.delete(value))
56
- count++;
57
- }
58
- return count;
59
- }
60
- replace(oldValue, newValue) {
61
- if (!this.has(oldValue))
62
- return false;
63
- this.delete(oldValue);
64
- this.add(newValue);
65
- return true;
66
- }
67
- track(value) {
68
- this.add(value);
69
- this._tracked.add(value);
70
- return this;
71
- }
72
- untrack(value) {
73
- return this._tracked.delete(value);
74
- }
75
- trackedKeys() {
76
- return Array.from(this._tracked);
77
- }
78
- forEachTracked(callback, thisArg) {
79
- this._tracked.forEach(value => {
80
- if (this.has(value))
81
- callback.call(thisArg, value, this);
82
- else
83
- this._tracked.delete(value);
84
- });
85
- }
86
- clearTracked() {
87
- for (const value of this._tracked)
88
- super.delete(value);
89
- this._tracked.clear();
90
- }
91
- }
92
- globalThis.WS2 = WS2I;
package/src/weak_set.ts DELETED
@@ -1,101 +0,0 @@
1
-
2
- export class WS2I<T extends WeakKey = A> extends WeakSet<T> implements WS2<T> {
3
- private readonly _tracked = new Set<T>();
4
-
5
- constructor(values?: readonly T[] | Iterable<T> | null) {
6
- super(values == null ? undefined : Array.from(values));
7
- }
8
- override add(value: T): this {
9
- return super.add(value);
10
- }
11
-
12
- override delete(value: T): boolean {
13
- this._tracked.delete(value);
14
- return super.delete(value);
15
- }
16
-
17
- override has(value: T): boolean {
18
- return super.has(value);
19
- }
20
-
21
- addIfAbsent(value: T): boolean {
22
- if (this.has(value)) return false;
23
- this.add(value);
24
- return true;
25
- }
26
-
27
- toggle(value: T): boolean {
28
- if (this.has(value)) {
29
- this.delete(value);
30
- return false;
31
- }
32
- this.add(value);
33
- return true;
34
- }
35
-
36
- deleteIfPresent(value: T): boolean {
37
- return this.delete(value);
38
- }
39
-
40
- hasAll(values: Iterable<T>): boolean {
41
- for (const value of values) {
42
- if (!this.has(value)) return false;
43
- }
44
- return true;
45
- }
46
-
47
- hasAny(values: Iterable<T>): boolean {
48
- for (const value of values) {
49
- if (this.has(value)) return true;
50
- }
51
- return false;
52
- }
53
-
54
- addAll(values: Iterable<T>): this {
55
- for (const value of values) this.add(value);
56
- return this;
57
- }
58
-
59
- deleteAll(values: Iterable<T>): N {
60
- let count = 0;
61
- for (const value of values) {
62
- if (this.delete(value)) count++;
63
- }
64
- return count as N;
65
- }
66
-
67
- replace(oldValue: T, newValue: T): boolean {
68
- if (!this.has(oldValue)) return false;
69
- this.delete(oldValue);
70
- this.add(newValue);
71
- return true;
72
- }
73
-
74
- track(value: T): this {
75
- this.add(value);
76
- this._tracked.add(value);
77
- return this;
78
- }
79
-
80
- untrack(value: T): boolean {
81
- return this._tracked.delete(value);
82
- }
83
-
84
- trackedKeys(): T[] {
85
- return Array.from(this._tracked);
86
- }
87
-
88
- forEachTracked(callback: (value: T, set: this) => void, thisArg?: A): void {
89
- this._tracked.forEach(value => {
90
- if (this.has(value)) callback.call(thisArg, value, this);
91
- else this._tracked.delete(value);
92
- });
93
- }
94
-
95
- clearTracked(): void {
96
- for (const value of this._tracked) super.delete(value);
97
- this._tracked.clear();
98
- }
99
- }
100
-
101
- (globalThis as A).WS2 = WS2I as WS2C;