@tolki/utils 1.2.0 → 1.4.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 +53 -0
- package/dist/cast.d.ts.map +1 -1
- package/dist/cast.js +94 -20
- package/dist/equality.d.ts +67 -9
- package/dist/equality.d.ts.map +1 -1
- package/dist/equality.js +181 -48
- package/dist/exceptions.d.ts +32 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/exceptions.js +24 -0
- package/dist/guards.d.ts +113 -0
- package/dist/guards.d.ts.map +1 -1
- package/dist/guards.js +144 -84
- package/dist/index.js +79 -53
- package/dist/keys.d.ts +70 -1
- package/dist/keys.d.ts.map +1 -1
- package/dist/keys.js +73 -17
- package/dist/range.d.ts +23 -0
- package/dist/range.d.ts.map +1 -0
- package/dist/range.js +8 -0
- package/dist/reflect.d.ts +7 -0
- package/dist/reflect.d.ts.map +1 -1
- package/dist/reflect.js +10 -6
- package/dist/sorting.d.ts +20 -0
- package/dist/sorting.d.ts.map +1 -0
- package/dist/sorting.js +18 -0
- package/dist/string.d.ts +10 -0
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +15 -10
- package/dist/utils.d.ts +3 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +79 -53
- 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
|
*
|
|
@@ -80,4 +96,41 @@ export declare function normalizeToArray<T>(data: ReadonlyArray<T> | unknown): T
|
|
|
80
96
|
* getAccessibleValues('hello'); -> []
|
|
81
97
|
*/
|
|
82
98
|
export declare function getAccessibleValues<T>(data: ReadonlyArray<T> | unknown): T[];
|
|
99
|
+
/**
|
|
100
|
+
* Normalize a set-operation operand the way Laravel's
|
|
101
|
+
* `EnumeratesValues::getArrayableItems()` does: nullish becomes an empty array,
|
|
102
|
+
* an Enumerable/Arrayable-like object unwraps via `all()`/`toArray()`, an
|
|
103
|
+
* iterable spreads, a plain object contributes its values, anything else
|
|
104
|
+
* becomes a one-element array.
|
|
105
|
+
*
|
|
106
|
+
* @param items - The operand to normalize
|
|
107
|
+
* @returns The operand's values, in iteration order
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* arrayableValues({ x: 20 }); -> [20]
|
|
111
|
+
*/
|
|
112
|
+
export declare function arrayableValues<T>(items: unknown): T[];
|
|
113
|
+
/**
|
|
114
|
+
* Normalize a keyed operand the way Laravel's `getArrayableItems()` does:
|
|
115
|
+
* nullish becomes `{}`, an Enumerable/Arrayable-like object unwraps via `all()`/`toArray()`/`toJSON()`,
|
|
116
|
+
* a Map or other iterable becomes an object, a list becomes an index-keyed object, and a WeakMap or
|
|
117
|
+
* WeakSet, whose entries can't be read, becomes `{}`. A Map's keys are cast as PHP casts an array key.
|
|
118
|
+
*
|
|
119
|
+
* @param items - The operand to normalize
|
|
120
|
+
* @returns The operand's entries as a plain object
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* arrayableItems({ all: () => ({ a: 1 }) }); -> { a: 1 }
|
|
124
|
+
* arrayableItems(new Map([[1, "a"], ["1", "b"]])); -> { 1: "b" }
|
|
125
|
+
*/
|
|
126
|
+
export declare function arrayableItems(items: unknown): Record<string, unknown>;
|
|
127
|
+
/**
|
|
128
|
+
* Cast a CSS-list value the way PHP casts it when pushed raw into
|
|
129
|
+
* `implode()`/`Str::finish()`: `null` becomes `""`, a boolean becomes
|
|
130
|
+
* `"1"`/`""`, and everything else goes through `String()`.
|
|
131
|
+
*
|
|
132
|
+
* @param value - The CSS class or style fragment to cast.
|
|
133
|
+
* @returns The string PHP would have interpolated.
|
|
134
|
+
*/
|
|
135
|
+
export declare function cssListItemToString(value: unknown): string;
|
|
83
136
|
//# 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":"AAkBA;;;;;;;;;;;;;;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;AAuBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,EAAE,CAyBtD;AAED;;;;;;;;;;;;GAYG;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"}
|
package/dist/cast.js
CHANGED
|
@@ -1,30 +1,104 @@
|
|
|
1
|
-
import { isArray as
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { isNull as s, isUndefined as l, isArray as b, isObject as c, isMap as h, isWeakMap as m, isWeakSet as S, isIterable as d, isBoolean as A, isFunction as u, isNumber as $ } from "./guards.js";
|
|
2
|
+
import { keyedEntries as y, defineKey as I, isPhpArrayKey as O } from "./keys.js";
|
|
3
|
+
const p = 14;
|
|
4
|
+
function z(r) {
|
|
5
|
+
return s(r) || l(r) || r === !1 ? "" : r === !0 ? "1" : $(r) || Number.isNaN(r) ? J(r) : String(r);
|
|
4
6
|
}
|
|
5
|
-
function
|
|
6
|
-
if (
|
|
7
|
+
function J(r) {
|
|
8
|
+
if (Number.isNaN(r))
|
|
9
|
+
return "NAN";
|
|
10
|
+
if (!Number.isFinite(r))
|
|
11
|
+
return r > 0 ? "INF" : "-INF";
|
|
12
|
+
if (O(r) && !Object.is(r, -0))
|
|
13
|
+
return BigInt(r).toString();
|
|
14
|
+
const [n, t] = T(Math.abs(r)), e = r < 0 || Object.is(r, -0) ? "-" : "";
|
|
15
|
+
if (t < -3 || t > p) {
|
|
16
|
+
const i = t - 1, a = `${n.charAt(0)}.${n.slice(1) || "0"}`;
|
|
17
|
+
return `${e}${a}E${i < 0 ? "-" : "+"}${Math.abs(i)}`;
|
|
18
|
+
}
|
|
19
|
+
if (t <= 0)
|
|
20
|
+
return `${e}0.${"0".repeat(-t)}${n}`;
|
|
21
|
+
const o = n.slice(0, t).padEnd(t, "0"), f = n.slice(t);
|
|
22
|
+
return f === "" ? `${e}${o}` : `${e}${o}.${f}`;
|
|
23
|
+
}
|
|
24
|
+
function T(r) {
|
|
25
|
+
if (r === 0)
|
|
26
|
+
return ["0", 1];
|
|
27
|
+
let n = r, t = 0;
|
|
28
|
+
for (; !Number.isInteger(n); )
|
|
29
|
+
n *= 2, t++;
|
|
30
|
+
const e = (BigInt(n) * 5n ** BigInt(t)).toString(), o = e.slice(p), f = "5".padEnd(o.length, "0");
|
|
31
|
+
let i = e.slice(0, p), a = e.length - t;
|
|
32
|
+
if (o > f || o === f && Number(i.at(-1)) % 2 === 1) {
|
|
33
|
+
const N = String(BigInt(i) + 1n);
|
|
34
|
+
a += N.length - i.length, i = N;
|
|
35
|
+
}
|
|
36
|
+
for (; i.endsWith("0"); )
|
|
37
|
+
i = i.slice(0, -1);
|
|
38
|
+
return [i, a];
|
|
39
|
+
}
|
|
40
|
+
function B(r) {
|
|
41
|
+
return c(r) && !s(r) && u(r.toArray);
|
|
42
|
+
}
|
|
43
|
+
function E(r) {
|
|
44
|
+
if (!c(r) || s(r))
|
|
7
45
|
return !1;
|
|
8
|
-
const
|
|
9
|
-
return
|
|
46
|
+
const n = u(r.toJson), t = u(r.toJSON);
|
|
47
|
+
return n || t;
|
|
10
48
|
}
|
|
11
|
-
function
|
|
12
|
-
return
|
|
49
|
+
function P(r) {
|
|
50
|
+
return c(r) && !s(r) && u(r.jsonSerialize);
|
|
13
51
|
}
|
|
14
|
-
function
|
|
15
|
-
return
|
|
52
|
+
function k(r) {
|
|
53
|
+
return b(r) ? r : null;
|
|
16
54
|
}
|
|
17
|
-
function
|
|
55
|
+
function w(r) {
|
|
18
56
|
return Array.isArray(r) ? r.slice() : null;
|
|
19
57
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
58
|
+
function M(r) {
|
|
59
|
+
return w(r) || [];
|
|
60
|
+
}
|
|
61
|
+
function g(r) {
|
|
62
|
+
if (!c(r))
|
|
63
|
+
return r;
|
|
64
|
+
for (const n of ["all", "toArray", "toJSON"])
|
|
65
|
+
if (u(r[n]))
|
|
66
|
+
return g(r[n]());
|
|
67
|
+
return r;
|
|
68
|
+
}
|
|
69
|
+
function x(r) {
|
|
70
|
+
const n = g(r);
|
|
71
|
+
return s(n) || l(n) ? [] : b(n) ? n.slice() : c(n) ? h(n) ? y(n).map(([, t]) => t) : d(n) ? [...n] : Object.values(n) : [n];
|
|
72
|
+
}
|
|
73
|
+
function K(r) {
|
|
74
|
+
const n = g(r);
|
|
75
|
+
if (s(n) || l(n))
|
|
76
|
+
return {};
|
|
77
|
+
if (b(n))
|
|
78
|
+
return { ...n };
|
|
79
|
+
if (c(n)) {
|
|
80
|
+
if (h(n)) {
|
|
81
|
+
const t = {};
|
|
82
|
+
for (const [e, o] of y(n))
|
|
83
|
+
I(t, e, o);
|
|
84
|
+
return t;
|
|
85
|
+
}
|
|
86
|
+
return m(n) || S(n) ? {} : d(n) ? { ...[...n] } : n;
|
|
87
|
+
}
|
|
88
|
+
return { 0: n };
|
|
89
|
+
}
|
|
90
|
+
function W(r) {
|
|
91
|
+
return s(r) || l(r) ? "" : A(r) ? r ? "1" : "" : String(r);
|
|
22
92
|
}
|
|
23
93
|
export {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
94
|
+
K as arrayableItems,
|
|
95
|
+
x as arrayableValues,
|
|
96
|
+
k as castableToArray,
|
|
97
|
+
W as cssListItemToString,
|
|
98
|
+
M as getAccessibleValues,
|
|
99
|
+
w as normalizeToArray,
|
|
100
|
+
B as toArrayable,
|
|
101
|
+
P as toJsonSerializable,
|
|
102
|
+
E as toJsonable,
|
|
103
|
+
z as toPhpKeyString
|
|
30
104
|
};
|
package/dist/equality.d.ts
CHANGED
|
@@ -1,21 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Order two values the way PHP 8's `<=>` does.
|
|
3
|
+
*
|
|
4
|
+
* Numeric operands compare numerically (`"9"` sorts below `"10"`); null/boolean
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* Faithful to PHP, this order is **not transitive** — `null` ties `0` and `""`,
|
|
10
|
+
* yet `0 > ""`.
|
|
11
|
+
*
|
|
12
|
+
* Four 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
|
+
* - an EMPTY plain object against null or a boolean reads as truthy, where PHP casts
|
|
20
|
+
* an empty array to false: `compareValues({}, null)` is 1 and `({}, true)` is 0,
|
|
21
|
+
* where PHP's `[] <=> null` is 0 and `[] <=> true` is -1. The `[]` spelling agrees.
|
|
22
|
+
*
|
|
23
|
+
* JS-only: a `Map`, a `Set` and a `RegExp` have no PHP analogue, so there is no
|
|
24
|
+
* rule to port — each carries no own enumerable keys, and any two of them tie.
|
|
25
|
+
* That is a decision, not a divergence; do not "fix" it by analogy with `Date`.
|
|
4
26
|
*
|
|
5
27
|
* @param a - First value to compare
|
|
6
28
|
* @param b - Second value to compare
|
|
7
29
|
* @returns -1 if a < b, 1 if a > b, 0 if equal
|
|
8
30
|
*
|
|
9
31
|
* @example
|
|
10
|
-
* compareValues(
|
|
11
|
-
* compareValues(
|
|
12
|
-
* compareValues({x: 1}, {x: 1}); -> 0
|
|
32
|
+
* compareValues(0, ""); -> 1
|
|
33
|
+
* compareValues([1], [1, 2]); -> -1
|
|
13
34
|
*/
|
|
14
35
|
export declare function compareValues(a: unknown, b: unknown): number;
|
|
15
36
|
/**
|
|
16
|
-
* PHP-like loose equality comparison.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
37
|
+
* PHP-like loose equality comparison, following PHP 8's `==` rules.
|
|
38
|
+
*
|
|
39
|
+
* A `null` against a string compares as `""`; a `null` or a boolean against
|
|
40
|
+
* anything else compares both sides as booleans; two numeric operands compare
|
|
41
|
+
* numerically; everything else scalar compares as strings. Arrays and objects
|
|
42
|
+
* compare deeply.
|
|
19
43
|
*
|
|
20
44
|
* @param a - First value to compare
|
|
21
45
|
* @param b - Second value to compare
|
|
@@ -27,13 +51,16 @@ export declare function compareValues(a: unknown, b: unknown): number;
|
|
|
27
51
|
* looseEqual(null, 0); -> true
|
|
28
52
|
* looseEqual(null, ''); -> true
|
|
29
53
|
* looseEqual(0, false); -> true
|
|
54
|
+
* looseEqual(0, ''); -> false
|
|
30
55
|
* looseEqual(1, '1'); -> true
|
|
56
|
+
* looseEqual('1e1', '10'); -> true
|
|
31
57
|
* looseEqual(['a'], ['a']); -> true
|
|
32
58
|
*/
|
|
33
59
|
export declare function looseEqual(a: unknown, b: unknown): boolean;
|
|
34
60
|
/**
|
|
35
61
|
* PHP-like strict equality comparison (=== operator).
|
|
36
|
-
* Performs strict type checking for primitives but value-based comparison for arrays and objects
|
|
62
|
+
* Performs strict type checking for primitives but value-based comparison for arrays and objects:
|
|
63
|
+
* like PHP's `===` on arrays, two plain objects need the same key/value pairs in the same order.
|
|
37
64
|
* NOTE: For class instances (objects with constructors other than Object), uses reference equality.
|
|
38
65
|
*
|
|
39
66
|
* @param a - First value to compare
|
|
@@ -46,6 +73,37 @@ export declare function looseEqual(a: unknown, b: unknown): boolean;
|
|
|
46
73
|
* strictEqual(1, '1'); -> false (different types)
|
|
47
74
|
* strictEqual(['a'], ['a']); -> true (same array content)
|
|
48
75
|
* strictEqual({a: 1}, {a: 1}); -> true (same object content)
|
|
76
|
+
* strictEqual({a: 1, b: 2}, {b: 2, a: 1}); -> false (same pairs, another order)
|
|
49
77
|
*/
|
|
50
78
|
export declare function strictEqual(a: unknown, b: unknown): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Compare two values with one of PHP's `where()` operators, the way Laravel's
|
|
81
|
+
* `EnumeratesValues::operatorForWhere()` does.
|
|
82
|
+
*
|
|
83
|
+
* An unrecognised operator falls through to `=`, as PHP's `switch` default does.
|
|
84
|
+
* When exactly one side is an object and the pair holds fewer than two strings,
|
|
85
|
+
* only the inequality operators answer true. That short-circuit is Laravel's own, ahead
|
|
86
|
+
* of the switch; raw PHP does order such a pair, emitting `Notice: Object of class P
|
|
87
|
+
* could not be converted to int` and comparing as if the object were 1. A plain
|
|
88
|
+
* object is not one of those objects, because it models a PHP array here. `===` and
|
|
89
|
+
* `!==` take `strictEqual`, PHP's by-value rule for an array. Every other
|
|
90
|
+
* relational operator orders through `compareValues`, PHP's own comparison rule, so
|
|
91
|
+
* `null` is ordered rather than refused. `NaN` orders with no number and no string —
|
|
92
|
+
* `<`, `>`, `<=` and `>=` are all false there and `<=>` still answers 1, as PHP's
|
|
93
|
+
* `NAN <=> 1` does — but against a bool or null PHP casts both sides to bool and
|
|
94
|
+
* orders normally, so `NAN <=> true` is 0 and `NAN <=> null` is 1.
|
|
95
|
+
*
|
|
96
|
+
* @param retrieved - The value read from the item
|
|
97
|
+
* @param operator - The comparison operator (`=`, `==`, `!=`, `<>`, `<`, `>`, `<=`, `>=`, `===`, `!==`, `<=>`)
|
|
98
|
+
* @param value - The value to compare against
|
|
99
|
+
* @returns True if the comparison holds
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
*
|
|
103
|
+
* operatorMatch(3, '>', 2); -> true
|
|
104
|
+
* operatorMatch('4', '===', 4); -> false
|
|
105
|
+
* operatorMatch(1, '>', null); -> true (PHP casts null to false and 1 to true)
|
|
106
|
+
* operatorMatch(1, 'nonsense', '1'); -> true (unknown operators compare loosely)
|
|
107
|
+
*/
|
|
108
|
+
export declare function operatorMatch(retrieved: unknown, operator: string, value: unknown): boolean;
|
|
51
109
|
//# 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":"AA+IA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;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,CA0G1D;AAmHD;;;;;;;;;;;;;;;;;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,75 +1,208 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { isString as s, isBoolean as l, isObject as p, isPlainObject as S, isArray as O, isPhpNumeric as j, isPhpFalsy as H, isNull as R, isUndefined as U } from "./guards.js";
|
|
2
|
+
function h(t) {
|
|
3
|
+
return R(t) || U(t);
|
|
4
|
+
}
|
|
5
|
+
function N(t, r) {
|
|
6
|
+
return t < r ? -1 : t > r ? 1 : 0;
|
|
7
|
+
}
|
|
8
|
+
const T = /^[ \t\n\r\v\f]*[+-]?\d+[ \t\n\r\v\f]*$/, A = 2 ** 63, k = 2n ** 63n;
|
|
9
|
+
function B(t) {
|
|
10
|
+
return t >= k ? 1 : t < -k ? -1 : 0;
|
|
11
|
+
}
|
|
12
|
+
function D(t, r) {
|
|
13
|
+
if (T.test(t) && T.test(r)) {
|
|
14
|
+
const i = BigInt(t), c = BigInt(r), o = B(i);
|
|
15
|
+
return o !== 0 && o === B(c) && Number(t) === Number(r) ? N(t, r) : i < c ? -1 : i > c ? 1 : 0;
|
|
8
16
|
}
|
|
9
|
-
|
|
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;
|
|
10
19
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
function _(t) {
|
|
21
|
+
return typeof t == "number" ? t !== 0 : s(t) ? t !== "" && t !== "0" : O(t) ? t.length > 0 : !!t;
|
|
22
|
+
}
|
|
23
|
+
function E(t, r) {
|
|
24
|
+
return G(t, r, void 0);
|
|
25
|
+
}
|
|
26
|
+
function G(t, r, n) {
|
|
27
|
+
if (h(t) && s(r))
|
|
28
|
+
return N("", r);
|
|
29
|
+
if (s(t) && h(r))
|
|
30
|
+
return N(t, "");
|
|
31
|
+
if (h(t) || h(r) || l(t) || l(r)) {
|
|
32
|
+
const e = _(t), i = _(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;
|
|
17
38
|
}
|
|
18
|
-
|
|
19
|
-
|
|
39
|
+
if (typeof t == "object" && typeof r == "object")
|
|
40
|
+
return q(t, r, n ?? /* @__PURE__ */ new Map());
|
|
41
|
+
if (j(t) && j(r)) {
|
|
42
|
+
if (s(t) && s(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 s(t) || s(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, o = r;
|
|
63
|
+
for (const u of e) {
|
|
64
|
+
if (!Object.hasOwn(r, u))
|
|
65
|
+
return 1;
|
|
66
|
+
const g = G(c[u], o[u], n);
|
|
67
|
+
if (g !== 0)
|
|
68
|
+
return g;
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
function y(t, r) {
|
|
73
|
+
if (t === r)
|
|
20
74
|
return !0;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return !1;
|
|
24
|
-
for (let n = 0; n < r.length; n++)
|
|
25
|
-
if (!u(r[n], e[n]))
|
|
26
|
-
return !1;
|
|
75
|
+
const n = h(t), e = h(r);
|
|
76
|
+
if (n && e)
|
|
27
77
|
return !0;
|
|
78
|
+
if (n || e) {
|
|
79
|
+
const o = n ? r : t;
|
|
80
|
+
return s(o) ? o === "" : !P(o);
|
|
28
81
|
}
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
82
|
+
if (l(t) || l(r))
|
|
83
|
+
return P(t) === P(r);
|
|
84
|
+
if (m(t) || m(r))
|
|
85
|
+
return !1;
|
|
86
|
+
if (s(t) !== s(r)) {
|
|
87
|
+
const o = s(t) ? r : t;
|
|
88
|
+
if (p(o) && !S(o) && x(o))
|
|
89
|
+
return String(o) === (s(t) ? t : r);
|
|
90
|
+
}
|
|
91
|
+
const i = d(t) || s(t), c = d(r) || s(r);
|
|
92
|
+
if (i && c)
|
|
93
|
+
return w(t) && w(r) ? s(t) && s(r) ? D(t, r) === 0 : F(t) && F(r) ? BigInt(t) === BigInt(r) : Number(t) === Number(r) : V(t) === V(r);
|
|
94
|
+
if (O(t) && O(r)) {
|
|
95
|
+
if (t.length !== r.length)
|
|
32
96
|
return !1;
|
|
33
|
-
for (
|
|
34
|
-
if (!t
|
|
35
|
-
r[s],
|
|
36
|
-
e[s]
|
|
37
|
-
))
|
|
97
|
+
for (let o = 0; o < t.length; o++)
|
|
98
|
+
if (!y(t[o], r[o]))
|
|
38
99
|
return !1;
|
|
39
100
|
return !0;
|
|
40
101
|
}
|
|
102
|
+
if (p(t) && p(r)) {
|
|
103
|
+
const o = Object.keys(t), u = Object.keys(r);
|
|
104
|
+
return o.length !== u.length ? !1 : o.every(
|
|
105
|
+
(g) => u.includes(g) && y(
|
|
106
|
+
t[g],
|
|
107
|
+
r[g]
|
|
108
|
+
)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
41
111
|
return !1;
|
|
42
112
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
113
|
+
function P(t) {
|
|
114
|
+
return typeof t == "bigint" ? t !== 0n : p(t) && !S(t) ? !0 : !H(t);
|
|
115
|
+
}
|
|
116
|
+
function d(t) {
|
|
117
|
+
return typeof t == "number" || typeof t == "bigint";
|
|
118
|
+
}
|
|
119
|
+
function w(t) {
|
|
120
|
+
return typeof t == "bigint" || j(t);
|
|
121
|
+
}
|
|
122
|
+
function F(t) {
|
|
123
|
+
return typeof t == "bigint" ? !0 : typeof t == "number" ? Number.isInteger(t) && t >= -A && t < A : T.test(t) && B(BigInt(t)) === 0;
|
|
124
|
+
}
|
|
125
|
+
function m(t) {
|
|
126
|
+
return typeof t == "number" && Number.isNaN(t);
|
|
127
|
+
}
|
|
128
|
+
function x(t) {
|
|
129
|
+
const r = t.toString;
|
|
130
|
+
return typeof r == "function" && r !== Object.prototype.toString;
|
|
131
|
+
}
|
|
132
|
+
function V(t) {
|
|
133
|
+
if (typeof t == "number") {
|
|
134
|
+
if (t === Number.POSITIVE_INFINITY)
|
|
135
|
+
return "INF";
|
|
136
|
+
if (t === Number.NEGATIVE_INFINITY)
|
|
137
|
+
return "-INF";
|
|
138
|
+
}
|
|
139
|
+
return String(t);
|
|
140
|
+
}
|
|
141
|
+
function I(t, r) {
|
|
142
|
+
if (t === r)
|
|
45
143
|
return !0;
|
|
46
|
-
if (Array.isArray(
|
|
47
|
-
if (
|
|
144
|
+
if (Array.isArray(t) && Array.isArray(r)) {
|
|
145
|
+
if (t.length !== r.length)
|
|
48
146
|
return !1;
|
|
49
|
-
for (let
|
|
50
|
-
if (!
|
|
147
|
+
for (let n = 0; n < t.length; n++)
|
|
148
|
+
if (!I(t[n], r[n]))
|
|
51
149
|
return !1;
|
|
52
150
|
return !0;
|
|
53
151
|
}
|
|
54
|
-
if (
|
|
55
|
-
const
|
|
56
|
-
if (!
|
|
152
|
+
if (t !== null && r !== null && typeof t == "object" && typeof r == "object" && !Array.isArray(t) && !Array.isArray(r)) {
|
|
153
|
+
const n = t.constructor === Object || t.constructor === void 0, e = r.constructor === Object || r.constructor === void 0;
|
|
154
|
+
if (!n || !e)
|
|
57
155
|
return !1;
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
156
|
+
const i = Object.keys(t), c = Object.keys(r);
|
|
157
|
+
if (i.length !== c.length)
|
|
60
158
|
return !1;
|
|
61
|
-
for (const
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
159
|
+
for (const [o, u] of i.entries())
|
|
160
|
+
if (u !== c[o] || !I(
|
|
161
|
+
t[u],
|
|
162
|
+
r[u]
|
|
65
163
|
))
|
|
66
164
|
return !1;
|
|
67
165
|
return !0;
|
|
68
166
|
}
|
|
69
167
|
return !1;
|
|
70
168
|
}
|
|
169
|
+
function Y(t, r, n) {
|
|
170
|
+
const e = [t, n], i = (f) => p(f) && !S(f);
|
|
171
|
+
if (e.filter(
|
|
172
|
+
(f) => s(f) || i(f) && x(f)
|
|
173
|
+
).length < 2 && e.filter(i).length === 1)
|
|
174
|
+
return ["!=", "<>", "!=="].includes(r);
|
|
175
|
+
const u = !(l(t) || l(n) || h(t) || h(n)) && (m(t) || m(n)), g = (f) => !u && f(E(t, n));
|
|
176
|
+
switch (r) {
|
|
177
|
+
case "!=":
|
|
178
|
+
case "<>":
|
|
179
|
+
return !y(t, n);
|
|
180
|
+
case "<":
|
|
181
|
+
return g((f) => f < 0);
|
|
182
|
+
case ">":
|
|
183
|
+
return g((f) => f > 0);
|
|
184
|
+
case "<=":
|
|
185
|
+
return g((f) => f <= 0);
|
|
186
|
+
case ">=":
|
|
187
|
+
return g((f) => f >= 0);
|
|
188
|
+
// PHP's `===` compares an array by value — same keys, same order, same types —
|
|
189
|
+
// and only a real object by identity. strictEqual is that rule; JS's own `===`
|
|
190
|
+
// would call every pair of equal arrays unequal (task-24, "r4-strict-operators").
|
|
191
|
+
case "===":
|
|
192
|
+
return I(t, n);
|
|
193
|
+
case "!==":
|
|
194
|
+
return !I(t, n);
|
|
195
|
+
// PHP's `<=>` is truthy for any non-zero result, so only an equal pair is
|
|
196
|
+
// falsy; an uncomparable one answers 1, not 0, and so counts as unequal.
|
|
197
|
+
case "<=>":
|
|
198
|
+
return u || E(t, n) !== 0;
|
|
199
|
+
default:
|
|
200
|
+
return y(t, n);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
71
203
|
export {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
204
|
+
E as compareValues,
|
|
205
|
+
y as looseEqual,
|
|
206
|
+
Y as operatorMatch,
|
|
207
|
+
I as strictEqual
|
|
75
208
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a lookup that must match exactly one item matches none.
|
|
3
|
+
*
|
|
4
|
+
* Laravel's `Illuminate\Support\ItemNotFoundException` extends `RuntimeException`
|
|
5
|
+
* without a constructor, so it carries no message at all — probed as
|
|
6
|
+
* `docs/php-parity/task-24-data-release-readiness.json`, "sole-empty-no-callback"
|
|
7
|
+
* and `task-23-obj-release-readiness.json`, "sole-none".
|
|
8
|
+
*/
|
|
9
|
+
export declare class ItemNotFoundException extends Error {
|
|
10
|
+
/**
|
|
11
|
+
* Create a new exception instance.
|
|
12
|
+
*/
|
|
13
|
+
constructor();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Thrown when a lookup that must match exactly one item matches several.
|
|
17
|
+
*
|
|
18
|
+
* The message is Laravel's `"{$count} items were found."` — probed as
|
|
19
|
+
* `docs/php-parity/task-24-data-release-readiness.json`, "sole-multi-no-callback"
|
|
20
|
+
* and `task-23-obj-release-readiness.json`, "sole-multi-list".
|
|
21
|
+
*/
|
|
22
|
+
export declare class MultipleItemsFoundException extends Error {
|
|
23
|
+
/** The number of items found, as Laravel's public `$count`. */
|
|
24
|
+
readonly count: number;
|
|
25
|
+
/**
|
|
26
|
+
* Create a new exception instance.
|
|
27
|
+
*
|
|
28
|
+
* @param count - The number of items the lookup matched
|
|
29
|
+
*/
|
|
30
|
+
constructor(count: number);
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=exceptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.d.ts","sourceRoot":"","sources":["../src/exceptions.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC5C;;OAEG;;CAMN;AAED;;;;;;GAMG;AACH,qBAAa,2BAA4B,SAAQ,KAAK;IAClD,+DAA+D;IAC/D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;gBACS,KAAK,EAAE,MAAM;CAM5B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class o extends Error {
|
|
2
|
+
/**
|
|
3
|
+
* Create a new exception instance.
|
|
4
|
+
*/
|
|
5
|
+
constructor() {
|
|
6
|
+
super(""), this.name = "ItemNotFoundException";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class n extends Error {
|
|
10
|
+
/** The number of items found, as Laravel's public `$count`. */
|
|
11
|
+
count;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new exception instance.
|
|
14
|
+
*
|
|
15
|
+
* @param count - The number of items the lookup matched
|
|
16
|
+
*/
|
|
17
|
+
constructor(t) {
|
|
18
|
+
super(`${t} items were found.`), this.name = "MultipleItemsFoundException", this.count = t;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
o as ItemNotFoundException,
|
|
23
|
+
n as MultipleItemsFoundException
|
|
24
|
+
};
|