@tolki/utils 1.4.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/keys.js CHANGED
@@ -1,67 +1,89 @@
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";
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";
2
3
  const u = 2 ** 63;
3
- function P(r) {
4
+ function T(r) {
4
5
  return !isNaN(Number(r)) && !isNaN(parseFloat(String(r))) ? Number(r) : r;
5
6
  }
6
- function h(r) {
7
+ function A(r) {
7
8
  return r === "__proto__" || r === "constructor" || r === "prototype";
8
9
  }
9
- function O(r) {
10
- return s(r) ? !0 : b(r) ? r >= -u && r < u : !1;
10
+ function B(r) {
11
+ return c(r) ? !0 : g(r) ? r >= -u && r < u : !1;
11
12
  }
12
- function K(r) {
13
+ function O(r) {
13
14
  return /^(0|[1-9]\d*)$/.test(r);
14
15
  }
15
- function f(r) {
16
- if (s(r)) {
16
+ function o(r) {
17
+ if (c(r)) {
17
18
  if (/^(0|-?[1-9]\d*)$/.test(r)) {
18
- const n = Number(r);
19
- if (Number.isSafeInteger(n))
20
- return n;
19
+ const t = Number(r);
20
+ if (Number.isSafeInteger(t))
21
+ return t;
21
22
  }
22
23
  return r;
23
24
  }
24
- if (N(r) || g(r))
25
+ if (N(r) || l(r))
25
26
  return "";
26
- if (m(r))
27
+ if (y(r))
27
28
  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);
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);
31
32
  }
32
33
  return String(r);
33
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
+ }
34
50
  function x(r) {
35
- if (!p(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))
36
58
  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]);
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]);
41
63
  }
42
- return [...n.values()];
43
- }
44
- function d(r) {
45
- return !a(r) && !I(r) && !S(r);
64
+ return [...t.values()];
46
65
  }
47
66
  function _(r) {
48
- let n = 0;
49
- return r.map(([t, e]) => K(t) ? [String(n++), e] : [t, e]);
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]);
50
72
  }
51
- function j(r) {
52
- let n = 0;
73
+ function M(r) {
74
+ let t = 0;
53
75
  return r.map(
54
- ([t, e]) => o(f(t)) ? [String(n++), e] : [t, e]
76
+ ([n, i]) => a(o(n)) ? [String(t++), i] : [n, i]
55
77
  );
56
78
  }
57
- function B(r, n, t) {
58
- if (!c(r)) {
59
- if (Object.getOwnPropertyDescriptor(r, n)?.configurable === !1) {
60
- r[n] = t;
79
+ function U(r, t, n) {
80
+ if (!p(r)) {
81
+ if (Object.getOwnPropertyDescriptor(r, t)?.configurable === !1) {
82
+ r[t] = n;
61
83
  return;
62
84
  }
63
- Object.defineProperty(r, n, {
64
- value: t,
85
+ Object.defineProperty(r, t, {
86
+ value: n,
65
87
  enumerable: !0,
66
88
  writable: !0,
67
89
  configurable: !0
@@ -69,13 +91,17 @@ function B(r, n, t) {
69
91
  }
70
92
  }
71
93
  export {
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
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
81
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"}
package/dist/reflect.js CHANGED
@@ -1,15 +1,25 @@
1
- import { isNull as n, isNumber as t, isInteger as e, isArray as i, isFunction as f, isUndefined as o } from "./guards.js";
2
- function p(r) {
3
- return n(r) ? "object" : i(r) ? "array" : typeof r;
1
+ import { isNull as i, isUndefined as f, isPhpInt as s, isString as u, isBoolean as c, isArray as e, isPlainObject as p, isFunction as n, isObject as b, isNumber as m, isInteger as y } from "./guards.js";
2
+ function o(r) {
3
+ return i(r) ? "object" : e(r) ? "array" : typeof r;
4
4
  }
5
- function s(r) {
5
+ function j(r) {
6
6
  return r === void 0 ? null : typeof r == "function" ? r() : r;
7
7
  }
8
- function y(r) {
9
- return n(r) ? "NULL" : t(r) ? e(r) ? "integer" : "double" : i(r) ? "array" : Number.isNaN(r) ? "double" : f(r) ? "object" : o(r) ? "NULL" : typeof r;
8
+ function O(r) {
9
+ return i(r) ? "NULL" : m(r) ? y(r) ? "integer" : "double" : e(r) ? "array" : Number.isNaN(r) ? "double" : n(r) ? "object" : f(r) ? "NULL" : typeof r;
10
+ }
11
+ function L(r) {
12
+ return i(r) || f(r) ? "null" : o(r) === "number" ? s(r) ? "int" : "float" : u(r) ? "string" : c(r) ? "bool" : e(r) || p(r) ? "array" : n(r) ? "Closure" : b(r) && n(r.constructor) ? N(r.constructor) : o(r);
13
+ }
14
+ function N(r) {
15
+ if (r.name)
16
+ return r.name;
17
+ const t = Object.getPrototypeOf(r);
18
+ return `${n(t) && t.name ? t.name : "class"}@anonymous`;
10
19
  }
11
20
  export {
12
- y as phpTypeName,
13
- s as resolveDefault,
14
- p as typeOf
21
+ L as phpDebugType,
22
+ O as phpTypeName,
23
+ j as resolveDefault,
24
+ o as typeOf
15
25
  };
package/dist/sorting.d.ts CHANGED
@@ -11,10 +11,24 @@ export type SortValueResolver = (item: unknown, key: PathKey) => unknown;
11
11
  * Build the sort-descriptor comparator factory for one path resolver.
12
12
  *
13
13
  * `@tolki/utils` sits below every path package, so the caller supplies the
14
- * resolver: `getNestedValue` for `Arr`/`Obj`, `dataGet` for `Collection`.
14
+ * resolver: `getNestedValue` for `Arr`/`Obj`, a `data_get` reader for `Collection`.
15
15
  *
16
16
  * @param resolve - Reads a descriptor's key off an item.
17
17
  * @returns A function building the comparator one sort descriptor implies.
18
18
  */
19
- export declare function createSortSpecComparator(resolve: SortValueResolver): <TValue>(spec: SortSpec<TValue>, forceDescending: boolean) => (a: TValue, b: TValue) => number;
19
+ export declare function createSortSpecComparator(resolve: SortValueResolver): <TValue>(spec: SortSpec<TValue>, forceDescending: boolean) => (a: TValue, b: TValue) => number | boolean;
20
+ /**
21
+ * Read a comparator's answers as PHP's `usort()`, `uasort()` and `uksort()` read them, for `Array.prototype.sort`.
22
+ *
23
+ * A number is cast to an int, so a fraction below 1 ties. A bool is deprecated in PHP 8 but still sorts: `true` is 1,
24
+ * and `false` asks again with the operands swapped, where `true` is -1.
25
+ *
26
+ * @param compare - A comparator of two items, answering a number or a bool
27
+ * @returns A comparator answering -1, 0 or 1
28
+ *
29
+ * @example
30
+ * [3, 1, 2].sort(phpSortComparator((a, b) => a > b)); -> [1, 2, 3]
31
+ * [3, 1, 2].sort(phpSortComparator((a, b) => (a - b) / 10)); -> [3, 1, 2]
32
+ */
33
+ export declare function phpSortComparator<TValue>(compare: (a: TValue, b: TValue) => unknown): (a: TValue, b: TValue) => number;
20
34
  //# sourceMappingURL=sorting.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sorting.d.ts","sourceRoot":"","sources":["../src/sorting.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAKtD;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;AAEzE;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,iBAAiB,IAC5B,MAAM,EACrC,MAAM,QAAQ,CAAC,MAAM,CAAC,EACtB,iBAAiB,OAAO,KACzB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAkCtC"}
1
+ {"version":3,"file":"sorting.d.ts","sourceRoot":"","sources":["../src/sorting.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAYtD;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;AAEzE;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,iBAAiB,IAC5B,MAAM,EACrC,MAAM,QAAQ,CAAC,MAAM,CAAC,EACtB,iBAAiB,OAAO,KACzB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAkChD;AAcD;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EACpC,OAAO,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO,GAC3C,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAUlC"}
package/dist/sorting.js CHANGED
@@ -1,18 +1,32 @@
1
- import { compareValues as f } from "./equality.js";
2
- import { isFunction as e, isArray as a, isUndefined as g } from "./guards.js";
3
- function y(o) {
4
- return function(r, c) {
5
- if (e(r))
6
- return r;
7
- if (a(r) && e(r[0]))
8
- return r[0];
9
- const [t, n] = a(r) ? r : [r, void 0], u = g(n) || n === !0 || n === "asc" || n === "Ascending", m = c || !u;
10
- return (s, d) => {
11
- const i = f(o(s, t), o(d, t));
12
- return m ? -i : i;
1
+ import { phpIntCast as g } from "./cast.js";
2
+ import { compareValues as C } from "./equality.js";
3
+ import { isArray as a, isFunction as S, isUndefined as h, isBoolean as A, isNumber as l } from "./guards.js";
4
+ function B(r) {
5
+ return function(n, t) {
6
+ if (c(n))
7
+ return n;
8
+ if (a(n) && c(n[0]))
9
+ return n[0];
10
+ const [u, o] = a(n) ? n : [n, void 0], m = h(o) || o === !0 || o === "asc" || o === "Ascending", s = t || !m;
11
+ return (p, d) => {
12
+ const e = C(r(p, u), r(d, u));
13
+ return s ? -e : e;
13
14
  };
14
15
  };
15
16
  }
17
+ function c(r) {
18
+ return S(r);
19
+ }
20
+ function D(r) {
21
+ return (i, n) => {
22
+ const t = r(i, n);
23
+ return t === !1 ? -f(r(n, i)) : f(t);
24
+ };
25
+ }
26
+ function f(r) {
27
+ return A(r) ? r ? 1 : 0 : l(r) ? Math.sign(g(r)) : 0;
28
+ }
16
29
  export {
17
- y as createSortSpecComparator
30
+ B as createSortSpecComparator,
31
+ D as phpSortComparator
18
32
  };