@tolki/utils 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,70 +1,98 @@
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 t, arrayableValues as i, castableToArray as s, cssListItemToString as a, getAccessibleValues as o, normalizeToArray as p, phpIntArgument as n, phpIntCast as l, phpStringCast as y, toArrayable as m, toJsonSerializable as u, toJsonable as c, toPhpKeyString as b } from "./cast.js";
2
+ import { compareValues as g, looseEqual as S, operatorMatch as f, strictEqual as x } from "./equality.js";
3
+ import { InvalidArgumentException as P, ItemNotFoundException as A, MultipleItemsFoundException as d, UnexpectedValueException as v } from "./exceptions.js";
4
+ import { isAccessibleData as E, isArray as N, isBoolean as T, isEnumCase as F, isFalsy as O, isFiniteNumber as V, isFloat as C, isFunction as M, isInteger as j, isIterable as k, isMap as L, isNegativeNumber as U, isNonPrimitive as w, isNull as z, isNumber as D, isObject as R, isObjectAny as q, isPhpAccessible as J, isPhpFalsy as W, isPhpInt as B, isPhpNumeric as G, isPlainObject as H, isPositiveNumber as Q, isPrimitive as X, isPrototypeObject as Y, isSet as Z, isString as _, isStringable as $, isSymbol as ee, isTruthy as re, isTruthyObject as te, isUndefined as ie, isWeakMap as se, isWeakSet as ae, objectToString as oe, phpValueMatch as pe, phpValueMatcher as ne } from "./guards.js";
5
+ import { arrayKeyExistsError as ye, defineKey as me, entriesKeyValue as ue, hasOwnToString as ce, isIllegalOffset as be, isIntegerLikeKey as he, isPhpArrayKey as ge, isUnsafeKey as Se, keyedEntries as fe, phpArrayKey as xe, phpComputedKey as Ie, reindexIntegerKeys as Pe, renumberPhpIntegerKeys as Ae } from "./keys.js";
6
+ import { resolvePadLength as ve, resolveRangeSize as Ke, resolveSliceRange as Ee, resolveSpliceRange as Ne, resolveTakeCount as Te } from "./range.js";
7
+ import { phpDebugType as Oe, phpTypeName as Ve, resolveDefault as Ce, typeOf as Me } from "./reflect.js";
8
+ import { createSortSpecComparator as ke, phpSortComparator as Le } from "./sorting.js";
9
+ import { arrayValueMessage as we, lowerFirst as ze, toLower as De, toUpper as Re, upperFirst as qe } from "./string.js";
9
10
  export {
10
- ye as arrayValueMessage,
11
+ P as InvalidArgumentException,
12
+ A as ItemNotFoundException,
13
+ d as MultipleItemsFoundException,
14
+ v as UnexpectedValueException,
15
+ ye as arrayKeyExistsError,
16
+ we as arrayValueMessage,
17
+ t as arrayableItems,
11
18
  i as arrayableValues,
12
19
  s as castableToArray,
13
- m as compareValues,
14
- pe as createSortSpecComparator,
15
- t as cssListItemToString,
16
- X as defineKey,
17
- Y as entriesKeyValue,
18
- 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,
63
- 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
20
+ g as compareValues,
21
+ ke as createSortSpecComparator,
22
+ a as cssListItemToString,
23
+ me as defineKey,
24
+ ue as entriesKeyValue,
25
+ o as getAccessibleValues,
26
+ ce as hasOwnToString,
27
+ E as isAccessibleData,
28
+ N as isArray,
29
+ T as isBoolean,
30
+ F as isEnumCase,
31
+ O as isFalsy,
32
+ V as isFiniteNumber,
33
+ C as isFloat,
34
+ M as isFunction,
35
+ be as isIllegalOffset,
36
+ j as isInteger,
37
+ he as isIntegerLikeKey,
38
+ k as isIterable,
39
+ L as isMap,
40
+ U as isNegativeNumber,
41
+ w as isNonPrimitive,
42
+ z as isNull,
43
+ D as isNumber,
44
+ R as isObject,
45
+ q as isObjectAny,
46
+ J as isPhpAccessible,
47
+ ge as isPhpArrayKey,
48
+ W as isPhpFalsy,
49
+ B as isPhpInt,
50
+ G as isPhpNumeric,
51
+ H as isPlainObject,
52
+ Q as isPositiveNumber,
53
+ X as isPrimitive,
54
+ Y as isPrototypeObject,
55
+ Z as isSet,
56
+ _ as isString,
57
+ $ as isStringable,
58
+ ee as isSymbol,
59
+ re as isTruthy,
60
+ te as isTruthyObject,
61
+ ie as isUndefined,
62
+ Se as isUnsafeKey,
63
+ se as isWeakMap,
64
+ ae as isWeakSet,
65
+ fe as keyedEntries,
66
+ S as looseEqual,
67
+ ze as lowerFirst,
68
+ p as normalizeToArray,
69
+ oe as objectToString,
70
+ f as operatorMatch,
71
+ xe as phpArrayKey,
72
+ Ie as phpComputedKey,
73
+ Oe as phpDebugType,
74
+ n as phpIntArgument,
75
+ l as phpIntCast,
76
+ Le as phpSortComparator,
77
+ y as phpStringCast,
78
+ Ve as phpTypeName,
79
+ pe as phpValueMatch,
80
+ ne as phpValueMatcher,
81
+ Pe as reindexIntegerKeys,
82
+ Ae as renumberPhpIntegerKeys,
83
+ Ce as resolveDefault,
84
+ ve as resolvePadLength,
85
+ Ke as resolveRangeSize,
86
+ Ee as resolveSliceRange,
87
+ Ne as resolveSpliceRange,
88
+ Te as resolveTakeCount,
89
+ x as strictEqual,
90
+ m as toArrayable,
91
+ u as toJsonSerializable,
92
+ c as toJsonable,
93
+ De as toLower,
94
+ b as toPhpKeyString,
95
+ Re as toUpper,
96
+ Me as typeOf,
97
+ qe as upperFirst
70
98
  };
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,86 @@ 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
+ * The key a Laravel method stores for a value it computed, cast the way that method casts it.
57
+ *
58
+ * @param value - The computed key
59
+ * @param options - `enumCases` reads an `@tolki/enum` case as its value, as `enum_value()` does; `stringables`
60
+ * reads an object with its own `toString` as that string; `invalid` builds the error the method's PHP throws
61
+ * @returns The key PHP stores, cast as {@linkcode phpArrayKey} casts it
62
+ * @throws TypeError `Cannot access offset of type X on array` for any other object, or the error `invalid` builds
63
+ *
64
+ * @example
65
+ * phpComputedKey(true); -> 1
66
+ * phpComputedKey(1.5); -> 1
67
+ * phpComputedKey({ value: 2, backed: true, name: "B" }, { enumCases: true }); -> 2
68
+ * phpComputedKey([1, 2]); -> throws TypeError("Cannot access offset of type array on array")
69
+ */
70
+ export declare function phpComputedKey(value: unknown, options?: {
71
+ enumCases?: boolean;
72
+ stringables?: boolean;
73
+ invalid?: (type: string) => Error;
74
+ }): string | number;
75
+ /**
76
+ * Determine whether a value is a key no PHP array can hold, which PHP throws for wherever it reads or writes one.
77
+ *
78
+ * @param value - The key to test
79
+ * @returns True for an array, an object or a function, which PHP reads as a Closure
80
+ *
81
+ * @example
82
+ * isIllegalOffset(["b"]); -> true
83
+ * isIllegalOffset(null); -> false
84
+ */
85
+ export declare function isIllegalOffset(value: unknown): value is object;
86
+ /**
87
+ * The error `array_key_exists` throws for a key no PHP array can hold, called from a namespace as Laravel calls it.
88
+ *
89
+ * @returns The TypeError PHP throws
90
+ */
91
+ export declare function arrayKeyExistsError(): TypeError;
92
+ /**
93
+ * Determine whether a value is an object with its own `toString`, the JS reading of a PHP Stringable.
94
+ *
95
+ * @param value - The value to test
96
+ * @returns True unless the value is no object, inherits `Object.prototype`'s `toString`, or is a Date
97
+ *
98
+ * @example
99
+ * hasOwnToString(new (class { toString() { return "a"; } })()); -> true
100
+ * hasOwnToString(new Date(0)); -> false
101
+ */
102
+ export declare function hasOwnToString(value: unknown): value is {
103
+ toString(): unknown;
104
+ };
105
+ /**
106
+ * Get the entries of a plain object or a Map, each keyed by the string PHP stores for its key.
107
+ *
108
+ * A Map keeps its insertion order, which a plain object cannot hold for integer keys. Keys PHP
109
+ * stores as one (`1` and `"1"`) fold into the first one's place with the last one's value.
110
+ *
111
+ * @param data - The plain object or Map to read.
112
+ * @returns The `[key, value]` pairs in iteration order.
113
+ *
114
+ * @example
115
+ * keyedEntries({ b: 1, a: 2 }); -> [["b", 1], ["a", 2]]
116
+ * keyedEntries(new Map([[2, "c"], [0, "a"]])); -> [["2", "c"], ["0", "a"]]
117
+ * keyedEntries(new Map([[1, "a"], ["1", "b"]])); -> [["1", "b"]]
118
+ */
119
+ export declare function keyedEntries<TValue = unknown>(data: object): [string, TValue][];
35
120
  /**
36
121
  * Renumber the integer-like keys in `entries` to a fresh 0-based sequence, in
37
122
  * the order they appear; string keys pass through unchanged.
@@ -45,6 +130,14 @@ export declare function isIntegerLikeKey(key: string): boolean;
45
130
  * @returns The same entries with integer-like keys renumbered from 0
46
131
  */
47
132
  export declare function reindexIntegerKeys<TValue>(entries: [string, TValue][]): [string, TValue][];
133
+ /**
134
+ * Renumber every key PHP stores as an integer to a fresh 0-based sequence, in order, as `array_shift`,
135
+ * `array_splice` and `array_unshift` do. Unlike `reindexIntegerKeys`, a negative key such as "-1" counts too.
136
+ *
137
+ * @param entries - The entries to renumber, in their intended order
138
+ * @returns The same entries with every integer key renumbered from 0
139
+ */
140
+ export declare function renumberPhpIntegerKeys<TValue>(entries: [string, TValue][]): [string, TValue][];
48
141
  /**
49
142
  * Define an own enumerable property on the target without going through a
50
143
  * 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;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;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EACrC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAC5B,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAUpB;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"}
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AA0BA;;;;;;;;;;GAUG;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;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAkCzD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC1B,KAAK,EAAE,OAAO,EACd,OAAO,GAAE;IACL,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,KAAK,CAAC;CAChC,GACP,MAAM,GAAG,MAAM,CAmBjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE/D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,SAAS,CAI/C;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC1B,KAAK,EAAE,OAAO,GACf,KAAK,IAAI;IAAE,QAAQ,IAAI,OAAO,CAAA;CAAE,CAQlC;AAYD;;;;;;;;;;;;;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,29 +1,89 @@
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 p, isObject as f, isFunction as s, isArray as b, isString as c, isInteger as g, isMap as m, isNull as N, isUndefined as l, isBoolean as y, isNumber as a, isEnumCase as S, isTruthyObject as d, isSymbol as I } from "./guards.js";
2
+ import { phpDebugType as K } from "./reflect.js";
3
+ const u = 2 ** 63;
4
+ function T(r) {
4
5
  return !isNaN(Number(r)) && !isNaN(parseFloat(String(r))) ? Number(r) : r;
5
6
  }
6
- function b(r) {
7
+ function A(r) {
7
8
  return r === "__proto__" || r === "constructor" || r === "prototype";
8
9
  }
9
- function N(r) {
10
- return u(r) ? !0 : f(r) ? r >= -i && r < i : !1;
10
+ function B(r) {
11
+ return c(r) ? !0 : g(r) ? r >= -u && r < u : !1;
11
12
  }
12
- function s(r) {
13
+ function O(r) {
13
14
  return /^(0|[1-9]\d*)$/.test(r);
14
15
  }
15
- function g(r) {
16
- let e = 0;
17
- return r.map(([t, n]) => s(t) ? [String(e++), n] : [t, n]);
16
+ function o(r) {
17
+ if (c(r)) {
18
+ if (/^(0|-?[1-9]\d*)$/.test(r)) {
19
+ const t = Number(r);
20
+ if (Number.isSafeInteger(t))
21
+ return t;
22
+ }
23
+ return r;
24
+ }
25
+ if (N(r) || l(r))
26
+ return "";
27
+ if (y(r))
28
+ return r ? 1 : 0;
29
+ if (a(r) || Number.isNaN(r)) {
30
+ const t = Number.isFinite(r) ? BigInt.asIntN(64, BigInt(Math.trunc(r))) : 0n;
31
+ return Number.isSafeInteger(Number(t)) ? Number(t) : String(t);
32
+ }
33
+ return String(r);
34
+ }
35
+ function C(r, t = {}) {
36
+ const { enumCases: n = !1, stringables: i = !1 } = t;
37
+ let e = r;
38
+ if (n && S(e) && (e = e.value), i && x(e) && (e = String(e.toString())), h(e))
39
+ throw (t.invalid ?? P)(K(e));
40
+ return o(e);
41
+ }
42
+ function h(r) {
43
+ return b(r) || f(r) || s(r);
44
+ }
45
+ function D() {
46
+ return new TypeError(
47
+ "array_key_exists(): Argument #1 ($key) must be a valid array offset type"
48
+ );
49
+ }
50
+ function x(r) {
51
+ return f(r) && !(r instanceof Date) && s(r.toString) && r.toString !== Object.prototype.toString;
52
+ }
53
+ function P(r) {
54
+ return new TypeError(`Cannot access offset of type ${r} on array`);
55
+ }
56
+ function $(r) {
57
+ if (!m(r))
58
+ return Object.entries(r);
59
+ const t = /* @__PURE__ */ new Map();
60
+ for (const [n, i] of r) {
61
+ const e = _(n) ? String(o(n)) : void 0;
62
+ t.set(e ?? n, [e ?? String(n), i]);
63
+ }
64
+ return [...t.values()];
65
+ }
66
+ function _(r) {
67
+ return !d(r) && !s(r) && !I(r);
68
+ }
69
+ function F(r) {
70
+ let t = 0;
71
+ return r.map(([n, i]) => O(n) ? [String(t++), i] : [n, i]);
72
+ }
73
+ function M(r) {
74
+ let t = 0;
75
+ return r.map(
76
+ ([n, i]) => a(o(n)) ? [String(t++), i] : [n, i]
77
+ );
18
78
  }
19
- function m(r, e, t) {
20
- if (!o(r)) {
21
- if (Object.getOwnPropertyDescriptor(r, e)?.configurable === !1) {
22
- r[e] = t;
79
+ function U(r, t, n) {
80
+ if (!p(r)) {
81
+ if (Object.getOwnPropertyDescriptor(r, t)?.configurable === !1) {
82
+ r[t] = n;
23
83
  return;
24
84
  }
25
- Object.defineProperty(r, e, {
26
- value: t,
85
+ Object.defineProperty(r, t, {
86
+ value: n,
27
87
  enumerable: !0,
28
88
  writable: !0,
29
89
  configurable: !0
@@ -31,10 +91,17 @@ function m(r, e, t) {
31
91
  }
32
92
  }
33
93
  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
94
+ D as arrayKeyExistsError,
95
+ U as defineKey,
96
+ T as entriesKeyValue,
97
+ x as hasOwnToString,
98
+ h as isIllegalOffset,
99
+ O as isIntegerLikeKey,
100
+ B as isPhpArrayKey,
101
+ A as isUnsafeKey,
102
+ $ as keyedEntries,
103
+ o as phpArrayKey,
104
+ C as phpComputedKey,
105
+ F as reindexIntegerKeys,
106
+ M as renumberPhpIntegerKeys
40
107
  };
package/dist/range.d.ts CHANGED
@@ -6,6 +6,13 @@ export type SliceRange = {
6
6
  start: number;
7
7
  end: number | undefined;
8
8
  };
9
+ /**
10
+ * The index `array_splice($items, $offset, $length)` starts at and the number of items it removes.
11
+ */
12
+ export type SpliceRange = {
13
+ start: number;
14
+ count: number;
15
+ };
9
16
  /**
10
17
  * Resolve `array_slice`'s offset/length pair into a slice window.
11
18
  *
@@ -20,4 +27,67 @@ export type SliceRange = {
20
27
  * @returns The `start` and `end` bounds to slice with.
21
28
  */
22
29
  export declare function resolveSliceRange(count: number, offset: number, length: number | null): SliceRange;
30
+ /**
31
+ * Resolve `array_splice`'s offset/length pair into where it starts and how many items it removes.
32
+ *
33
+ * The offset and the length are read as PHP reads the int parameters, dropping a fraction first, so a
34
+ * negative offset counts back from the end before it is combined with the length, as `array_splice` does.
35
+ *
36
+ * @param size - The number of items being spliced.
37
+ * @param offset - The starting index, negative to count back from the end.
38
+ * @param length - How many items to remove, negative to stop that many from the end, or `null` or `undefined` to
39
+ * run to the end, as PHP's `?int` length reads null.
40
+ * @returns The `start` index and the `count` of items to remove.
41
+ * @throws TypeError when the offset or the length is NAN, infinite or outside PHP's int range, which array_splice()
42
+ * refuses.
43
+ */
44
+ export declare function resolveSpliceRange(size: number, offset: number, length: number | null | undefined): SpliceRange;
45
+ /**
46
+ * Resolve how many items Laravel's `shift()` and `pop()` take: one per item of `range(1, min($count, $size))`.
47
+ *
48
+ * PHP's `min()` answers `$size` over a `NAN` count, so `NAN` takes every item, and a fraction is dropped.
49
+ *
50
+ * @param count - How many items the caller asked for, above 0
51
+ * @param size - How many items there are
52
+ * @returns The number of items to take
53
+ * @throws Error for a fractional count below 2 that the items do not cap, as PHP's range() throws its ValueError
54
+ *
55
+ * @example
56
+ * resolveTakeCount(2.5, 4); -> 2
57
+ * resolveTakeCount(NaN, 4); -> 4
58
+ * resolveTakeCount(1.5, 4); -> throws Error
59
+ */
60
+ export declare function resolveTakeCount(count: number, size: number): number;
61
+ /**
62
+ * Resolve the length `array_pad()` pads to, reading it as PHP reads the int parameter.
63
+ *
64
+ * @param size - The length the caller passed, negative to pad at the beginning
65
+ * @returns The length without its fraction
66
+ * @throws TypeError when the length is NAN, infinite or outside PHP's int range
67
+ * @throws Error when the length is past PHP's maximum array size, as its ValueError
68
+ *
69
+ * @example
70
+ * resolvePadLength(7.5); -> 7
71
+ * resolvePadLength(NaN); -> throws TypeError
72
+ */
73
+ export declare function resolvePadLength(size: number): number;
74
+ /**
75
+ * Resolve how many items PHP's `range()` builds from a start to an end by a step, refusing what `range()` refuses.
76
+ *
77
+ * A range whose bounds and step PHP holds as ints counts whole steps. Any other range rounds its size half up, and
78
+ * `range()` then drops an item that rounding puts past the end.
79
+ *
80
+ * @param start - The first value
81
+ * @param end - The value to stop at, counted down to when it is below the start
82
+ * @param step - The distance between two values, negative only on a decreasing range
83
+ * @returns The number of items `range()` makes room for
84
+ * @throws Error when an argument is not a finite number, the step is 0, negative on an increasing range or longer
85
+ * than the range, or when the range would hold 2^30 items or more, each with the message of PHP's ValueError
86
+ *
87
+ * @example
88
+ * resolveRangeSize(1, 5, 1); -> 5
89
+ * resolveRangeSize(0, 1, 0.25); -> 5
90
+ * resolveRangeSize(1, 1e19, 1); -> throws Error
91
+ */
92
+ export declare function resolveRangeSize(start: number, end: number, step: number): number;
23
93
  //# sourceMappingURL=range.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../src/range.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEpE;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GAAG,IAAI,GACtB,UAAU,CASZ"}
1
+ {"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../src/range.ts"],"names":[],"mappings":"AAYA;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEpE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3D;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GAAG,IAAI,GACtB,UAAU,CASZ;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAC9B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAClC,WAAW,CAoBb;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAWpE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAarD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC5B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GACb,MAAM,CAgDR"}
package/dist/range.js CHANGED
@@ -1,8 +1,106 @@
1
- import { isNull as t } from "./guards.js";
2
- function n(o, a, r) {
3
- const i = a < 0 ? Math.max(o + a, 0) : a, m = t(r) ? void 0 : r >= 0 ? i + r : Math.max(i, o + r);
4
- return { start: i, end: m };
1
+ import { phpIntArgument as f } from "./cast.js";
2
+ import { isFiniteNumber as u, isNull as h, isUndefined as l, isInteger as p, isPhpInt as d } from "./guards.js";
3
+ const s = 2 ** 30;
4
+ function x(e, n, t) {
5
+ const r = n < 0 ? Math.max(e + n, 0) : n, a = h(t) ? void 0 : t >= 0 ? r + t : Math.max(r, e + t);
6
+ return { start: r, end: a };
7
+ }
8
+ function y(e, n, t) {
9
+ const r = f(
10
+ n,
11
+ "array_splice(): Argument #2 ($offset) must be of type int, float given"
12
+ ), a = r < 0 ? Math.max(e + r, 0) : Math.min(r, e);
13
+ if (h(t) || l(t))
14
+ return { start: a, count: e - a };
15
+ const i = f(
16
+ t,
17
+ "array_splice(): Argument #3 ($length) must be of type ?int, float given"
18
+ );
19
+ return {
20
+ start: a,
21
+ count: i < 0 ? Math.max(e + i - a, 0) : i
22
+ };
23
+ }
24
+ function A(e, n) {
25
+ const t = Number.isNaN(e) ? n : Math.min(e, n);
26
+ if (!p(t) && t < 2)
27
+ throw new Error(
28
+ "range(): Argument #3 ($step) must be less than the range spanned by argument #1 ($start) and argument #2 ($end)"
29
+ );
30
+ return Math.floor(t);
31
+ }
32
+ function v(e) {
33
+ const n = f(
34
+ e,
35
+ "array_pad(): Argument #2 ($length) must be of type int, float given"
36
+ );
37
+ if (Math.abs(n) > s)
38
+ throw new Error(
39
+ "array_pad(): Argument #2 ($length) must not exceed the maximum allowed array size"
40
+ );
41
+ return n;
42
+ }
43
+ function I(e, n, t) {
44
+ if (!u(t))
45
+ throw g("#3 ($step)", t);
46
+ if (t === 0)
47
+ throw new Error("range(): Argument #3 ($step) cannot be 0");
48
+ if (!u(e))
49
+ throw g("#1 ($start)", e);
50
+ if (!u(n))
51
+ throw g("#2 ($end)", n);
52
+ if (n > e && t < 0)
53
+ throw new Error(
54
+ "range(): Argument #3 ($step) must be greater than 0 for increasing ranges"
55
+ );
56
+ const r = Math.abs(t), a = Math.abs(n - e);
57
+ if (a !== 0 && a < r)
58
+ throw new Error(
59
+ "range(): Argument #3 ($step) must be less than the range spanned by argument #1 ($start) and argument #2 ($end)"
60
+ );
61
+ const i = a / r + 1, o = ![e, n, t].every(($) => d($));
62
+ if (i >= s)
63
+ throw b(
64
+ Math.min(e, n),
65
+ Math.max(e, n),
66
+ r,
67
+ o
68
+ );
69
+ const c = Math.floor(i);
70
+ return o && i - c >= 0.5 ? c + 1 : c;
71
+ }
72
+ function g(e, n) {
73
+ const t = Number.isNaN(n) ? "NAN" : "INF";
74
+ return new Error(
75
+ `range(): Argument ${e} must be a finite number, ${t} provided`
76
+ );
77
+ }
78
+ function b(e, n, t, r) {
79
+ if (r) {
80
+ const i = (n - e) / t + 1;
81
+ return new Error(
82
+ `The supplied range exceeds the maximum array size by ${m(i - s)} elements: start=${m(e)}, end=${m(n)}, step=${m(t)}. Max size: ${s}`
83
+ );
84
+ }
85
+ const a = (BigInt(n) - BigInt(e)) / BigInt(t);
86
+ return new Error(
87
+ `The supplied range exceeds the maximum array size by ${a + 1n - BigInt(s)} elements: start=${e}, end=${n}, step=${t}. Calculated size: ${a}. Maximum size: ${s}.`
88
+ );
89
+ }
90
+ function m(e) {
91
+ if (!u(e))
92
+ return "inf";
93
+ let n = Math.abs(e), t = 0;
94
+ for (; !Number.isInteger(n); )
95
+ n *= 2, t++;
96
+ const r = 2n ** BigInt(t), a = BigInt(n) * 10n, i = a % r * 2n;
97
+ let o = a / r;
98
+ return (i > r || i === r && o % 2n === 1n) && (o += 1n), `${e < 0 ? "-" : ""}${o / 10n}.${o % 10n}`;
5
99
  }
6
100
  export {
7
- n as resolveSliceRange
101
+ v as resolvePadLength,
102
+ I as resolveRangeSize,
103
+ x as resolveSliceRange,
104
+ y as resolveSpliceRange,
105
+ A as resolveTakeCount
8
106
  };
package/dist/reflect.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * Get a more specific type description for debugging purposes.
3
- * Differentiates between null, arrays, and other types.
3
+ * Reports JavaScript's `typeof` name, except that an array reports as "array".
4
4
  *
5
5
  * @param {unknown} v - The value to get the type of.
6
6
  * @returns {string} A string describing the type.
7
7
  * @example
8
8
  * Get specific types
9
- * typeOf(null); -> "null"
9
+ * typeOf(null); -> "object"
10
10
  * typeOf([]); -> "array"
11
11
  * typeOf({}); -> "object"
12
12
  */
@@ -30,4 +30,18 @@ export declare function resolveDefault<D>(defaultValue?: D | (() => D)): D | nul
30
30
  * @returns The PHP type name ("NULL", "integer", "double", or the JS typeof otherwise).
31
31
  */
32
32
  export declare function phpTypeName(value: unknown): string;
33
+ /**
34
+ * Name a value's type as PHP's `get_debug_type()` does, for messages that name what they were given.
35
+ *
36
+ * @param value - The value to name
37
+ * @returns null, int, float, string, bool, array, Closure for a function, or the class's name (Parent@anonymous for an
38
+ * anonymous class extending Parent, class@anonymous for one extending none), else the JavaScript typeof name
39
+ *
40
+ * @example
41
+ * phpDebugType(1.5); -> "float"
42
+ * phpDebugType({ a: 1 }); -> "array"
43
+ * phpDebugType(() => 1); -> "Closure"
44
+ * phpDebugType(new Date()); -> "Date"
45
+ */
46
+ export declare function phpDebugType(value: unknown): string;
33
47
  //# sourceMappingURL=reflect.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reflect.d.ts","sourceRoot":"","sources":["../src/reflect.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,CASzC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAOxE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CA6BlD"}
1
+ {"version":3,"file":"reflect.d.ts","sourceRoot":"","sources":["../src/reflect.ts"],"names":[],"mappings":"AAcA;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,CASzC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAOxE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CA6BlD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAkCnD"}