@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/guards.d.ts CHANGED
@@ -19,6 +19,21 @@ export declare function isArray<T>(value: DataItems<T> | T[] | unknown): value i
19
19
  * isObject(null); -> false
20
20
  */
21
21
  export declare function isObject<T, K extends PropertyKey = PropertyKey>(value: DataItems<T, K> | unknown): value is Record<K, T>;
22
+ /**
23
+ * Check if a value is a plain object: one whose prototype is `Object.prototype`
24
+ * or `null`, the shape this port models a PHP array with.
25
+ *
26
+ * @param value - The value to check
27
+ * @returns True if the value is a plain object
28
+ *
29
+ * @example
30
+ *
31
+ * isPlainObject({ a: 1 }); -> true
32
+ * isPlainObject(Object.create(null)); -> true
33
+ * isPlainObject(new Date()); -> false
34
+ * isPlainObject([1, 2]); -> false
35
+ */
36
+ export declare function isPlainObject(value: unknown): value is Record<PropertyKey, unknown>;
22
37
  /**
23
38
  * Check if a value is any object (including arrays, null).
24
39
  *
@@ -46,6 +61,26 @@ export declare function isObjectAny(value: unknown): value is object;
46
61
  * isTruthyObject(undefined); -> false
47
62
  */
48
63
  export declare function isTruthyObject(value: unknown): value is object;
64
+ /**
65
+ * Check if a value is a prototype object: the `prototype` of the constructor it
66
+ * carries as an own property. True for `Object.prototype`, `Array.prototype`,
67
+ * `Function.prototype` and every class prototype.
68
+ *
69
+ * Detects only prototypes carrying an own *function* `constructor`, so
70
+ * `%IteratorPrototype%` and friends -- whose `constructor` is an accessor or
71
+ * absent -- are not detected, and neither is a `Proxy` wrapping a prototype.
72
+ *
73
+ * @param value - The value to check
74
+ * @returns True if the value is some function's prototype object
75
+ *
76
+ * @example
77
+ *
78
+ * isPrototypeObject(Object.prototype); -> true
79
+ * isPrototypeObject(Array.prototype); -> true
80
+ * isPrototypeObject({ constructor: Object }); -> false
81
+ * isPrototypeObject({ a: 1 }); -> false
82
+ */
83
+ export declare function isPrototypeObject(value: unknown): boolean;
49
84
  /**
50
85
  * Check if a value is a string.
51
86
  *
@@ -294,6 +329,37 @@ export declare function isFalsy<TValue = unknown>(value: TValue): boolean;
294
329
  * isTruthy({}); -> false
295
330
  */
296
331
  export declare function isTruthy(value: unknown): boolean;
332
+ /**
333
+ * Determine whether a value is falsy the way PHP's `array_filter()` (no
334
+ * callback) treats it — PHP's own truthiness, not JS's.
335
+ *
336
+ * Drops `false`, `null`/`undefined`, `0`, `""`, `"0"`, and an empty array or
337
+ * plain object; keeps `"00"`, `"0.0"`, and `NaN` (all truthy in PHP).
338
+ *
339
+ * @param value - The value to check
340
+ * @returns True if the value is falsy under PHP's rules
341
+ *
342
+ * @example
343
+ *
344
+ * isPhpFalsy("0"); -> true
345
+ * isPhpFalsy("00"); -> false
346
+ */
347
+ export declare function isPhpFalsy(value: unknown): boolean;
348
+ /**
349
+ * Determine whether a value is numeric the way PHP's `is_numeric()` treats
350
+ * it, using PHP's numeric-string grammar rather than JS's `Number()`.
351
+ *
352
+ * `Number("")`, `Number(" ")`, `Number("0x10")`, and `Number("Infinity")` are
353
+ * all numeric to JS but not to PHP.
354
+ *
355
+ * @param value - The value to check
356
+ * @returns True if the value is numeric under PHP's rules
357
+ *
358
+ * @example
359
+ *
360
+ * isPhpNumeric("0x10"); -> false
361
+ */
362
+ export declare function isPhpNumeric(value: unknown): boolean;
297
363
  /**
298
364
  * Check if a value is a primitive type (null, boolean, number, string, symbol, undefined).
299
365
  *
@@ -350,4 +416,51 @@ export declare function isFiniteNumber(value: unknown): value is number;
350
416
  * isAccessibleData('hello'); -> false
351
417
  */
352
418
  export declare function isAccessibleData(data: unknown): boolean;
419
+ /**
420
+ * Check whether a value is one PHP's `Arr::accessible` would walk: a value that
421
+ * carries array entries rather than object state.
422
+ *
423
+ * An array and a plain object both model a PHP array; a `Map` is the JavaScript
424
+ * shape for a PHP array with non-string keys. A `Date`, a `Set`, a class instance
425
+ * or any other exotic object keeps its state somewhere other than its own
426
+ * enumerable keys, so walking it reads nothing — PHP rejects a `DateTime` the
427
+ * same way, because it does not implement `ArrayAccess`.
428
+ *
429
+ * @param value - The value to check
430
+ * @returns True if the value's own entries are its data
431
+ *
432
+ * @example
433
+ *
434
+ * isPhpAccessible({ a: 1 }); -> true
435
+ * isPhpAccessible([1, 2]); -> true
436
+ * isPhpAccessible(new Map()); -> true
437
+ * isPhpAccessible(new Date()); -> false
438
+ * isPhpAccessible(new Set()); -> false
439
+ */
440
+ export declare function isPhpAccessible(value: unknown): value is object;
441
+ /**
442
+ * Determine whether two values match the way PHP's `array_diff`/`array_intersect`
443
+ * do: `(string) $a === (string) $b`. Only `string`, `boolean`, `null`, and plain
444
+ * finite numbers cast this way; everything else falls back to SameValueZero
445
+ * identity. A high-precision float still casts (JS keeps every digit; PHP's
446
+ * `precision=14` rounds) — a real divergence, not a fallback.
447
+ *
448
+ * @param a - First value to compare
449
+ * @param b - Second value to compare
450
+ * @returns True if the values match under PHP's string-cast comparison, or are identical
451
+ *
452
+ * @example
453
+ *
454
+ * phpValueMatch(0, "0"); -> true
455
+ */
456
+ export declare function phpValueMatch(a: unknown, b: unknown): boolean;
457
+ /**
458
+ * Build a reusable membership test with the same semantics as `phpValueMatch`,
459
+ * without its O(n) rescan per call: cast operands go into a `Set`, so `diff`/
460
+ * `intersect` can test each item in O(1) instead of scanning `others` per item.
461
+ *
462
+ * @param others - The values to test membership against.
463
+ * @returns A predicate that is true when a value matches any of them.
464
+ */
465
+ export declare function phpValueMatcher(others: readonly unknown[]): (value: unknown) => boolean;
353
466
  //# sourceMappingURL=guards.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../src/guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,GAAG,KAA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MAAM,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAsChE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEhD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAuBlD;AAWD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAYpD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASnD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEtD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE9D;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAEvD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE/D;AAkCD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAe7D;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC3B,MAAM,EAAE,SAAS,OAAO,EAAE,GAC3B,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAuB7B"}
package/dist/guards.js CHANGED
@@ -1,116 +1,176 @@
1
- function f(n) {
2
- return Array.isArray(n);
1
+ function y(t) {
2
+ return Array.isArray(t);
3
3
  }
4
- function o(n) {
5
- return !r(n) && typeof n == "object" && !f(n);
4
+ function b(t) {
5
+ return !i(t) && typeof t == "object" && !y(t);
6
6
  }
7
- function c(n) {
8
- return typeof n == "object";
7
+ function h(t) {
8
+ if (!b(t))
9
+ return !1;
10
+ const n = Object.getPrototypeOf(t);
11
+ return n === Object.prototype || n === null;
9
12
  }
10
- function S(n) {
11
- return !r(n) && !e(n) && c(n);
13
+ function S(t) {
14
+ return typeof t == "object";
12
15
  }
13
- function i(n) {
14
- return typeof n == "string";
16
+ function P(t) {
17
+ return !i(t) && !c(t) && S(t);
15
18
  }
16
- function j(n) {
17
- return !!(i(n) || t(n) || b(n));
19
+ function T(t) {
20
+ if (!P(t) && !u(t))
21
+ return !1;
22
+ const n = Object.getOwnPropertyDescriptor(
23
+ t,
24
+ "constructor"
25
+ )?.value;
26
+ return u(n) && n.prototype === t;
18
27
  }
19
- function b(n) {
20
- return !!(o(n) && s(n.toString));
28
+ function o(t) {
29
+ return typeof t == "string";
21
30
  }
22
- function t(n) {
23
- return typeof n == "number" && !isNaN(n);
31
+ function W(t) {
32
+ return !!(o(t) || e(t) || j(t));
24
33
  }
25
- function A(n) {
26
- return t(n) && Number.isInteger(n);
34
+ function j(t) {
35
+ return !!(b(t) && u(t.toString));
27
36
  }
28
- function k(n) {
29
- return t(n) && !Number.isInteger(n);
37
+ function e(t) {
38
+ return typeof t == "number" && !isNaN(t);
30
39
  }
31
- function F(n) {
32
- return t(n) && n >= 0;
40
+ function E(t) {
41
+ return e(t) && Number.isInteger(t);
33
42
  }
34
- function d(n) {
35
- return t(n) && n < 0;
43
+ function R(t) {
44
+ return e(t) && !Number.isInteger(t);
36
45
  }
37
- function u(n) {
38
- return typeof n == "boolean";
46
+ function _(t) {
47
+ return e(t) && t >= 0;
39
48
  }
40
- function s(n) {
41
- return typeof n == "function";
49
+ function w(t) {
50
+ return e(t) && t < 0;
42
51
  }
43
- function e(n) {
44
- return typeof n > "u";
52
+ function N(t) {
53
+ return typeof t == "boolean";
45
54
  }
46
- function y(n) {
47
- return typeof n == "symbol";
55
+ function u(t) {
56
+ return typeof t == "function";
48
57
  }
49
- function r(n) {
50
- return n === null;
58
+ function c(t) {
59
+ return typeof t > "u";
51
60
  }
52
- function m(n) {
53
- return n instanceof Map;
61
+ function d(t) {
62
+ return typeof t == "symbol";
54
63
  }
55
- function N(n) {
56
- return n instanceof Set;
64
+ function i(t) {
65
+ return t === null;
57
66
  }
58
- function h(n) {
59
- return r(n) || e(n) || i(n) ? !1 : s(
60
- n[Symbol.iterator]
67
+ function m(t) {
68
+ return t instanceof Map;
69
+ }
70
+ function O(t) {
71
+ return t instanceof Set;
72
+ }
73
+ function z(t) {
74
+ return i(t) || c(t) || o(t) ? !1 : u(
75
+ t[Symbol.iterator]
61
76
  );
62
77
  }
63
- function I(n) {
64
- return n instanceof WeakMap;
78
+ function B(t) {
79
+ return t instanceof WeakMap;
80
+ }
81
+ function D(t) {
82
+ return t instanceof WeakSet;
83
+ }
84
+ function A(t) {
85
+ return Number.isNaN(t) ? !0 : e(t) ? t === 0 : c(t) || i(t) ? !0 : N(t) ? t === !1 : o(t) ? t.trim() === "" : m(t) || O(t) ? t.size === 0 : y(t) ? t.length === 0 : b(t) ? Object.keys(t).length === 0 : !1;
86
+ }
87
+ function U(t) {
88
+ return !A(t);
89
+ }
90
+ function V(t) {
91
+ return t === !1 || t === null || c(t) || t === 0 || t === "" || t === "0" ? !0 : y(t) ? t.length === 0 : b(t) ? Object.keys(t).length === 0 : !1;
92
+ }
93
+ const k = /^[ \t\n\r\v\f]*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[ \t\n\r\v\f]*$/;
94
+ function x(t) {
95
+ return typeof t == "number" ? !0 : o(t) ? k.test(t) : !1;
96
+ }
97
+ function M(t) {
98
+ return i(t) || N(t) || e(t) || o(t) || d(t) || c(t);
65
99
  }
66
- function M(n) {
67
- return n instanceof WeakSet;
100
+ function C(t) {
101
+ return !M(t);
68
102
  }
69
- function p(n) {
70
- return Number.isNaN(n) ? !0 : t(n) ? n === 0 : e(n) || r(n) ? !0 : u(n) ? n === !1 : i(n) ? n.trim() === "" : m(n) || N(n) ? n.size === 0 : f(n) ? n.length === 0 : o(n) ? Object.keys(n).length === 0 : !1;
103
+ function F(t) {
104
+ return e(t) && Number.isFinite(t);
71
105
  }
72
- function O(n) {
73
- return !p(n);
106
+ function G(t) {
107
+ return Array.isArray(t);
74
108
  }
75
- function g(n) {
76
- return r(n) || u(n) || t(n) || i(n) || y(n) || e(n);
109
+ function H(t) {
110
+ return y(t) || h(t) || m(t);
77
111
  }
78
- function W(n) {
79
- return !g(n);
112
+ function p(t) {
113
+ if (o(t))
114
+ return t;
115
+ if (N(t))
116
+ return t ? "1" : "";
117
+ if (i(t))
118
+ return "";
119
+ if (F(t)) {
120
+ const n = String(t);
121
+ return n.includes("e") ? null : n;
122
+ }
123
+ return null;
80
124
  }
81
- function P(n) {
82
- return t(n) && Number.isFinite(n);
125
+ function I(t, n) {
126
+ const f = p(t), r = p(n);
127
+ return f !== null && r !== null ? f === r : t === n || typeof t == "number" && typeof n == "number" && Number.isNaN(t) && Number.isNaN(n);
83
128
  }
84
- function T(n) {
85
- return Array.isArray(n);
129
+ function $(t) {
130
+ const n = /* @__PURE__ */ new Set(), f = [];
131
+ for (const r of t) {
132
+ const s = p(r);
133
+ i(s) ? f.push(r) : n.add(s);
134
+ }
135
+ return (r) => {
136
+ const s = p(r);
137
+ return i(s) ? f.some((g) => I(r, g)) : n.has(s);
138
+ };
86
139
  }
87
140
  export {
88
- T as isAccessibleData,
89
- f as isArray,
90
- u as isBoolean,
91
- p as isFalsy,
92
- P as isFiniteNumber,
93
- k as isFloat,
94
- s as isFunction,
95
- A as isInteger,
96
- h as isIterable,
141
+ G as isAccessibleData,
142
+ y as isArray,
143
+ N as isBoolean,
144
+ A as isFalsy,
145
+ F as isFiniteNumber,
146
+ R as isFloat,
147
+ u as isFunction,
148
+ E as isInteger,
149
+ z as isIterable,
97
150
  m as isMap,
98
- d as isNegativeNumber,
99
- W as isNonPrimitive,
100
- r as isNull,
101
- t as isNumber,
102
- o as isObject,
103
- c as isObjectAny,
104
- F as isPositiveNumber,
105
- g as isPrimitive,
106
- N as isSet,
107
- i as isString,
108
- j as isStringable,
109
- y as isSymbol,
110
- O as isTruthy,
111
- S as isTruthyObject,
112
- e as isUndefined,
113
- I as isWeakMap,
114
- M as isWeakSet,
115
- b as objectToString
151
+ w as isNegativeNumber,
152
+ C as isNonPrimitive,
153
+ i as isNull,
154
+ e as isNumber,
155
+ b as isObject,
156
+ S as isObjectAny,
157
+ H as isPhpAccessible,
158
+ V as isPhpFalsy,
159
+ x as isPhpNumeric,
160
+ h as isPlainObject,
161
+ _ as isPositiveNumber,
162
+ M as isPrimitive,
163
+ T as isPrototypeObject,
164
+ O as isSet,
165
+ o as isString,
166
+ W as isStringable,
167
+ d as isSymbol,
168
+ U as isTruthy,
169
+ P as isTruthyObject,
170
+ c as isUndefined,
171
+ B as isWeakMap,
172
+ D as isWeakSet,
173
+ j as objectToString,
174
+ I as phpValueMatch,
175
+ $ as phpValueMatcher
116
176
  };
package/dist/index.js CHANGED
@@ -1,55 +1,81 @@
1
- import { castableToArray as r, getAccessibleValues as s, normalizeToArray as t, toArrayable as o, toJsonSerializable as a, toJsonable as l } from "./cast.js";
2
- import { compareValues as b, looseEqual as m, strictEqual as p } from "./equality.js";
3
- import { isAccessibleData as y, isArray as c, isBoolean as f, isFalsy as A, isFiniteNumber as N, isFloat as S, isFunction as g, isInteger as x, isIterable as F, isMap as v, isNegativeNumber as T, isNonPrimitive as j, isNull as K, isNumber as O, isObject as P, isObjectAny as d, isPositiveNumber as h, isPrimitive as U, isSet as V, isString as k, isStringable as q, isSymbol as w, isTruthy as z, isTruthyObject as D, isUndefined as E, isWeakMap as I, isWeakSet as J, objectToString as M } from "./guards.js";
4
- import { defineKey as B, entriesKeyValue as L, isPhpArrayKey as C, isUnsafeKey as G } from "./keys.js";
5
- import { resolveDefault as Q, typeOf as R } from "./reflect.js";
6
- import { lowerFirst as Y, toLower as Z, toUpper as _, upperFirst as $ } from "./string.js";
1
+ import { arrayableItems as i, arrayableValues as t, castableToArray as s, cssListItemToString as o, getAccessibleValues as a, normalizeToArray as p, toArrayable as l, toJsonSerializable as n, toJsonable as y, toPhpKeyString as c } from "./cast.js";
2
+ import { compareValues as u, looseEqual as b, operatorMatch as h, strictEqual as f } from "./equality.js";
3
+ import { ItemNotFoundException as x, MultipleItemsFoundException as S } from "./exceptions.js";
4
+ import { isAccessibleData as A, isArray as N, isBoolean as F, isFalsy as I, isFiniteNumber as K, isFloat as d, isFunction as M, isInteger as T, isIterable as V, isMap as j, isNegativeNumber as v, isNonPrimitive as O, isNull as E, isNumber as k, isObject as L, isObjectAny as U, isPhpAccessible as q, isPhpFalsy as w, isPhpNumeric as z, isPlainObject as D, isPositiveNumber as J, isPrimitive as W, isPrototypeObject as B, isSet as C, isString as R, isStringable as G, isSymbol as H, isTruthy as Q, isTruthyObject as X, isUndefined as Y, isWeakMap as Z, isWeakSet as _, objectToString as $, phpValueMatch as ee, phpValueMatcher as re } from "./guards.js";
5
+ import { defineKey as te, entriesKeyValue as se, isIntegerLikeKey as oe, isPhpArrayKey as ae, isUnsafeKey as pe, keyedEntries as le, phpArrayKey as ne, reindexIntegerKeys as ye, renumberPhpIntegerKeys as ce } from "./keys.js";
6
+ import { resolveSliceRange as ue } from "./range.js";
7
+ import { phpTypeName as he, resolveDefault as fe, typeOf as ge } from "./reflect.js";
8
+ import { createSortSpecComparator as Se } from "./sorting.js";
9
+ import { arrayValueMessage as Ae, lowerFirst as Ne, toLower as Fe, toUpper as Ie, upperFirst as Ke } from "./string.js";
7
10
  export {
8
- r as castableToArray,
9
- b as compareValues,
10
- B as defineKey,
11
- L as entriesKeyValue,
12
- s as getAccessibleValues,
13
- y as isAccessibleData,
14
- c as isArray,
15
- f as isBoolean,
16
- A as isFalsy,
17
- N as isFiniteNumber,
18
- S as isFloat,
19
- g as isFunction,
20
- x as isInteger,
21
- F as isIterable,
22
- v as isMap,
23
- T as isNegativeNumber,
24
- j as isNonPrimitive,
25
- K as isNull,
26
- O as isNumber,
27
- P as isObject,
28
- d as isObjectAny,
29
- C as isPhpArrayKey,
30
- h as isPositiveNumber,
31
- U as isPrimitive,
32
- V as isSet,
33
- k as isString,
34
- q as isStringable,
35
- w as isSymbol,
36
- z as isTruthy,
37
- D as isTruthyObject,
38
- E as isUndefined,
39
- G as isUnsafeKey,
40
- I as isWeakMap,
41
- J as isWeakSet,
42
- m as looseEqual,
43
- Y as lowerFirst,
44
- t as normalizeToArray,
45
- M as objectToString,
46
- Q as resolveDefault,
47
- p as strictEqual,
48
- o as toArrayable,
49
- a as toJsonSerializable,
50
- l as toJsonable,
51
- Z as toLower,
52
- _ as toUpper,
53
- R as typeOf,
54
- $ as upperFirst
11
+ x as ItemNotFoundException,
12
+ S as MultipleItemsFoundException,
13
+ Ae as arrayValueMessage,
14
+ i as arrayableItems,
15
+ t as arrayableValues,
16
+ s as castableToArray,
17
+ u as compareValues,
18
+ Se as createSortSpecComparator,
19
+ o as cssListItemToString,
20
+ te as defineKey,
21
+ se as entriesKeyValue,
22
+ a as getAccessibleValues,
23
+ A as isAccessibleData,
24
+ N as isArray,
25
+ F as isBoolean,
26
+ I as isFalsy,
27
+ K as isFiniteNumber,
28
+ d as isFloat,
29
+ M as isFunction,
30
+ T as isInteger,
31
+ oe as isIntegerLikeKey,
32
+ V as isIterable,
33
+ j as isMap,
34
+ v as isNegativeNumber,
35
+ O as isNonPrimitive,
36
+ E as isNull,
37
+ k as isNumber,
38
+ L as isObject,
39
+ U as isObjectAny,
40
+ q as isPhpAccessible,
41
+ ae as isPhpArrayKey,
42
+ w as isPhpFalsy,
43
+ z as isPhpNumeric,
44
+ D as isPlainObject,
45
+ J as isPositiveNumber,
46
+ W as isPrimitive,
47
+ B as isPrototypeObject,
48
+ C as isSet,
49
+ R as isString,
50
+ G as isStringable,
51
+ H as isSymbol,
52
+ Q as isTruthy,
53
+ X as isTruthyObject,
54
+ Y as isUndefined,
55
+ pe as isUnsafeKey,
56
+ Z as isWeakMap,
57
+ _ as isWeakSet,
58
+ le as keyedEntries,
59
+ b as looseEqual,
60
+ Ne as lowerFirst,
61
+ p as normalizeToArray,
62
+ $ as objectToString,
63
+ h as operatorMatch,
64
+ ne as phpArrayKey,
65
+ he as phpTypeName,
66
+ ee as phpValueMatch,
67
+ re as phpValueMatcher,
68
+ ye as reindexIntegerKeys,
69
+ ce as renumberPhpIntegerKeys,
70
+ fe as resolveDefault,
71
+ ue as resolveSliceRange,
72
+ f as strictEqual,
73
+ l as toArrayable,
74
+ n as toJsonSerializable,
75
+ y as toJsonable,
76
+ Fe as toLower,
77
+ c as toPhpKeyString,
78
+ Ie as toUpper,
79
+ ge as typeOf,
80
+ Ke as upperFirst
55
81
  };
package/dist/keys.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  /**
2
2
  * Figures out if the entry key should be a number or a string.
3
3
  *
4
+ * @deprecated Use `phpArrayKey` instead. This conversion is `Number()`/`parseFloat`, which is
5
+ * looser than PHP's: it turns `"01"` into `1`, `"1.5"` into `1.5` and `"0x10"` into `16`, keys
6
+ * PHP would all keep as strings. `phpArrayKey` converts only a canonical integer string, so a
7
+ * key handed to a callback is the key PHP stores. Kept because it is a published export.
8
+ *
4
9
  * @param value - The entry key value (number, string, or symbol)
5
10
  * @returns The entry key as a number if it can be converted, otherwise returns the original value
6
11
  */
@@ -23,14 +28,78 @@ export declare function isUnsafeKey(key: string): boolean;
23
28
  * @returns True if the value can be used as a PHP array key
24
29
  */
25
30
  export declare function isPhpArrayKey(value: unknown): value is string | number;
31
+ /**
32
+ * Whether `key` is a canonical non-negative integer string ("0", "1", "23",
33
+ * but not "01", "-1", "1.5") — the key class JS always sorts ahead of string
34
+ * keys, and PHP treats as an integer array key.
35
+ *
36
+ * @param key - The key to test
37
+ * @returns True if `key` is a canonical non-negative integer string
38
+ */
39
+ export declare function isIntegerLikeKey(key: string): boolean;
40
+ /**
41
+ * The key PHP stores for an array key: a canonical decimal integer string
42
+ * becomes a number and any other string stays the same string; `null` becomes
43
+ * `""`, a boolean `0` or `1`, and a float is truncated toward zero (INF and NAN become `0`).
44
+ *
45
+ * @param key - The key, as `Object.keys` reports it or as a value used as an array offset
46
+ * @returns The key PHP would report
47
+ *
48
+ * @example
49
+ * phpArrayKey("10"); -> 10
50
+ * phpArrayKey("01"); -> "01"
51
+ * phpArrayKey(true); -> 1
52
+ * phpArrayKey(1.5); -> 1
53
+ */
54
+ export declare function phpArrayKey(key: unknown): string | number;
55
+ /**
56
+ * Get the entries of a plain object or a Map, each keyed by the string PHP stores for its key.
57
+ *
58
+ * A Map keeps its insertion order, which a plain object cannot hold for integer keys. Keys PHP
59
+ * stores as one (`1` and `"1"`) fold into the first one's place with the last one's value.
60
+ *
61
+ * @param data - The plain object or Map to read.
62
+ * @returns The `[key, value]` pairs in iteration order.
63
+ *
64
+ * @example
65
+ * keyedEntries({ b: 1, a: 2 }); -> [["b", 1], ["a", 2]]
66
+ * keyedEntries(new Map([[2, "c"], [0, "a"]])); -> [["2", "c"], ["0", "a"]]
67
+ * keyedEntries(new Map([[1, "a"], ["1", "b"]])); -> [["1", "b"]]
68
+ */
69
+ export declare function keyedEntries<TValue = unknown>(data: object): [string, TValue][];
70
+ /**
71
+ * Renumber the integer-like keys in `entries` to a fresh 0-based sequence, in
72
+ * the order they appear; string keys pass through unchanged.
73
+ *
74
+ * The one integer-key policy for every reordering helper, since a plain JS
75
+ * object always re-sorts integer-like keys ascending (ECMA-262). Negative keys
76
+ * are excluded — JS doesn't re-sort those. A mixed object keeps neither order:
77
+ * integer keys hoist ahead of string keys, so `sort({x: 5, 0: 9})` gives `[9, 5]`.
78
+ *
79
+ * @param entries - The entries to renumber, in their intended order
80
+ * @returns The same entries with integer-like keys renumbered from 0
81
+ */
82
+ export declare function reindexIntegerKeys<TValue>(entries: [string, TValue][]): [string, TValue][];
83
+ /**
84
+ * Renumber every key PHP stores as an integer to a fresh 0-based sequence, in order, as `array_shift`,
85
+ * `array_splice` and `array_unshift` do. Unlike `reindexIntegerKeys`, a negative key such as "-1" counts too.
86
+ *
87
+ * @param entries - The entries to renumber, in their intended order
88
+ * @returns The same entries with every integer key renumbered from 0
89
+ */
90
+ export declare function renumberPhpIntegerKeys<TValue>(entries: [string, TValue][]): [string, TValue][];
26
91
  /**
27
92
  * Define an own enumerable property on the target without going through a
28
93
  * setter, so a key such as `__proto__` becomes a real own key rather than
29
94
  * reaching `Object.prototype` through the inherited setter.
30
95
  *
96
+ * `defineKey` is the sanctioned way to write a computed key onto a fresh result object;
97
+ * plain `result[key] = value` lets a `"__proto__"` key reparent `result`. A key that is
98
+ * already non-configurable falls back to assignment, since it cannot be a setter.
99
+ *
31
100
  * @param target - The object to define the key on
32
101
  * @param key - The key to define
33
102
  * @param value - The value to store under the key
34
103
  */
35
- export declare function defineKey<TValue>(target: Record<string, TValue>, key: string, value: TValue): void;
104
+ export declare function defineKey<TValue>(target: Record<string, TValue>, key: PropertyKey, value: TValue): void;
36
105
  //# sourceMappingURL=keys.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,WAAW,EACjD,KAAK,EAAE,CAAC,GACT,CAAC,SAAS,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,CAAC,CAMpC;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,GAAG,MAAM,CAatE;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAC5B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACd,IAAI,CAON"}
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","s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