@tolki/utils 1.3.0 → 1.5.0
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/dist/cast.d.ts +75 -3
- package/dist/cast.d.ts.map +1 -1
- package/dist/cast.js +117 -33
- package/dist/equality.d.ts +49 -2
- package/dist/equality.d.ts.map +1 -1
- package/dist/equality.js +148 -86
- package/dist/exceptions.d.ts +60 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/exceptions.js +54 -0
- package/dist/guards.d.ts +66 -2
- package/dist/guards.d.ts.map +1 -1
- package/dist/guards.js +110 -91
- package/dist/index.js +94 -66
- package/dist/keys.d.ts +93 -0
- package/dist/keys.d.ts.map +1 -1
- package/dist/keys.js +89 -22
- package/dist/range.d.ts +70 -0
- package/dist/range.d.ts.map +1 -1
- package/dist/range.js +103 -5
- package/dist/reflect.d.ts +16 -2
- package/dist/reflect.d.ts.map +1 -1
- package/dist/reflect.js +19 -9
- package/dist/sorting.d.ts +16 -2
- package/dist/sorting.d.ts.map +1 -1
- package/dist/sorting.js +27 -13
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +94 -66
- package/package.json +2 -2
package/dist/cast.d.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cast a value to the string key PHP's `array_combine` stores it under:
|
|
3
|
+
* `null`, `undefined` and `false` become `""`, `true` becomes `"1"`, a number prints the way PHP's
|
|
4
|
+
* `(string)` cast prints it (`INF`, `-0`, `1.0E+21`, 14 significant digits), anything else is stringified.
|
|
5
|
+
*
|
|
6
|
+
* A whole-number JS number always prints as an integer: JS has one number type, so it can't tell
|
|
7
|
+
* PHP's float `123456789012345.0` from the integer `123456789012345`.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The value to use as a key
|
|
10
|
+
* @returns The key string
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* toPhpKeyString(true); -> "1"
|
|
14
|
+
* toPhpKeyString(0.1 + 0.2); -> "0.3"
|
|
15
|
+
*/
|
|
16
|
+
export declare function toPhpKeyString(value: unknown): string;
|
|
1
17
|
/**
|
|
2
18
|
* Check if a value is arrayable (has a toArray method).
|
|
3
19
|
*
|
|
@@ -83,17 +99,33 @@ export declare function getAccessibleValues<T>(data: ReadonlyArray<T> | unknown)
|
|
|
83
99
|
/**
|
|
84
100
|
* Normalize a set-operation operand the way Laravel's
|
|
85
101
|
* `EnumeratesValues::getArrayableItems()` does: nullish becomes an empty array,
|
|
86
|
-
* an Enumerable/Arrayable-like
|
|
87
|
-
* iterable spreads, a plain object contributes its values
|
|
88
|
-
* becomes a one-element array.
|
|
102
|
+
* an Enumerable/Arrayable-like class instance unwraps via `all()`/`toArray()`/`toJSON()`,
|
|
103
|
+
* an iterable spreads, a plain object contributes its values whatever members it
|
|
104
|
+
* has, anything else becomes a one-element array.
|
|
89
105
|
*
|
|
90
106
|
* @param items - The operand to normalize
|
|
91
107
|
* @returns The operand's values, in iteration order
|
|
92
108
|
*
|
|
93
109
|
* @example
|
|
94
110
|
* arrayableValues({ x: 20 }); -> [20]
|
|
111
|
+
* arrayableValues(collect([20])); -> [20]
|
|
95
112
|
*/
|
|
96
113
|
export declare function arrayableValues<T>(items: unknown): T[];
|
|
114
|
+
/**
|
|
115
|
+
* Normalize a keyed operand the way Laravel's `getArrayableItems()` does:
|
|
116
|
+
* nullish becomes `{}`, an Enumerable/Arrayable-like class instance unwraps via `all()`/`toArray()`/`toJSON()`,
|
|
117
|
+
* a Map or other iterable becomes an object, a list becomes an index-keyed object, and a WeakMap or
|
|
118
|
+
* WeakSet, whose entries can't be read, becomes `{}`. A Map's keys are cast as PHP casts an array key.
|
|
119
|
+
* A plain object is returned as it is, whatever members it has.
|
|
120
|
+
*
|
|
121
|
+
* @param items - The operand to normalize
|
|
122
|
+
* @returns The operand's entries as a plain object
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* arrayableItems(collect({ a: 1 })); -> { a: 1 }
|
|
126
|
+
* arrayableItems(new Map([[1, "a"], ["1", "b"]])); -> { 1: "b" }
|
|
127
|
+
*/
|
|
128
|
+
export declare function arrayableItems(items: unknown): Record<string, unknown>;
|
|
97
129
|
/**
|
|
98
130
|
* Cast a CSS-list value the way PHP casts it when pushed raw into
|
|
99
131
|
* `implode()`/`Str::finish()`: `null` becomes `""`, a boolean becomes
|
|
@@ -103,4 +135,44 @@ export declare function arrayableValues<T>(items: unknown): T[];
|
|
|
103
135
|
* @returns The string PHP would have interpolated.
|
|
104
136
|
*/
|
|
105
137
|
export declare function cssListItemToString(value: unknown): string;
|
|
138
|
+
/**
|
|
139
|
+
* Read a number PHP passes to an int parameter, which drops a fraction as PHP's coercion does.
|
|
140
|
+
*
|
|
141
|
+
* @param value - The number passed where PHP declares an int
|
|
142
|
+
* @param message - The TypeError message PHP gives for a number no int can hold
|
|
143
|
+
* @returns The number without its fraction
|
|
144
|
+
* @throws TypeError when the number is NAN, infinite or outside PHP's 64-bit int range
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* phpIntArgument(7.5, "array_pad(): Argument #2 ($length) must be of type int, float given"); -> 7
|
|
148
|
+
* phpIntArgument(NaN, "array_pad(): Argument #2 ($length) must be of type int, float given"); -> throws TypeError
|
|
149
|
+
*/
|
|
150
|
+
export declare function phpIntArgument(value: number, message: string): number;
|
|
151
|
+
/**
|
|
152
|
+
* Cast a number to an int as PHP's `(int)` cast does, as it also reads a comparator's answer and the operands of `%`.
|
|
153
|
+
*
|
|
154
|
+
* @param value - The number to cast
|
|
155
|
+
* @returns The number without its fraction; past PHP's 64-bit int range its low 64 bits as PHP 8 keeps them, and 0 for
|
|
156
|
+
* NAN or an infinity
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* phpIntCast(2.7); -> 2
|
|
160
|
+
* phpIntCast(1e19); -> -8446744073709551616
|
|
161
|
+
* phpIntCast(NaN); -> 0
|
|
162
|
+
*/
|
|
163
|
+
export declare function phpIntCast(value: number): number;
|
|
164
|
+
/**
|
|
165
|
+
* Cast a value to a string as PHP's `(string)` cast does, as `implode()` casts each piece and `.` its operands.
|
|
166
|
+
*
|
|
167
|
+
* @param value - The value to cast
|
|
168
|
+
* @returns "Array" for an array or what stands for one, an object's own toString, else toPhpKeyString()'s cast of a
|
|
169
|
+
* scalar: true to "1", false and null to "", a float to 14 digits
|
|
170
|
+
* @throws Error `Object of class X could not be converted to string` for any other object and for a closure
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* phpStringCast([1, 2]); -> "Array"
|
|
174
|
+
* phpStringCast(true); -> "1"
|
|
175
|
+
* phpStringCast(() => 1); -> throws Error("Object of class Closure could not be converted to string")
|
|
176
|
+
*/
|
|
177
|
+
export declare function phpStringCast(value: unknown): string;
|
|
106
178
|
//# sourceMappingURL=cast.d.ts.map
|
package/dist/cast.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cast.d.ts","sourceRoot":"","sources":["../src/cast.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cast.d.ts","sourceRoot":"","sources":["../src/cast.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAYrD;AAqFD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,OAAO,IAAI,CAAC,EAAE,CAAA;CAAE,CAM1E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,CAAC,EACxB,KAAK,EAAE,OAAO,GACf,KAAK,IAAI;IAAE,MAAM,IAAI,CAAC,CAAA;CAAE,GAAG;IAAE,MAAM,IAAI,CAAC,CAAA;CAAE,CAS5C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAChC,KAAK,EAAE,OAAO,GACf,KAAK,IAAI;IAAE,aAAa,IAAI,CAAC,CAAA;CAAE,CAMjC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,EAAE,GAAG,IAAI,CAG7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAC9B,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,GACjC,CAAC,EAAE,GAAG,IAAI,CAKZ;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,EAAE,CAG5E;AAwBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,EAAE,CAyBtD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAkCtE;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAU1D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAOrE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAahD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAiBpD"}
|
package/dist/cast.js
CHANGED
|
@@ -1,46 +1,130 @@
|
|
|
1
|
-
import { isNull as
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { isNull as s, isUndefined as a, isArray as b, isObject as c, isMap as d, isWeakMap as l, isWeakSet as A, isIterable as y, isBoolean as I, isFiniteNumber as m, isPhpAccessible as $, isFunction as f, isNumber as O, isPlainObject as w } from "./guards.js";
|
|
2
|
+
import { keyedEntries as S, defineKey as T, hasOwnToString as j, isPhpArrayKey as B } from "./keys.js";
|
|
3
|
+
import { phpDebugType as E } from "./reflect.js";
|
|
4
|
+
const g = 14;
|
|
5
|
+
function F(r) {
|
|
6
|
+
return s(r) || a(r) || r === !1 ? "" : r === !0 ? "1" : O(r) || Number.isNaN(r) ? J(r) : String(r);
|
|
4
7
|
}
|
|
5
8
|
function J(r) {
|
|
6
|
-
if (
|
|
9
|
+
if (Number.isNaN(r))
|
|
10
|
+
return "NAN";
|
|
11
|
+
if (!Number.isFinite(r))
|
|
12
|
+
return r > 0 ? "INF" : "-INF";
|
|
13
|
+
if (B(r) && !Object.is(r, -0))
|
|
14
|
+
return BigInt(r).toString();
|
|
15
|
+
const [t, n] = P(Math.abs(r)), e = r < 0 || Object.is(r, -0) ? "-" : "";
|
|
16
|
+
if (n < -3 || n > g) {
|
|
17
|
+
const i = n - 1, p = `${t.charAt(0)}.${t.slice(1) || "0"}`;
|
|
18
|
+
return `${e}${p}E${i < 0 ? "-" : "+"}${Math.abs(i)}`;
|
|
19
|
+
}
|
|
20
|
+
if (n <= 0)
|
|
21
|
+
return `${e}0.${"0".repeat(-n)}${t}`;
|
|
22
|
+
const o = t.slice(0, n).padEnd(n, "0"), u = t.slice(n);
|
|
23
|
+
return u === "" ? `${e}${o}` : `${e}${o}.${u}`;
|
|
24
|
+
}
|
|
25
|
+
function P(r) {
|
|
26
|
+
if (r === 0)
|
|
27
|
+
return ["0", 1];
|
|
28
|
+
let t = r, n = 0;
|
|
29
|
+
for (; !Number.isInteger(t); )
|
|
30
|
+
t *= 2, n++;
|
|
31
|
+
const e = (BigInt(t) * 5n ** BigInt(n)).toString(), o = e.slice(g), u = "5".padEnd(o.length, "0");
|
|
32
|
+
let i = e.slice(0, g), p = e.length - n;
|
|
33
|
+
if (o > u || o === u && Number(i.at(-1)) % 2 === 1) {
|
|
34
|
+
const N = String(BigInt(i) + 1n);
|
|
35
|
+
p += N.length - i.length, i = N;
|
|
36
|
+
}
|
|
37
|
+
for (; i.endsWith("0"); )
|
|
38
|
+
i = i.slice(0, -1);
|
|
39
|
+
return [i, p];
|
|
40
|
+
}
|
|
41
|
+
function C(r) {
|
|
42
|
+
return c(r) && !s(r) && f(r.toArray);
|
|
43
|
+
}
|
|
44
|
+
function K(r) {
|
|
45
|
+
if (!c(r) || s(r))
|
|
7
46
|
return !1;
|
|
8
|
-
const
|
|
9
|
-
return
|
|
47
|
+
const t = f(r.toJson), n = f(r.toJSON);
|
|
48
|
+
return t || n;
|
|
10
49
|
}
|
|
11
|
-
function
|
|
12
|
-
return
|
|
50
|
+
function W(r) {
|
|
51
|
+
return c(r) && !s(r) && f(r.jsonSerialize);
|
|
13
52
|
}
|
|
14
|
-
function
|
|
15
|
-
return
|
|
53
|
+
function D(r) {
|
|
54
|
+
return b(r) ? r : null;
|
|
16
55
|
}
|
|
17
|
-
function
|
|
56
|
+
function M(r) {
|
|
18
57
|
return Array.isArray(r) ? r.slice() : null;
|
|
19
58
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
22
|
-
}
|
|
23
|
-
function
|
|
24
|
-
if (
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
59
|
+
function L(r) {
|
|
60
|
+
return M(r) || [];
|
|
61
|
+
}
|
|
62
|
+
function h(r) {
|
|
63
|
+
if (w(r) || !c(r))
|
|
64
|
+
return r;
|
|
65
|
+
for (const t of ["all", "toArray", "toJSON"])
|
|
66
|
+
if (f(r[t]))
|
|
67
|
+
return h(r[t]());
|
|
68
|
+
return r;
|
|
69
|
+
}
|
|
70
|
+
function V(r) {
|
|
71
|
+
const t = h(r);
|
|
72
|
+
return s(t) || a(t) ? [] : b(t) ? t.slice() : c(t) ? d(t) ? S(t).map(([, n]) => n) : y(t) ? [...t] : Object.values(t) : [t];
|
|
73
|
+
}
|
|
74
|
+
function _(r) {
|
|
75
|
+
const t = h(r);
|
|
76
|
+
if (s(t) || a(t))
|
|
77
|
+
return {};
|
|
78
|
+
if (b(t))
|
|
79
|
+
return { ...t };
|
|
80
|
+
if (c(t)) {
|
|
81
|
+
if (d(t)) {
|
|
82
|
+
const n = {};
|
|
83
|
+
for (const [e, o] of S(t))
|
|
84
|
+
T(n, e, o);
|
|
85
|
+
return n;
|
|
86
|
+
}
|
|
87
|
+
return l(t) || A(t) ? {} : y(t) ? { ...[...t] } : t;
|
|
31
88
|
}
|
|
32
|
-
return
|
|
89
|
+
return { 0: t };
|
|
90
|
+
}
|
|
91
|
+
function H(r) {
|
|
92
|
+
return s(r) || a(r) ? "" : I(r) ? r ? "1" : "" : String(r);
|
|
93
|
+
}
|
|
94
|
+
function R(r, t) {
|
|
95
|
+
if (!m(r) || r < -9223372036854776e3 || r >= 2 ** 63)
|
|
96
|
+
throw new TypeError(t);
|
|
97
|
+
return Math.trunc(r);
|
|
98
|
+
}
|
|
99
|
+
function U(r) {
|
|
100
|
+
if (!m(r))
|
|
101
|
+
return 0;
|
|
102
|
+
const t = Math.trunc(r);
|
|
103
|
+
return t >= -9223372036854776e3 && t < 2 ** 63 ? t === 0 ? 0 : t : Number(BigInt.asIntN(64, BigInt(t)));
|
|
33
104
|
}
|
|
34
|
-
function
|
|
35
|
-
|
|
105
|
+
function q(r) {
|
|
106
|
+
if ($(r))
|
|
107
|
+
return "Array";
|
|
108
|
+
if (j(r))
|
|
109
|
+
return String(r.toString());
|
|
110
|
+
if (c(r) || f(r))
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Object of class ${E(r)} could not be converted to string`
|
|
113
|
+
);
|
|
114
|
+
return F(r);
|
|
36
115
|
}
|
|
37
116
|
export {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
117
|
+
_ as arrayableItems,
|
|
118
|
+
V as arrayableValues,
|
|
119
|
+
D as castableToArray,
|
|
120
|
+
H as cssListItemToString,
|
|
121
|
+
L as getAccessibleValues,
|
|
122
|
+
M as normalizeToArray,
|
|
123
|
+
R as phpIntArgument,
|
|
124
|
+
U as phpIntCast,
|
|
125
|
+
q as phpStringCast,
|
|
126
|
+
C as toArrayable,
|
|
127
|
+
W as toJsonSerializable,
|
|
128
|
+
K as toJsonable,
|
|
129
|
+
F as toPhpKeyString
|
|
46
130
|
};
|
package/dist/equality.d.ts
CHANGED
|
@@ -2,17 +2,32 @@
|
|
|
2
2
|
* Order two values the way PHP 8's `<=>` does.
|
|
3
3
|
*
|
|
4
4
|
* Numeric operands compare numerically (`"9"` sorts below `"10"`); null/boolean
|
|
5
|
-
* compares both sides as booleans; arrays
|
|
5
|
+
* compares both sides as booleans; two arrays or objects take PHP's array rule —
|
|
6
|
+
* fewer entries first, then element-wise over the left operand's keys. Two `Date`s
|
|
7
|
+
* compare by time value, as PHP compares two `DateTime` objects.
|
|
6
8
|
*
|
|
7
9
|
* Faithful to PHP, this order is **not transitive** — `null` ties `0` and `""`,
|
|
8
10
|
* yet `0 > ""`.
|
|
9
11
|
*
|
|
12
|
+
* Three recorded divergences from PHP:
|
|
13
|
+
* - a cyclic pair ties, where PHP raises `Error: Nesting level too deep`;
|
|
14
|
+
* - an array against a scalar keeps JS coercion, where PHP sorts every array above
|
|
15
|
+
* every scalar;
|
|
16
|
+
* - a `Date` against an array or a plain object keeps the entry-count rule, where
|
|
17
|
+
* PHP sorts every object above every array: `new DateTime(...) <=> []` is 1 and
|
|
18
|
+
* `[] <=> new DateTime(...)` is -1. (Only two OBJECTS answer 1 from either side.)
|
|
19
|
+
*
|
|
20
|
+
* JS-only: a `Map`, a `Set` and a `RegExp` have no PHP analogue, so there is no
|
|
21
|
+
* rule to port — each carries no own enumerable keys, and any two of them tie.
|
|
22
|
+
* That is a decision, not a divergence; do not "fix" it by analogy with `Date`.
|
|
23
|
+
*
|
|
10
24
|
* @param a - First value to compare
|
|
11
25
|
* @param b - Second value to compare
|
|
12
26
|
* @returns -1 if a < b, 1 if a > b, 0 if equal
|
|
13
27
|
*
|
|
14
28
|
* @example
|
|
15
29
|
* compareValues(0, ""); -> 1
|
|
30
|
+
* compareValues([1], [1, 2]); -> -1
|
|
16
31
|
*/
|
|
17
32
|
export declare function compareValues(a: unknown, b: unknown): number;
|
|
18
33
|
/**
|
|
@@ -41,7 +56,8 @@ export declare function compareValues(a: unknown, b: unknown): number;
|
|
|
41
56
|
export declare function looseEqual(a: unknown, b: unknown): boolean;
|
|
42
57
|
/**
|
|
43
58
|
* PHP-like strict equality comparison (=== operator).
|
|
44
|
-
* Performs strict type checking for primitives but value-based comparison for arrays and objects
|
|
59
|
+
* Performs strict type checking for primitives but value-based comparison for arrays and objects:
|
|
60
|
+
* like PHP's `===` on arrays, two plain objects need the same key/value pairs in the same order.
|
|
45
61
|
* NOTE: For class instances (objects with constructors other than Object), uses reference equality.
|
|
46
62
|
*
|
|
47
63
|
* @param a - First value to compare
|
|
@@ -54,6 +70,37 @@ export declare function looseEqual(a: unknown, b: unknown): boolean;
|
|
|
54
70
|
* strictEqual(1, '1'); -> false (different types)
|
|
55
71
|
* strictEqual(['a'], ['a']); -> true (same array content)
|
|
56
72
|
* strictEqual({a: 1}, {a: 1}); -> true (same object content)
|
|
73
|
+
* strictEqual({a: 1, b: 2}, {b: 2, a: 1}); -> false (same pairs, another order)
|
|
57
74
|
*/
|
|
58
75
|
export declare function strictEqual(a: unknown, b: unknown): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Compare two values with one of PHP's `where()` operators, the way Laravel's
|
|
78
|
+
* `EnumeratesValues::operatorForWhere()` does.
|
|
79
|
+
*
|
|
80
|
+
* An unrecognised operator falls through to `=`, as PHP's `switch` default does.
|
|
81
|
+
* When exactly one side is an object and the pair holds fewer than two strings,
|
|
82
|
+
* only the inequality operators answer true. That short-circuit is Laravel's own, ahead
|
|
83
|
+
* of the switch; raw PHP does order such a pair, emitting `Notice: Object of class P
|
|
84
|
+
* could not be converted to int` and comparing as if the object were 1. A plain
|
|
85
|
+
* object is not one of those objects, because it models a PHP array here. `===` and
|
|
86
|
+
* `!==` take `strictEqual`, PHP's by-value rule for an array. Every other
|
|
87
|
+
* relational operator orders through `compareValues`, PHP's own comparison rule, so
|
|
88
|
+
* `null` is ordered rather than refused. `NaN` orders with no number and no string —
|
|
89
|
+
* `<`, `>`, `<=` and `>=` are all false there and `<=>` still answers 1, as PHP's
|
|
90
|
+
* `NAN <=> 1` does — but against a bool or null PHP casts both sides to bool and
|
|
91
|
+
* orders normally, so `NAN <=> true` is 0 and `NAN <=> null` is 1.
|
|
92
|
+
*
|
|
93
|
+
* @param retrieved - The value read from the item
|
|
94
|
+
* @param operator - The comparison operator (`=`, `==`, `!=`, `<>`, `<`, `>`, `<=`, `>=`, `===`, `!==`, `<=>`)
|
|
95
|
+
* @param value - The value to compare against
|
|
96
|
+
* @returns True if the comparison holds
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
*
|
|
100
|
+
* operatorMatch(3, '>', 2); -> true
|
|
101
|
+
* operatorMatch('4', '===', 4); -> false
|
|
102
|
+
* operatorMatch(1, '>', null); -> true (PHP casts null to false and 1 to true)
|
|
103
|
+
* operatorMatch(1, 'nonsense', '1'); -> true (unknown operators compare loosely)
|
|
104
|
+
*/
|
|
105
|
+
export declare function operatorMatch(retrieved: unknown, operator: string, value: unknown): boolean;
|
|
59
106
|
//# sourceMappingURL=equality.d.ts.map
|
package/dist/equality.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"equality.d.ts","sourceRoot":"","sources":["../src/equality.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"equality.d.ts","sourceRoot":"","sources":["../src/equality.ts"],"names":[],"mappings":"AAmIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAE5D;AAuID;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAgH1D;AA8FD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAqE3D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,aAAa,CACzB,SAAS,EAAE,OAAO,EAClB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,OAAO,GACf,OAAO,CA2DT"}
|
package/dist/equality.js
CHANGED
|
@@ -1,109 +1,136 @@
|
|
|
1
|
-
import {
|
|
2
|
-
function
|
|
3
|
-
return
|
|
1
|
+
import { isNumber as T, isString as u, isPhpNumeric as y, isBoolean as h, isObject as m, isPlainObject as V, isArray as B, isPhpFalsy as H, isNull as R, isUndefined as U } from "./guards.js";
|
|
2
|
+
function g(t) {
|
|
3
|
+
return R(t) || U(t);
|
|
4
4
|
}
|
|
5
|
-
function
|
|
5
|
+
function N(t, r) {
|
|
6
6
|
return t < r ? -1 : t > r ? 1 : 0;
|
|
7
7
|
}
|
|
8
|
-
const
|
|
9
|
-
function
|
|
10
|
-
return t >=
|
|
8
|
+
const j = /^[ \t\n\r\v\f]*[+-]?\d+[ \t\n\r\v\f]*$/, A = 2 ** 63, k = 2n ** 63n;
|
|
9
|
+
function S(t) {
|
|
10
|
+
return t >= k ? 1 : t < -k ? -1 : 0;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
if (
|
|
14
|
-
const
|
|
15
|
-
return
|
|
12
|
+
function D(t, r) {
|
|
13
|
+
if (j.test(t) && j.test(r)) {
|
|
14
|
+
const i = BigInt(t), c = BigInt(r), s = S(i);
|
|
15
|
+
return s !== 0 && s === S(c) && Number(t) === Number(r) ? N(t, r) : i < c ? -1 : i > c ? 1 : 0;
|
|
16
16
|
}
|
|
17
|
-
const
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
22
|
-
}
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return
|
|
17
|
+
const n = Number(t), e = Number(r);
|
|
18
|
+
return n === e && !Number.isFinite(n) ? N(t, r) : n < e ? -1 : n > e ? 1 : 0;
|
|
19
|
+
}
|
|
20
|
+
function p(t) {
|
|
21
|
+
return !H(t);
|
|
22
|
+
}
|
|
23
|
+
function _(t, r) {
|
|
24
|
+
return G(t, r, void 0);
|
|
25
|
+
}
|
|
26
|
+
function G(t, r, n) {
|
|
27
|
+
if (g(t) && u(r))
|
|
28
|
+
return N("", r);
|
|
29
|
+
if (u(t) && g(r))
|
|
30
|
+
return N(t, "");
|
|
31
|
+
if (g(t) || g(r) || h(t) || h(r)) {
|
|
32
|
+
const e = p(t), i = p(r);
|
|
33
|
+
return e === i ? 0 : e ? 1 : -1;
|
|
34
|
+
}
|
|
35
|
+
if (t instanceof Date && r instanceof Date) {
|
|
36
|
+
const e = t.getTime(), i = r.getTime();
|
|
37
|
+
return e < i ? -1 : e > i ? 1 : 0;
|
|
31
38
|
}
|
|
32
39
|
if (typeof t == "object" && typeof r == "object")
|
|
33
|
-
return
|
|
34
|
-
if (
|
|
35
|
-
if (
|
|
36
|
-
return
|
|
37
|
-
const e = Number(t),
|
|
38
|
-
return e <
|
|
40
|
+
return q(t, r, n ?? /* @__PURE__ */ new Map());
|
|
41
|
+
if (y(t) && y(r)) {
|
|
42
|
+
if (u(t) && u(r))
|
|
43
|
+
return D(t, r);
|
|
44
|
+
const e = Number(t), i = Number(r);
|
|
45
|
+
return e < i ? -1 : e > i ? 1 : 0;
|
|
46
|
+
}
|
|
47
|
+
return u(t) || u(r) ? N(String(t), String(r)) : t < r ? -1 : t > r ? 1 : 0;
|
|
48
|
+
}
|
|
49
|
+
function q(t, r, n) {
|
|
50
|
+
const e = n.get(t);
|
|
51
|
+
if (e?.has(r))
|
|
52
|
+
return 0;
|
|
53
|
+
const i = e ?? /* @__PURE__ */ new Set();
|
|
54
|
+
e || n.set(t, i), i.add(r);
|
|
55
|
+
const c = C(t, r, n);
|
|
56
|
+
return i.delete(r), c;
|
|
57
|
+
}
|
|
58
|
+
function C(t, r, n) {
|
|
59
|
+
const e = Object.keys(t), i = Object.keys(r).length;
|
|
60
|
+
if (e.length !== i)
|
|
61
|
+
return e.length < i ? -1 : 1;
|
|
62
|
+
const c = t, s = r;
|
|
63
|
+
for (const f of e) {
|
|
64
|
+
if (!Object.hasOwn(r, f))
|
|
65
|
+
return 1;
|
|
66
|
+
const l = G(c[f], s[f], n);
|
|
67
|
+
if (l !== 0)
|
|
68
|
+
return l;
|
|
39
69
|
}
|
|
40
|
-
return
|
|
70
|
+
return 0;
|
|
41
71
|
}
|
|
42
|
-
function
|
|
72
|
+
function I(t, r) {
|
|
43
73
|
if (t === r)
|
|
44
74
|
return !0;
|
|
45
|
-
|
|
46
|
-
|
|
75
|
+
if (T(t) && T(r))
|
|
76
|
+
return !1;
|
|
77
|
+
if (u(t) && u(r))
|
|
78
|
+
return y(t) && y(r) && D(t, r) === 0;
|
|
79
|
+
const n = g(t), e = g(r);
|
|
80
|
+
if (n && e)
|
|
47
81
|
return !0;
|
|
48
|
-
if (
|
|
49
|
-
const
|
|
50
|
-
return
|
|
82
|
+
if (n || e) {
|
|
83
|
+
const s = n ? r : t;
|
|
84
|
+
return u(s) ? s === "" : !p(s);
|
|
51
85
|
}
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
54
|
-
if (
|
|
86
|
+
if (h(t) || h(r))
|
|
87
|
+
return p(t) === p(r);
|
|
88
|
+
if (P(t) || P(r))
|
|
55
89
|
return !1;
|
|
56
|
-
if (
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
59
|
-
return String(
|
|
90
|
+
if (u(t) !== u(r)) {
|
|
91
|
+
const s = u(t) ? r : t;
|
|
92
|
+
if (m(s) && !V(s) && x(s))
|
|
93
|
+
return String(s) === (u(t) ? t : r);
|
|
60
94
|
}
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
return
|
|
64
|
-
if (
|
|
95
|
+
const i = E(t) || u(t), c = E(r) || u(r);
|
|
96
|
+
if (i && c)
|
|
97
|
+
return d(t) && d(r) ? w(t) && w(r) ? BigInt(t) === BigInt(r) : Number(t) === Number(r) : F(t) === F(r);
|
|
98
|
+
if (B(t) && B(r)) {
|
|
65
99
|
if (t.length !== r.length)
|
|
66
100
|
return !1;
|
|
67
|
-
for (let
|
|
68
|
-
if (!
|
|
101
|
+
for (let s = 0; s < t.length; s++)
|
|
102
|
+
if (!I(t[s], r[s]))
|
|
69
103
|
return !1;
|
|
70
104
|
return !0;
|
|
71
105
|
}
|
|
72
|
-
if (
|
|
73
|
-
const
|
|
74
|
-
return
|
|
75
|
-
(
|
|
76
|
-
t[
|
|
77
|
-
r[
|
|
106
|
+
if (m(t) && m(r)) {
|
|
107
|
+
const s = Object.keys(t), f = Object.keys(r);
|
|
108
|
+
return s.length !== f.length ? !1 : s.every(
|
|
109
|
+
(l) => f.includes(l) && I(
|
|
110
|
+
t[l],
|
|
111
|
+
r[l]
|
|
78
112
|
)
|
|
79
113
|
);
|
|
80
114
|
}
|
|
81
115
|
return !1;
|
|
82
116
|
}
|
|
83
|
-
function
|
|
84
|
-
const r = Object.getPrototypeOf(t);
|
|
85
|
-
return r === Object.prototype || r === null;
|
|
86
|
-
}
|
|
87
|
-
function l(t) {
|
|
88
|
-
return typeof t == "bigint" ? t !== 0n : N(t) && !G(t) ? !0 : !R(t);
|
|
89
|
-
}
|
|
90
|
-
function A(t) {
|
|
117
|
+
function E(t) {
|
|
91
118
|
return typeof t == "number" || typeof t == "bigint";
|
|
92
119
|
}
|
|
93
|
-
function
|
|
94
|
-
return typeof t == "bigint" ||
|
|
120
|
+
function d(t) {
|
|
121
|
+
return typeof t == "bigint" || y(t);
|
|
95
122
|
}
|
|
96
|
-
function
|
|
97
|
-
return typeof t == "bigint" ? !0 : typeof t == "number" ? Number.isInteger(t) && t >= -
|
|
123
|
+
function w(t) {
|
|
124
|
+
return typeof t == "bigint" ? !0 : typeof t == "number" ? Number.isInteger(t) && t >= -A && t < A : j.test(t) && S(BigInt(t)) === 0;
|
|
98
125
|
}
|
|
99
|
-
function
|
|
126
|
+
function P(t) {
|
|
100
127
|
return typeof t == "number" && Number.isNaN(t);
|
|
101
128
|
}
|
|
102
|
-
function
|
|
129
|
+
function x(t) {
|
|
103
130
|
const r = t.toString;
|
|
104
131
|
return typeof r == "function" && r !== Object.prototype.toString;
|
|
105
132
|
}
|
|
106
|
-
function
|
|
133
|
+
function F(t) {
|
|
107
134
|
if (typeof t == "number") {
|
|
108
135
|
if (t === Number.POSITIVE_INFINITY)
|
|
109
136
|
return "INF";
|
|
@@ -112,36 +139,71 @@ function k(t) {
|
|
|
112
139
|
}
|
|
113
140
|
return String(t);
|
|
114
141
|
}
|
|
115
|
-
function
|
|
142
|
+
function O(t, r) {
|
|
116
143
|
if (t === r)
|
|
117
144
|
return !0;
|
|
118
145
|
if (Array.isArray(t) && Array.isArray(r)) {
|
|
119
146
|
if (t.length !== r.length)
|
|
120
147
|
return !1;
|
|
121
|
-
for (let
|
|
122
|
-
if (!
|
|
148
|
+
for (let n = 0; n < t.length; n++)
|
|
149
|
+
if (!O(t[n], r[n]))
|
|
123
150
|
return !1;
|
|
124
151
|
return !0;
|
|
125
152
|
}
|
|
126
153
|
if (t !== null && r !== null && typeof t == "object" && typeof r == "object" && !Array.isArray(t) && !Array.isArray(r)) {
|
|
127
|
-
const
|
|
128
|
-
if (!
|
|
154
|
+
const n = t.constructor === Object || t.constructor === void 0, e = r.constructor === Object || r.constructor === void 0;
|
|
155
|
+
if (!n || !e)
|
|
129
156
|
return !1;
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
157
|
+
const i = Object.keys(t), c = Object.keys(r);
|
|
158
|
+
if (i.length !== c.length)
|
|
132
159
|
return !1;
|
|
133
|
-
for (const
|
|
134
|
-
if (
|
|
135
|
-
t[
|
|
136
|
-
r[
|
|
160
|
+
for (const [s, f] of i.entries())
|
|
161
|
+
if (f !== c[s] || !O(
|
|
162
|
+
t[f],
|
|
163
|
+
r[f]
|
|
137
164
|
))
|
|
138
165
|
return !1;
|
|
139
166
|
return !0;
|
|
140
167
|
}
|
|
141
168
|
return !1;
|
|
142
169
|
}
|
|
170
|
+
function Y(t, r, n) {
|
|
171
|
+
const e = [t, n], i = (o) => m(o) && !V(o);
|
|
172
|
+
if (e.filter(
|
|
173
|
+
(o) => u(o) || i(o) && x(o)
|
|
174
|
+
).length < 2 && e.filter(i).length === 1)
|
|
175
|
+
return ["!=", "<>", "!=="].includes(r);
|
|
176
|
+
const f = !(h(t) || h(n) || g(t) || g(n)) && (P(t) || P(n)), l = (o) => !f && o(_(t, n));
|
|
177
|
+
switch (r) {
|
|
178
|
+
case "!=":
|
|
179
|
+
case "<>":
|
|
180
|
+
return !I(t, n);
|
|
181
|
+
case "<":
|
|
182
|
+
return l((o) => o < 0);
|
|
183
|
+
case ">":
|
|
184
|
+
return l((o) => o > 0);
|
|
185
|
+
case "<=":
|
|
186
|
+
return l((o) => o <= 0);
|
|
187
|
+
case ">=":
|
|
188
|
+
return l((o) => o >= 0);
|
|
189
|
+
// PHP's `===` compares an array by value — same keys, same order, same types —
|
|
190
|
+
// and only a real object by identity. strictEqual is that rule; JS's own `===`
|
|
191
|
+
// would call every pair of equal arrays unequal (task-24, "r4-strict-operators").
|
|
192
|
+
case "===":
|
|
193
|
+
return O(t, n);
|
|
194
|
+
case "!==":
|
|
195
|
+
return !O(t, n);
|
|
196
|
+
// PHP's `<=>` is truthy for any non-zero result, so only an equal pair is
|
|
197
|
+
// falsy; an uncomparable one answers 1, not 0, and so counts as unequal.
|
|
198
|
+
case "<=>":
|
|
199
|
+
return f || _(t, n) !== 0;
|
|
200
|
+
default:
|
|
201
|
+
return I(t, n);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
143
204
|
export {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
205
|
+
_ as compareValues,
|
|
206
|
+
I as looseEqual,
|
|
207
|
+
Y as operatorMatch,
|
|
208
|
+
O as strictEqual
|
|
147
209
|
};
|