@tolki/utils 1.1.0 → 1.3.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 ADDED
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Check if a value is arrayable (has a toArray method).
3
+ *
4
+ * @param value - The value to check
5
+ * @returns True if the value is arrayable
6
+ *
7
+ * @example
8
+ *
9
+ * isArrayable({ toArray: () => [1, 2, 3] }); -> true
10
+ * isArrayable([1, 2, 3]); -> false
11
+ * isArrayable("hello"); -> false
12
+ */
13
+ export declare function toArrayable<T>(value: unknown): value is {
14
+ toArray(): T[];
15
+ };
16
+ /**
17
+ * Check if a value is jsonable (has a toJSON method).
18
+ *
19
+ * @param value - The value to check
20
+ * @returns True if the value is jsonable
21
+ *
22
+ * @example
23
+ *
24
+ * isJsonable({ toJSON: () => ({ a: 1 }) }); -> true
25
+ * isJsonable("hello"); -> false
26
+ */
27
+ export declare function toJsonable<T>(value: unknown): value is {
28
+ toJson(): T;
29
+ } | {
30
+ toJSON(): T;
31
+ };
32
+ /**
33
+ * Check if a value is json serializable (has a jsonSerialize method).
34
+ *
35
+ * @param value - The value to check
36
+ * @returns True if the value is json serializable
37
+ *
38
+ * @example
39
+ *
40
+ * isJsonSerializable({ jsonSerialize: () => ({ a: 1 }) }); -> true
41
+ * isJsonSerializable("hello"); -> false
42
+ */
43
+ export declare function toJsonSerializable<T>(value: unknown): value is {
44
+ jsonSerialize(): T;
45
+ };
46
+ /**
47
+ * Convert a value to an array if it's already an array, otherwise return null.
48
+ * Used internally for safe array conversion without coercion.
49
+ *
50
+ * @param {unknown} value - The value to convert.
51
+ * @returns {unknown[] | null} The array if value is an array, null otherwise.
52
+ * @example
53
+ * Convert to array
54
+ * toArray([1, 2, 3]); -> [1, 2, 3]
55
+ * toArray("hello"); -> null
56
+ * toArray({}); -> null
57
+ */
58
+ export declare function castableToArray<T>(value: unknown): T[] | null;
59
+ /**
60
+ * Helper function to normalize data to an array format.
61
+ * Handles arrays only now.
62
+ *
63
+ * @param data - The data to normalize (array or other)
64
+ * @returns An array representation of the data, or null if not accessible
65
+ *
66
+ * @example
67
+ * normalizeToArray([1, 2, 3]); -> [1, 2, 3]
68
+ * normalizeToArray('hello'); -> null
69
+ */
70
+ export declare function normalizeToArray<T>(data: ReadonlyArray<T> | unknown): T[] | null;
71
+ /**
72
+ * Helper function to get normalized values from data.
73
+ * Returns array values or empty array if data is not accessible.
74
+ *
75
+ * @param data - The data to get values from
76
+ * @returns Array of values or empty array
77
+ *
78
+ * @example
79
+ * getAccessibleValues([1, 2, 3]); -> [1, 2, 3]
80
+ * getAccessibleValues('hello'); -> []
81
+ */
82
+ export declare function getAccessibleValues<T>(data: ReadonlyArray<T> | unknown): T[];
83
+ /**
84
+ * Normalize a set-operation operand the way Laravel's
85
+ * `EnumeratesValues::getArrayableItems()` does: nullish becomes an empty array,
86
+ * an Enumerable/Arrayable-like object unwraps via `all()`/`toArray()`, an
87
+ * iterable spreads, a plain object contributes its values, anything else
88
+ * becomes a one-element array.
89
+ *
90
+ * @param items - The operand to normalize
91
+ * @returns The operand's values, in iteration order
92
+ *
93
+ * @example
94
+ * arrayableValues({ x: 20 }); -> [20]
95
+ */
96
+ export declare function arrayableValues<T>(items: unknown): T[];
97
+ /**
98
+ * Cast a CSS-list value the way PHP casts it when pushed raw into
99
+ * `implode()`/`Str::finish()`: `null` becomes `""`, a boolean becomes
100
+ * `"1"`/`""`, and everything else goes through `String()`.
101
+ *
102
+ * @param value - The CSS class or style fragment to cast.
103
+ * @returns The string PHP would have interpolated.
104
+ */
105
+ export declare function cssListItemToString(value: unknown): string;
106
+ //# sourceMappingURL=cast.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cast.d.ts","sourceRoot":"","sources":["../src/cast.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;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;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,EAAE,CAsCtD;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAU1D"}
package/dist/cast.js ADDED
@@ -0,0 +1,46 @@
1
+ import { isNull as o, isUndefined as u, isArray as f, isObject as i, isFunction as t, isMap as c, isIterable as a, isBoolean as l } from "./guards.js";
2
+ function b(r) {
3
+ return i(r) && !o(r) && t(r.toArray);
4
+ }
5
+ function J(r) {
6
+ if (!i(r) || o(r))
7
+ return !1;
8
+ const n = t(r.toJson), s = t(r.toJSON);
9
+ return n || s;
10
+ }
11
+ function S(r) {
12
+ return i(r) && !o(r) && t(r.jsonSerialize);
13
+ }
14
+ function O(r) {
15
+ return f(r) ? r : null;
16
+ }
17
+ function y(r) {
18
+ return Array.isArray(r) ? r.slice() : null;
19
+ }
20
+ function z(r) {
21
+ return y(r) || [];
22
+ }
23
+ function e(r) {
24
+ if (o(r) || u(r))
25
+ return [];
26
+ if (f(r))
27
+ return r.slice();
28
+ if (i(r)) {
29
+ const n = r;
30
+ return t(n.all) ? e(n.all()) : t(n.toArray) ? e(n.toArray()) : t(n.toJSON) ? e(n.toJSON()) : c(r) ? [...r.values()] : a(r) ? [...r] : Object.values(r);
31
+ }
32
+ return [r];
33
+ }
34
+ function N(r) {
35
+ return o(r) || u(r) ? "" : l(r) ? r ? "1" : "" : String(r);
36
+ }
37
+ export {
38
+ e as arrayableValues,
39
+ O as castableToArray,
40
+ N as cssListItemToString,
41
+ z as getAccessibleValues,
42
+ y as normalizeToArray,
43
+ b as toArrayable,
44
+ S as toJsonSerializable,
45
+ J as toJsonable
46
+ };
@@ -0,0 +1,59 @@
1
+ /**
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; arrays/objects order by JSON form.
6
+ *
7
+ * Faithful to PHP, this order is **not transitive** — `null` ties `0` and `""`,
8
+ * yet `0 > ""`.
9
+ *
10
+ * @param a - First value to compare
11
+ * @param b - Second value to compare
12
+ * @returns -1 if a < b, 1 if a > b, 0 if equal
13
+ *
14
+ * @example
15
+ * compareValues(0, ""); -> 1
16
+ */
17
+ export declare function compareValues(a: unknown, b: unknown): number;
18
+ /**
19
+ * PHP-like loose equality comparison, following PHP 8's `==` rules.
20
+ *
21
+ * A `null` against a string compares as `""`; a `null` or a boolean against
22
+ * anything else compares both sides as booleans; two numeric operands compare
23
+ * numerically; everything else scalar compares as strings. Arrays and objects
24
+ * compare deeply.
25
+ *
26
+ * @param a - First value to compare
27
+ * @param b - Second value to compare
28
+ * @returns True if values are loosely equal in PHP-like manner
29
+ *
30
+ * @example
31
+ *
32
+ * looseEqual(null, false); -> true
33
+ * looseEqual(null, 0); -> true
34
+ * looseEqual(null, ''); -> true
35
+ * looseEqual(0, false); -> true
36
+ * looseEqual(0, ''); -> false
37
+ * looseEqual(1, '1'); -> true
38
+ * looseEqual('1e1', '10'); -> true
39
+ * looseEqual(['a'], ['a']); -> true
40
+ */
41
+ export declare function looseEqual(a: unknown, b: unknown): boolean;
42
+ /**
43
+ * PHP-like strict equality comparison (=== operator).
44
+ * Performs strict type checking for primitives but value-based comparison for arrays and objects.
45
+ * NOTE: For class instances (objects with constructors other than Object), uses reference equality.
46
+ *
47
+ * @param a - First value to compare
48
+ * @param b - Second value to compare
49
+ * @returns True if values are strictly equal in PHP-like manner
50
+ *
51
+ * @example
52
+ *
53
+ * strictEqual(1, 1); -> true
54
+ * strictEqual(1, '1'); -> false (different types)
55
+ * strictEqual(['a'], ['a']); -> true (same array content)
56
+ * strictEqual({a: 1}, {a: 1}); -> true (same object content)
57
+ */
58
+ export declare function strictEqual(a: unknown, b: unknown): boolean;
59
+ //# sourceMappingURL=equality.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"equality.d.ts","sourceRoot":"","sources":["../src/equality.ts"],"names":[],"mappings":"AAwIA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAyC5D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CA0G1D;AAgID;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAqE3D"}
@@ -0,0 +1,147 @@
1
+ import { isString as i, isBoolean as g, isPhpNumeric as p, isObject as N, isArray as I, isNull as V, isUndefined as H, isPhpFalsy as R } from "./guards.js";
2
+ function s(t) {
3
+ return V(t) || H(t);
4
+ }
5
+ function c(t, r) {
6
+ return t < r ? -1 : t > r ? 1 : 0;
7
+ }
8
+ const m = /^[ \t\n\r\v\f]*[+-]?\d+[ \t\n\r\v\f]*$/, S = 2 ** 63, P = 2n ** 63n;
9
+ function h(t) {
10
+ return t >= P ? 1 : t < -P ? -1 : 0;
11
+ }
12
+ function F(t, r) {
13
+ if (m.test(t) && m.test(r)) {
14
+ const o = BigInt(t), u = BigInt(r), n = h(o);
15
+ return n !== 0 && n === h(u) && Number(t) === Number(r) ? c(t, r) : o < u ? -1 : o > u ? 1 : 0;
16
+ }
17
+ const e = Number(t), f = Number(r);
18
+ return e === f && !Number.isFinite(e) ? c(t, r) : e < f ? -1 : e > f ? 1 : 0;
19
+ }
20
+ function j(t) {
21
+ return typeof t == "number" ? t !== 0 : i(t) ? t !== "" && t !== "0" : I(t) ? t.length > 0 : !!t;
22
+ }
23
+ function w(t, r) {
24
+ if (s(t) && i(r))
25
+ return c("", r);
26
+ if (i(t) && s(r))
27
+ return c(t, "");
28
+ if (s(t) || s(r) || g(t) || g(r)) {
29
+ const e = j(t), f = j(r);
30
+ return e === f ? 0 : e ? 1 : -1;
31
+ }
32
+ if (typeof t == "object" && typeof r == "object")
33
+ return c(JSON.stringify(t), JSON.stringify(r));
34
+ if (p(t) && p(r)) {
35
+ if (i(t) && i(r))
36
+ return F(t, r);
37
+ const e = Number(t), f = Number(r);
38
+ return e < f ? -1 : e > f ? 1 : 0;
39
+ }
40
+ return i(t) || i(r) ? c(String(t), String(r)) : t < r ? -1 : t > r ? 1 : 0;
41
+ }
42
+ function B(t, r) {
43
+ if (t === r)
44
+ return !0;
45
+ const e = s(t), f = s(r);
46
+ if (e && f)
47
+ return !0;
48
+ if (e || f) {
49
+ const n = e ? r : t;
50
+ return i(n) ? n === "" : !l(n);
51
+ }
52
+ if (g(t) || g(r))
53
+ return l(t) === l(r);
54
+ if (d(t) || d(r))
55
+ return !1;
56
+ if (i(t) !== i(r)) {
57
+ const n = i(t) ? r : t;
58
+ if (N(n) && !G(n) && U(n))
59
+ return String(n) === (i(t) ? t : r);
60
+ }
61
+ const o = A(t) || i(t), u = A(r) || i(r);
62
+ if (o && u)
63
+ return T(t) && T(r) ? i(t) && i(r) ? F(t, r) === 0 : _(t) && _(r) ? BigInt(t) === BigInt(r) : Number(t) === Number(r) : k(t) === k(r);
64
+ if (I(t) && I(r)) {
65
+ if (t.length !== r.length)
66
+ return !1;
67
+ for (let n = 0; n < t.length; n++)
68
+ if (!B(t[n], r[n]))
69
+ return !1;
70
+ return !0;
71
+ }
72
+ if (N(t) && N(r)) {
73
+ const n = Object.keys(t), O = Object.keys(r);
74
+ return n.length !== O.length ? !1 : n.every(
75
+ (y) => O.includes(y) && B(
76
+ t[y],
77
+ r[y]
78
+ )
79
+ );
80
+ }
81
+ return !1;
82
+ }
83
+ function G(t) {
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) {
91
+ return typeof t == "number" || typeof t == "bigint";
92
+ }
93
+ function T(t) {
94
+ return typeof t == "bigint" || p(t);
95
+ }
96
+ function _(t) {
97
+ return typeof t == "bigint" ? !0 : typeof t == "number" ? Number.isInteger(t) && t >= -S && t < S : m.test(t) && h(BigInt(t)) === 0;
98
+ }
99
+ function d(t) {
100
+ return typeof t == "number" && Number.isNaN(t);
101
+ }
102
+ function U(t) {
103
+ const r = t.toString;
104
+ return typeof r == "function" && r !== Object.prototype.toString;
105
+ }
106
+ function k(t) {
107
+ if (typeof t == "number") {
108
+ if (t === Number.POSITIVE_INFINITY)
109
+ return "INF";
110
+ if (t === Number.NEGATIVE_INFINITY)
111
+ return "-INF";
112
+ }
113
+ return String(t);
114
+ }
115
+ function E(t, r) {
116
+ if (t === r)
117
+ return !0;
118
+ if (Array.isArray(t) && Array.isArray(r)) {
119
+ if (t.length !== r.length)
120
+ return !1;
121
+ for (let e = 0; e < t.length; e++)
122
+ if (!E(t[e], r[e]))
123
+ return !1;
124
+ return !0;
125
+ }
126
+ if (t !== null && r !== null && typeof t == "object" && typeof r == "object" && !Array.isArray(t) && !Array.isArray(r)) {
127
+ const e = t.constructor === Object || t.constructor === void 0, f = r.constructor === Object || r.constructor === void 0;
128
+ if (!e || !f)
129
+ return !1;
130
+ const o = Object.keys(t), u = Object.keys(r);
131
+ if (o.length !== u.length)
132
+ return !1;
133
+ for (const n of o)
134
+ if (!u.includes(n) || !E(
135
+ t[n],
136
+ r[n]
137
+ ))
138
+ return !1;
139
+ return !0;
140
+ }
141
+ return !1;
142
+ }
143
+ export {
144
+ w as compareValues,
145
+ B as looseEqual,
146
+ E as strictEqual
147
+ };