@tolki/utils 1.3.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
  *
@@ -401,6 +416,28 @@ export declare function isFiniteNumber(value: unknown): value is number;
401
416
  * isAccessibleData('hello'); -> false
402
417
  */
403
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;
404
441
  /**
405
442
  * Determine whether two values match the way PHP's `array_diff`/`array_intersect`
406
443
  * do: `(string) $a === (string) $b`. Only `string`, `boolean`, `null`, and plain
@@ -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,KAAK,IAAI,CAAC,EAAE,CAE5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,GAAG,WAAW,EAC3D,KAAK,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,GACjC,KAAK,IAAI,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAEvB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE9D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAczD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CACxB,KAAK,EAAE,OAAO,GACf,KAAK,IAAI,MAAM,GAAG;IAAE,QAAQ,IAAI,MAAM,CAAA;CAAE,CAc1C;AAED;;;;;GAKG;AACH,wBAAgB,cAA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C,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,55 +1,61 @@
1
- function p(t) {
1
+ function y(t) {
2
2
  return Array.isArray(t);
3
3
  }
4
4
  function b(t) {
5
- return !i(t) && typeof t == "object" && !p(t);
5
+ return !i(t) && typeof t == "object" && !y(t);
6
6
  }
7
- function g(t) {
7
+ function h(t) {
8
+ if (!b(t))
9
+ return !1;
10
+ const n = Object.getPrototypeOf(t);
11
+ return n === Object.prototype || n === null;
12
+ }
13
+ function S(t) {
8
14
  return typeof t == "object";
9
15
  }
10
- function h(t) {
11
- return !i(t) && !u(t) && g(t);
16
+ function P(t) {
17
+ return !i(t) && !c(t) && S(t);
12
18
  }
13
- function I(t) {
14
- if (!h(t) && !c(t))
19
+ function T(t) {
20
+ if (!P(t) && !u(t))
15
21
  return !1;
16
22
  const n = Object.getOwnPropertyDescriptor(
17
23
  t,
18
24
  "constructor"
19
25
  )?.value;
20
- return c(n) && n.prototype === t;
26
+ return u(n) && n.prototype === t;
21
27
  }
22
28
  function o(t) {
23
29
  return typeof t == "string";
24
30
  }
25
- function T(t) {
26
- return !!(o(t) || e(t) || S(t));
31
+ function W(t) {
32
+ return !!(o(t) || e(t) || j(t));
27
33
  }
28
- function S(t) {
29
- return !!(b(t) && c(t.toString));
34
+ function j(t) {
35
+ return !!(b(t) && u(t.toString));
30
36
  }
31
37
  function e(t) {
32
38
  return typeof t == "number" && !isNaN(t);
33
39
  }
34
- function W(t) {
40
+ function E(t) {
35
41
  return e(t) && Number.isInteger(t);
36
42
  }
37
- function E(t) {
43
+ function R(t) {
38
44
  return e(t) && !Number.isInteger(t);
39
45
  }
40
- function R(t) {
46
+ function _(t) {
41
47
  return e(t) && t >= 0;
42
48
  }
43
- function _(t) {
49
+ function w(t) {
44
50
  return e(t) && t < 0;
45
51
  }
46
52
  function N(t) {
47
53
  return typeof t == "boolean";
48
54
  }
49
- function c(t) {
55
+ function u(t) {
50
56
  return typeof t == "function";
51
57
  }
52
- function u(t) {
58
+ function c(t) {
53
59
  return typeof t > "u";
54
60
  }
55
61
  function d(t) {
@@ -58,108 +64,113 @@ function d(t) {
58
64
  function i(t) {
59
65
  return t === null;
60
66
  }
61
- function P(t) {
67
+ function m(t) {
62
68
  return t instanceof Map;
63
69
  }
64
- function j(t) {
70
+ function O(t) {
65
71
  return t instanceof Set;
66
72
  }
67
- function w(t) {
68
- return i(t) || u(t) || o(t) ? !1 : c(
73
+ function z(t) {
74
+ return i(t) || c(t) || o(t) ? !1 : u(
69
75
  t[Symbol.iterator]
70
76
  );
71
77
  }
72
- function z(t) {
78
+ function B(t) {
73
79
  return t instanceof WeakMap;
74
80
  }
75
- function B(t) {
81
+ function D(t) {
76
82
  return t instanceof WeakSet;
77
83
  }
78
84
  function A(t) {
79
- return Number.isNaN(t) ? !0 : e(t) ? t === 0 : u(t) || i(t) ? !0 : N(t) ? t === !1 : o(t) ? t.trim() === "" : P(t) || j(t) ? t.size === 0 : p(t) ? t.length === 0 : b(t) ? Object.keys(t).length === 0 : !1;
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;
80
86
  }
81
- function D(t) {
87
+ function U(t) {
82
88
  return !A(t);
83
89
  }
84
- function U(t) {
85
- return t === !1 || t === null || u(t) || t === 0 || t === "" || t === "0" ? !0 : p(t) ? t.length === 0 : b(t) ? Object.keys(t).length === 0 : !1;
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;
86
92
  }
87
93
  const k = /^[ \t\n\r\v\f]*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[ \t\n\r\v\f]*$/;
88
- function V(t) {
94
+ function x(t) {
89
95
  return typeof t == "number" ? !0 : o(t) ? k.test(t) : !1;
90
96
  }
91
- function O(t) {
92
- return i(t) || N(t) || e(t) || o(t) || d(t) || u(t);
97
+ function M(t) {
98
+ return i(t) || N(t) || e(t) || o(t) || d(t) || c(t);
93
99
  }
94
- function x(t) {
95
- return !O(t);
100
+ function C(t) {
101
+ return !M(t);
96
102
  }
97
- function M(t) {
103
+ function F(t) {
98
104
  return e(t) && Number.isFinite(t);
99
105
  }
100
- function C(t) {
106
+ function G(t) {
101
107
  return Array.isArray(t);
102
108
  }
103
- function y(t) {
109
+ function H(t) {
110
+ return y(t) || h(t) || m(t);
111
+ }
112
+ function p(t) {
104
113
  if (o(t))
105
114
  return t;
106
115
  if (N(t))
107
116
  return t ? "1" : "";
108
117
  if (i(t))
109
118
  return "";
110
- if (M(t)) {
119
+ if (F(t)) {
111
120
  const n = String(t);
112
121
  return n.includes("e") ? null : n;
113
122
  }
114
123
  return null;
115
124
  }
116
- function F(t, n) {
117
- const f = y(t), r = y(n);
125
+ function I(t, n) {
126
+ const f = p(t), r = p(n);
118
127
  return f !== null && r !== null ? f === r : t === n || typeof t == "number" && typeof n == "number" && Number.isNaN(t) && Number.isNaN(n);
119
128
  }
120
- function G(t) {
129
+ function $(t) {
121
130
  const n = /* @__PURE__ */ new Set(), f = [];
122
131
  for (const r of t) {
123
- const s = y(r);
132
+ const s = p(r);
124
133
  i(s) ? f.push(r) : n.add(s);
125
134
  }
126
135
  return (r) => {
127
- const s = y(r);
128
- return i(s) ? f.some((m) => F(r, m)) : n.has(s);
136
+ const s = p(r);
137
+ return i(s) ? f.some((g) => I(r, g)) : n.has(s);
129
138
  };
130
139
  }
131
140
  export {
132
- C as isAccessibleData,
133
- p as isArray,
141
+ G as isAccessibleData,
142
+ y as isArray,
134
143
  N as isBoolean,
135
144
  A as isFalsy,
136
- M as isFiniteNumber,
137
- E as isFloat,
138
- c as isFunction,
139
- W as isInteger,
140
- w as isIterable,
141
- P as isMap,
142
- _ as isNegativeNumber,
143
- x as isNonPrimitive,
145
+ F as isFiniteNumber,
146
+ R as isFloat,
147
+ u as isFunction,
148
+ E as isInteger,
149
+ z as isIterable,
150
+ m as isMap,
151
+ w as isNegativeNumber,
152
+ C as isNonPrimitive,
144
153
  i as isNull,
145
154
  e as isNumber,
146
155
  b as isObject,
147
- g as isObjectAny,
148
- U as isPhpFalsy,
149
- V as isPhpNumeric,
150
- R as isPositiveNumber,
151
- O as isPrimitive,
152
- I as isPrototypeObject,
153
- j as isSet,
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,
154
165
  o as isString,
155
- T as isStringable,
166
+ W as isStringable,
156
167
  d as isSymbol,
157
- D as isTruthy,
158
- h as isTruthyObject,
159
- u as isUndefined,
160
- z as isWeakMap,
161
- B as isWeakSet,
162
- S as objectToString,
163
- F as phpValueMatch,
164
- G as phpValueMatcher
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
165
176
  };
package/dist/index.js CHANGED
@@ -1,70 +1,81 @@
1
- import { arrayableValues as i, castableToArray as s, cssListItemToString as t, getAccessibleValues as a, normalizeToArray as o, toArrayable as l, toJsonSerializable as p, toJsonable as n } from "./cast.js";
2
- import { compareValues as m, looseEqual as c, strictEqual as u } from "./equality.js";
3
- import { isAccessibleData as f, isArray as g, isBoolean as S, isFalsy as h, isFiniteNumber as x, isFloat as N, isFunction as A, isInteger as F, isIterable as P, isMap as T, isNegativeNumber as V, isNonPrimitive as v, isNull as K, isNumber as j, isObject as I, isObjectAny as M, isPhpFalsy as O, isPhpNumeric as d, isPositiveNumber as k, isPrimitive as L, isPrototypeObject as U, isSet as q, isString as w, isStringable as z, isSymbol as D, isTruthy as E, isTruthyObject as J, isUndefined as W, isWeakMap as B, isWeakSet as C, objectToString as R, phpValueMatch as G, phpValueMatcher as H } from "./guards.js";
4
- import { defineKey as X, entriesKeyValue as Y, isIntegerLikeKey as Z, isPhpArrayKey as _, isUnsafeKey as $, reindexIntegerKeys as ee } from "./keys.js";
5
- import { resolveSliceRange as ie } from "./range.js";
6
- import { phpTypeName as te, resolveDefault as ae, typeOf as oe } from "./reflect.js";
7
- import { createSortSpecComparator as pe } from "./sorting.js";
8
- import { arrayValueMessage as ye, lowerFirst as me, toLower as ce, toUpper as ue, upperFirst as be } 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";
9
10
  export {
10
- ye as arrayValueMessage,
11
- i as arrayableValues,
11
+ x as ItemNotFoundException,
12
+ S as MultipleItemsFoundException,
13
+ Ae as arrayValueMessage,
14
+ i as arrayableItems,
15
+ t as arrayableValues,
12
16
  s as castableToArray,
13
- m as compareValues,
14
- pe as createSortSpecComparator,
15
- t as cssListItemToString,
16
- X as defineKey,
17
- Y as entriesKeyValue,
17
+ u as compareValues,
18
+ Se as createSortSpecComparator,
19
+ o as cssListItemToString,
20
+ te as defineKey,
21
+ se as entriesKeyValue,
18
22
  a as getAccessibleValues,
19
- f as isAccessibleData,
20
- g as isArray,
21
- S as isBoolean,
22
- h as isFalsy,
23
- x as isFiniteNumber,
24
- N as isFloat,
25
- A as isFunction,
26
- F as isInteger,
27
- Z as isIntegerLikeKey,
28
- P as isIterable,
29
- T as isMap,
30
- V as isNegativeNumber,
31
- v as isNonPrimitive,
32
- K as isNull,
33
- j as isNumber,
34
- I as isObject,
35
- M as isObjectAny,
36
- _ as isPhpArrayKey,
37
- O as isPhpFalsy,
38
- d as isPhpNumeric,
39
- k as isPositiveNumber,
40
- L as isPrimitive,
41
- U as isPrototypeObject,
42
- q as isSet,
43
- w as isString,
44
- z as isStringable,
45
- D as isSymbol,
46
- E as isTruthy,
47
- J as isTruthyObject,
48
- W as isUndefined,
49
- $ as isUnsafeKey,
50
- B as isWeakMap,
51
- C as isWeakSet,
52
- c as looseEqual,
53
- me as lowerFirst,
54
- o as normalizeToArray,
55
- R as objectToString,
56
- te as phpTypeName,
57
- G as phpValueMatch,
58
- H as phpValueMatcher,
59
- ee as reindexIntegerKeys,
60
- ae as resolveDefault,
61
- ie as resolveSliceRange,
62
- u as strictEqual,
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,
63
73
  l as toArrayable,
64
- p as toJsonSerializable,
65
- n as toJsonable,
66
- ce as toLower,
67
- ue as toUpper,
68
- oe as typeOf,
69
- be as upperFirst
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
70
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
  */
@@ -32,6 +37,36 @@ export declare function isPhpArrayKey(value: unknown): value is string | number;
32
37
  * @returns True if `key` is a canonical non-negative integer string
33
38
  */
34
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][];
35
70
  /**
36
71
  * Renumber the integer-like keys in `entries` to a fresh 0-based sequence, in
37
72
  * the order they appear; string keys pass through unchanged.
@@ -45,6 +80,14 @@ export declare function isIntegerLikeKey(key: string): boolean;
45
80
  * @returns The same entries with integer-like keys renumbered from 0
46
81
  */
47
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][];
48
91
  /**
49
92
  * Define an own enumerable property on the target without going through a
50
93
  * setter, so a key such as `__proto__` becomes a real own key rather than
@@ -1 +1 @@
1
- {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;A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AED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAkCzD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAG,OAAO,EACzC,IAAI,EAAE,MAAM,GACb,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAkBpB;AAYD;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EACrC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAC5B,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAUpB;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EACzC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAC5B,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAQpB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAC5B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,GAAG,EAAE,WAAW,EAChB,KAAK,EAAE,MAAM,GACd,IAAI,CAsBN"}
package/dist/keys.js CHANGED
@@ -1,28 +1,66 @@
1
- import { isPrototypeObject as o, isString as u, isInteger as f } from "./guards.js";
2
- const i = 2 ** 63;
3
- function p(r) {
1
+ import { isPrototypeObject as c, isString as s, isInteger as b, isMap as p, isNull as N, isUndefined as g, isBoolean as m, isNumber as o, isTruthyObject as a, isFunction as I, isSymbol as S } from "./guards.js";
2
+ const u = 2 ** 63;
3
+ function P(r) {
4
4
  return !isNaN(Number(r)) && !isNaN(parseFloat(String(r))) ? Number(r) : r;
5
5
  }
6
- function b(r) {
6
+ function h(r) {
7
7
  return r === "__proto__" || r === "constructor" || r === "prototype";
8
8
  }
9
- function N(r) {
10
- return u(r) ? !0 : f(r) ? r >= -i && r < i : !1;
9
+ function O(r) {
10
+ return s(r) ? !0 : b(r) ? r >= -u && r < u : !1;
11
11
  }
12
- function s(r) {
12
+ function K(r) {
13
13
  return /^(0|[1-9]\d*)$/.test(r);
14
14
  }
15
- function g(r) {
16
- let e = 0;
17
- return r.map(([t, n]) => s(t) ? [String(e++), n] : [t, n]);
15
+ function f(r) {
16
+ if (s(r)) {
17
+ if (/^(0|-?[1-9]\d*)$/.test(r)) {
18
+ const n = Number(r);
19
+ if (Number.isSafeInteger(n))
20
+ return n;
21
+ }
22
+ return r;
23
+ }
24
+ if (N(r) || g(r))
25
+ return "";
26
+ if (m(r))
27
+ return r ? 1 : 0;
28
+ if (o(r) || Number.isNaN(r)) {
29
+ const n = Number.isFinite(r) ? BigInt.asIntN(64, BigInt(Math.trunc(r))) : 0n;
30
+ return Number.isSafeInteger(Number(n)) ? Number(n) : String(n);
31
+ }
32
+ return String(r);
33
+ }
34
+ function x(r) {
35
+ if (!p(r))
36
+ return Object.entries(r);
37
+ const n = /* @__PURE__ */ new Map();
38
+ for (const [t, e] of r) {
39
+ const i = d(t) ? String(f(t)) : void 0;
40
+ n.set(i ?? t, [i ?? String(t), e]);
41
+ }
42
+ return [...n.values()];
43
+ }
44
+ function d(r) {
45
+ return !a(r) && !I(r) && !S(r);
46
+ }
47
+ function _(r) {
48
+ let n = 0;
49
+ return r.map(([t, e]) => K(t) ? [String(n++), e] : [t, e]);
50
+ }
51
+ function j(r) {
52
+ let n = 0;
53
+ return r.map(
54
+ ([t, e]) => o(f(t)) ? [String(n++), e] : [t, e]
55
+ );
18
56
  }
19
- function m(r, e, t) {
20
- if (!o(r)) {
21
- if (Object.getOwnPropertyDescriptor(r, e)?.configurable === !1) {
22
- r[e] = t;
57
+ function B(r, n, t) {
58
+ if (!c(r)) {
59
+ if (Object.getOwnPropertyDescriptor(r, n)?.configurable === !1) {
60
+ r[n] = t;
23
61
  return;
24
62
  }
25
- Object.defineProperty(r, e, {
63
+ Object.defineProperty(r, n, {
26
64
  value: t,
27
65
  enumerable: !0,
28
66
  writable: !0,
@@ -31,10 +69,13 @@ function m(r, e, t) {
31
69
  }
32
70
  }
33
71
  export {
34
- m as defineKey,
35
- p as entriesKeyValue,
36
- s as isIntegerLikeKey,
37
- N as isPhpArrayKey,
38
- b as isUnsafeKey,
39
- g as reindexIntegerKeys
72
+ B as defineKey,
73
+ P as entriesKeyValue,
74
+ K as isIntegerLikeKey,
75
+ O as isPhpArrayKey,
76
+ h as isUnsafeKey,
77
+ x as keyedEntries,
78
+ f as phpArrayKey,
79
+ _ as reindexIntegerKeys,
80
+ j as renumberPhpIntegerKeys
40
81
  };
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './cast';
2
2
  export * from './equality';
3
+ export * from './exceptions';
3
4
  export * from './guards';
4
5
  export * from './keys';
5
6
  export * from './range';
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}