@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.
- package/README.html +823 -121
- package/README.md +288 -107
- package/dist/index.js +12967 -0
- package/package.json +22 -7
- package/src/dx.d.ts +2 -10
- 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/number.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export class N2I extends Number implements N2 {
|
|
4
|
-
abs(): N2I {
|
|
5
|
-
return new N2I(Math.abs(this.valueOf()));
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
sign(): N2I {
|
|
9
|
-
return new N2I(Math.sign(this.valueOf()));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
neg(): N2I {
|
|
13
|
-
return new N2I(-this.valueOf());
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
inv(): N2I {
|
|
17
|
-
return new N2I(1 / this.valueOf());
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
clamp(min: N, max: N): N2I {
|
|
21
|
-
return new N2I(Math.min(Math.max(this.valueOf(), min), max));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
wrap(min: N, max: N): N2I {
|
|
25
|
-
const range = max - min;
|
|
26
|
-
if (range <= 0) throw new RangeError("max must be greater than min");
|
|
27
|
-
return new N2I((((this.valueOf() - min) % range) + range) % range + min);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
lerp(to: N, t: N): N2I {
|
|
31
|
-
return new N2I(this.valueOf() + (to - this.valueOf()) * t);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
map(inMin: N, inMax: N, outMin: N, outMax: N): N2I {
|
|
35
|
-
if (inMax === inMin) throw new RangeError("Input range cannot be zero");
|
|
36
|
-
return new N2I(outMin + ((this.valueOf() - inMin) / (inMax - inMin)) * (outMax - outMin));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
remap(inMin: N, inMax: N, outMin: N, outMax: N): N2I {
|
|
40
|
-
return this.map(inMin, inMax, outMin, outMax);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
roundTo(precision: N = 0): N2I {
|
|
44
|
-
const factor = 10 ** precision;
|
|
45
|
-
return new N2I(Math.round((this.valueOf() + Number.EPSILON) * factor) / factor);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
floorTo(precision: N = 0): N2I {
|
|
49
|
-
const factor = 10 ** precision;
|
|
50
|
-
return new N2I(Math.floor(this.valueOf() * factor) / factor);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
ceilTo(precision: N = 0): N2I {
|
|
54
|
-
const factor = 10 ** precision;
|
|
55
|
-
return new N2I(Math.ceil(this.valueOf() * factor) / factor);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
truncTo(precision: N = 0): N2I {
|
|
59
|
-
const factor = 10 ** precision;
|
|
60
|
-
return new N2I(Math.trunc(this.valueOf() * factor) / factor);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
isFinite(): B {
|
|
64
|
-
return Number.isFinite(this.valueOf());
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
isInteger(): B {
|
|
68
|
-
return Number.isInteger(this.valueOf());
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
isSafeInteger(): B {
|
|
72
|
-
return Number.isSafeInteger(this.valueOf());
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
isNaN(): B {
|
|
76
|
-
return Number.isNaN(this.valueOf());
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
isBetween(min: N, max: N, inclusive: B = true): B {
|
|
80
|
-
const value = this.valueOf();
|
|
81
|
-
return inclusive ? value >= min && value <= max : value > min && value < max;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
isPositive(): B {
|
|
85
|
-
return this.valueOf() > 0;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
isNegative(): B {
|
|
89
|
-
return this.valueOf() < 0;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
isZero(): B {
|
|
93
|
-
return this.valueOf() === 0;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
isEven(): B {
|
|
97
|
-
const value = this.valueOf();
|
|
98
|
-
return Number.isInteger(value) && value % 2 === 0;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
isOdd(): B {
|
|
102
|
-
const value = this.valueOf();
|
|
103
|
-
return Number.isInteger(value) && Math.abs(value % 2) === 1;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
squared(): N2I {
|
|
107
|
-
return new N2I(this.valueOf() ** 2);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
cubed(): N2I {
|
|
111
|
-
return new N2I(this.valueOf() ** 3);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
sqrt(): N2I {
|
|
115
|
-
return new N2I(Math.sqrt(this.valueOf()));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
cbrt(): N2I {
|
|
119
|
-
return new N2I(Math.cbrt(this.valueOf()));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
pow(exponent: N): N2I {
|
|
123
|
-
return new N2I(this.valueOf() ** exponent);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
root(degree: N): N2I {
|
|
127
|
-
if (degree === 0) throw new RangeError("Root degree cannot be zero");
|
|
128
|
-
const value = this.valueOf();
|
|
129
|
-
if (value < 0 && Number.isInteger(degree) && Math.abs(degree % 2) === 1) {
|
|
130
|
-
return new N2I(-((-value) ** (1 / degree)));
|
|
131
|
-
}
|
|
132
|
-
return new N2I(value ** (1 / degree));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
mod(divisor: N): N2I {
|
|
136
|
-
if (divisor === 0) throw new RangeError("Divisor cannot be zero");
|
|
137
|
-
return new N2I(((this.valueOf() % divisor) + divisor) % divisor);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
percentOf(total: N): N2I {
|
|
141
|
-
return new N2I((this.valueOf() / 100) * total);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
percentChange(from: N): N2I {
|
|
145
|
-
if (from === 0) throw new RangeError("Cannot calculate percentage change from zero");
|
|
146
|
-
return new N2I(((this.valueOf() - from) / Math.abs(from)) * 100);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
toRadians(): N2I {
|
|
150
|
-
return new N2I(this.valueOf() * (Math.PI / 180));
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
toDegrees(): N2I {
|
|
154
|
-
return new N2I(this.valueOf() * (180 / Math.PI));
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
toOrdinal(): S {
|
|
158
|
-
const value = this.valueOf();
|
|
159
|
-
const integer = Math.trunc(value);
|
|
160
|
-
const abs = Math.abs(integer);
|
|
161
|
-
const suffix = abs % 100 >= 11 && abs % 100 <= 13
|
|
162
|
-
? "th"
|
|
163
|
-
: ({ 1: "st", 2: "nd", 3: "rd" } as Record<N, S>)[abs % 10] ?? "th";
|
|
164
|
-
return `${value}${suffix}`;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
toBytes(unit?: N, decimals: N = 2): S {
|
|
168
|
-
const value = this.valueOf();
|
|
169
|
-
const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
|
|
170
|
-
const index = unit === undefined
|
|
171
|
-
? value === 0 ? 0 : Math.min(Math.floor(Math.log(Math.abs(value)) / Math.log(1024)), units.length - 1)
|
|
172
|
-
: Math.max(0, Math.min(Math.trunc(unit), units.length - 1));
|
|
173
|
-
return `${(value / 1024 ** index).toFixed(decimals)} ${units[index]}`;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
toCompact(locales?: Intl.LocalesArgument): S {
|
|
177
|
-
return new Intl.NumberFormat(locales, { notation: "compact", maximumFractionDigits: 2 }).format(this.valueOf());
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
toCurrency(currency: S = "USD", locales?: Intl.LocalesArgument): S {
|
|
181
|
-
return new Intl.NumberFormat(locales, { style: "currency", currency }).format(this.valueOf());
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
toPercent(decimals: N = 0, locales?: Intl.LocalesArgument): S {
|
|
185
|
-
return new Intl.NumberFormat(locales, {
|
|
186
|
-
style: "percent",
|
|
187
|
-
maximumFractionDigits: decimals,
|
|
188
|
-
}).format(this.valueOf());
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
times(fn: (index: N) => void): this {
|
|
192
|
-
const count = this.valueOf();
|
|
193
|
-
for (let i = 0; i < count; i++) fn(i);
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
upTo(end: N, step: N = 1): AR<N> {
|
|
198
|
-
if (step <= 0) throw new RangeError("step must be positive");
|
|
199
|
-
const out: N[] = [];
|
|
200
|
-
for (let value = this.valueOf(); value <= end; value += step) out.push(value);
|
|
201
|
-
return out as AR<N>;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
downTo(end: N, step: N = 1): AR<N> {
|
|
205
|
-
if (step <= 0) throw new RangeError("step must be positive");
|
|
206
|
-
const out: N[] = [];
|
|
207
|
-
for (let value = this.valueOf(); value >= end; value -= step) out.push(value);
|
|
208
|
-
return out as AR<N>;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
globalThis.N2 = N2I;
|
package/src/object.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
export class O2I {
|
|
2
|
-
static isEmpty(value) {
|
|
3
|
-
return Reflect.ownKeys(value).length === 0;
|
|
4
|
-
}
|
|
5
|
-
static isNotEmpty(value) {
|
|
6
|
-
return Reflect.ownKeys(value).length !== 0;
|
|
7
|
-
}
|
|
8
|
-
static size(value) {
|
|
9
|
-
return Reflect.ownKeys(value).length;
|
|
10
|
-
}
|
|
11
|
-
static isPlainObject(value) {
|
|
12
|
-
if (value === null || typeof value !== "object")
|
|
13
|
-
return false;
|
|
14
|
-
const prototype = Object.getPrototypeOf(value);
|
|
15
|
-
return prototype === Object.prototype || prototype === null;
|
|
16
|
-
}
|
|
17
|
-
static isObject(value) {
|
|
18
|
-
return value !== null && typeof value === "object";
|
|
19
|
-
}
|
|
20
|
-
static get(value, path, fallback) {
|
|
21
|
-
const parts = typeof path === "string" ? path.split(".") : path;
|
|
22
|
-
let current = value;
|
|
23
|
-
for (const key of parts) {
|
|
24
|
-
if (current === null || (typeof current !== "object" && typeof current !== "function")) {
|
|
25
|
-
return fallback;
|
|
26
|
-
}
|
|
27
|
-
current = current[key];
|
|
28
|
-
}
|
|
29
|
-
return current === undefined ? fallback : current;
|
|
30
|
-
}
|
|
31
|
-
static getOr(value, path, fallback) {
|
|
32
|
-
return this.get(value, path, fallback);
|
|
33
|
-
}
|
|
34
|
-
static set(value, path, next) {
|
|
35
|
-
const parts = typeof path === "string" ? path.split(".") : path;
|
|
36
|
-
if (!parts.length)
|
|
37
|
-
return value;
|
|
38
|
-
let current = value;
|
|
39
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
40
|
-
const key = parts[i];
|
|
41
|
-
const child = current[key];
|
|
42
|
-
if (child === null || (typeof child !== "object" && typeof child !== "function")) {
|
|
43
|
-
current[key] = {};
|
|
44
|
-
}
|
|
45
|
-
current = current[key];
|
|
46
|
-
}
|
|
47
|
-
current[parts[parts.length - 1]] = next;
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
|
-
static unset(value, path) {
|
|
51
|
-
const parts = typeof path === "string" ? path.split(".") : path;
|
|
52
|
-
if (!parts.length)
|
|
53
|
-
return false;
|
|
54
|
-
let current = value;
|
|
55
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
56
|
-
const child = current[parts[i]];
|
|
57
|
-
if (child === null || typeof child !== "object")
|
|
58
|
-
return false;
|
|
59
|
-
current = child;
|
|
60
|
-
}
|
|
61
|
-
return Reflect.deleteProperty(current, parts[parts.length - 1]);
|
|
62
|
-
}
|
|
63
|
-
static hasPath(value, path) {
|
|
64
|
-
const parts = typeof path === "string" ? path.split(".") : path;
|
|
65
|
-
let current = value;
|
|
66
|
-
for (const key of parts) {
|
|
67
|
-
if (current === null || (typeof current !== "object" && typeof current !== "function")) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
if (!Object.prototype.hasOwnProperty.call(current, key))
|
|
71
|
-
return false;
|
|
72
|
-
current = current[key];
|
|
73
|
-
}
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
static clone(value) {
|
|
77
|
-
if (value === null || typeof value !== "object")
|
|
78
|
-
return value;
|
|
79
|
-
if (Array.isArray(value))
|
|
80
|
-
return value.slice();
|
|
81
|
-
return Object.assign(Object.create(Object.getPrototypeOf(value)), value);
|
|
82
|
-
}
|
|
83
|
-
static deepClone(value) {
|
|
84
|
-
return structuredClone(value);
|
|
85
|
-
}
|
|
86
|
-
static deepMerge(target, source) {
|
|
87
|
-
const result = { ...target };
|
|
88
|
-
for (const key of Reflect.ownKeys(source)) {
|
|
89
|
-
const incoming = source[key];
|
|
90
|
-
const current = result[key];
|
|
91
|
-
result[key] =
|
|
92
|
-
this.isPlainObject(current) && this.isPlainObject(incoming)
|
|
93
|
-
? this.deepMerge(current, incoming)
|
|
94
|
-
: incoming;
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
static defaults(target, defaults) {
|
|
99
|
-
return Object.assign({}, defaults, target);
|
|
100
|
-
}
|
|
101
|
-
static getOrCreate(value, key, create) {
|
|
102
|
-
const object = value;
|
|
103
|
-
if (!Object.prototype.hasOwnProperty.call(object, key) || object[key] === undefined) {
|
|
104
|
-
object[key] = create();
|
|
105
|
-
}
|
|
106
|
-
return object;
|
|
107
|
-
}
|
|
108
|
-
static pick(value, keys) {
|
|
109
|
-
const result = {};
|
|
110
|
-
for (const key of keys) {
|
|
111
|
-
if (Object.prototype.hasOwnProperty.call(value, key))
|
|
112
|
-
result[key] = value[key];
|
|
113
|
-
}
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
static omit(value, keys) {
|
|
117
|
-
const result = { ...value };
|
|
118
|
-
for (const key of keys)
|
|
119
|
-
delete result[key];
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
static pickBy(value, predicate) {
|
|
123
|
-
const result = {};
|
|
124
|
-
for (const key of Object.keys(value)) {
|
|
125
|
-
if (predicate(value[key], key))
|
|
126
|
-
result[key] = value[key];
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
static omitBy(value, predicate) {
|
|
131
|
-
const result = {};
|
|
132
|
-
for (const key of Object.keys(value)) {
|
|
133
|
-
if (!predicate(value[key], key))
|
|
134
|
-
result[key] = value[key];
|
|
135
|
-
}
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
static mapKeys(value, mapper) {
|
|
139
|
-
const result = {};
|
|
140
|
-
for (const key of Object.keys(value)) {
|
|
141
|
-
result[mapper(key, value[key])] = value[key];
|
|
142
|
-
}
|
|
143
|
-
return result;
|
|
144
|
-
}
|
|
145
|
-
static mapValues(value, mapper) {
|
|
146
|
-
const result = {};
|
|
147
|
-
for (const key of Object.keys(value)) {
|
|
148
|
-
result[key] = mapper(value[key], key);
|
|
149
|
-
}
|
|
150
|
-
return result;
|
|
151
|
-
}
|
|
152
|
-
static mapEntries(value, mapper) {
|
|
153
|
-
const result = {};
|
|
154
|
-
for (const key of Object.keys(value)) {
|
|
155
|
-
const [nextKey, nextValue] = mapper([key, value[key]]);
|
|
156
|
-
result[nextKey] = nextValue;
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
}
|
|
160
|
-
static filter(value, predicate) {
|
|
161
|
-
return this.pickBy(value, predicate);
|
|
162
|
-
}
|
|
163
|
-
static isEqual(first, second) {
|
|
164
|
-
if (Object.is(first, second))
|
|
165
|
-
return true;
|
|
166
|
-
if (first === null || second === null ||
|
|
167
|
-
typeof first !== "object" || typeof second !== "object") {
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
if (first instanceof Date && second instanceof Date) {
|
|
171
|
-
return first.getTime() === second.getTime();
|
|
172
|
-
}
|
|
173
|
-
if (Array.isArray(first) || Array.isArray(second)) {
|
|
174
|
-
return Array.isArray(first) && Array.isArray(second) &&
|
|
175
|
-
first.length === second.length &&
|
|
176
|
-
first.every((value, index) => this.isEqual(value, second[index]));
|
|
177
|
-
}
|
|
178
|
-
const firstKeys = Reflect.ownKeys(first);
|
|
179
|
-
const secondKeys = Reflect.ownKeys(second);
|
|
180
|
-
return firstKeys.length === secondKeys.length &&
|
|
181
|
-
firstKeys.every(key => secondKeys.includes(key) &&
|
|
182
|
-
this.isEqual(first[key], second[key]));
|
|
183
|
-
}
|
|
184
|
-
static isShallowEqual(first, second) {
|
|
185
|
-
if (Object.is(first, second))
|
|
186
|
-
return true;
|
|
187
|
-
const firstKeys = Reflect.ownKeys(first);
|
|
188
|
-
const secondKeys = Reflect.ownKeys(second);
|
|
189
|
-
return firstKeys.length === secondKeys.length &&
|
|
190
|
-
firstKeys.every(key => secondKeys.includes(key) &&
|
|
191
|
-
Object.is(first[key], second[key]));
|
|
192
|
-
}
|
|
193
|
-
static hasAll(value, keys) {
|
|
194
|
-
return keys.every(key => Object.prototype.hasOwnProperty.call(value, key));
|
|
195
|
-
}
|
|
196
|
-
static hasAny(value, keys) {
|
|
197
|
-
return keys.some(key => Object.prototype.hasOwnProperty.call(value, key));
|
|
198
|
-
}
|
|
199
|
-
static assertKeys(value, keys) {
|
|
200
|
-
const missing = keys.filter(key => !Object.prototype.hasOwnProperty.call(value, key));
|
|
201
|
-
if (missing.length) {
|
|
202
|
-
throw new TypeError(`Missing required keys: ${missing.map(String).join(", ")}`);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
static with(value, key, next) {
|
|
206
|
-
return { ...value, [key]: next };
|
|
207
|
-
}
|
|
208
|
-
static without(value, key) {
|
|
209
|
-
const result = { ...value };
|
|
210
|
-
delete result[key];
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
static update(value, key, updater) {
|
|
214
|
-
return { ...value, [key]: updater(value[key]) };
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
globalThis.O2 = O2I;
|