@tolki/utils 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,48 +1,70 @@
1
- import { castableToArray as s, compareValues as t, entriesKeyValue as r, getAccessibleValues as a, isAccessibleData as o, isArray as l, isBoolean as b, isFalsy as n, isFiniteNumber as u, isFloat as c, isFunction as y, isInteger as m, isIterable as p, isMap as A, isNegativeNumber as N, isNonPrimitive as S, isNull as g, isNumber as F, isObject as f, isObjectAny as v, isPositiveNumber as T, isPrimitive as j, isSet as O, isString as P, isStringable as U, isSymbol as V, isTruthy as d, isTruthyObject as h, isUndefined as k, isUnsafeKey as q, isWeakMap as w, isWeakSet as z, looseEqual as D, lowerFirst as E, normalizeToArray as I, objectToString as J, resolveDefault as K, strictEqual as M, toArrayable as W, toJsonSerializable as x, toJsonable as B, toLower as L, toUpper as C, typeOf as G, upperFirst as H } from "./utils.js";
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";
2
9
  export {
10
+ ye as arrayValueMessage,
11
+ i as arrayableValues,
3
12
  s as castableToArray,
4
- t as compareValues,
5
- r as entriesKeyValue,
13
+ m as compareValues,
14
+ pe as createSortSpecComparator,
15
+ t as cssListItemToString,
16
+ X as defineKey,
17
+ Y as entriesKeyValue,
6
18
  a as getAccessibleValues,
7
- o as isAccessibleData,
8
- l as isArray,
9
- b as isBoolean,
10
- n as isFalsy,
11
- u as isFiniteNumber,
12
- c as isFloat,
13
- y as isFunction,
14
- m as isInteger,
15
- p as isIterable,
16
- A as isMap,
17
- N as isNegativeNumber,
18
- S as isNonPrimitive,
19
- g as isNull,
20
- F as isNumber,
21
- f as isObject,
22
- v as isObjectAny,
23
- T as isPositiveNumber,
24
- j as isPrimitive,
25
- O as isSet,
26
- P as isString,
27
- U as isStringable,
28
- V as isSymbol,
29
- d as isTruthy,
30
- h as isTruthyObject,
31
- k as isUndefined,
32
- q as isUnsafeKey,
33
- w as isWeakMap,
34
- z as isWeakSet,
35
- D as looseEqual,
36
- E as lowerFirst,
37
- I as normalizeToArray,
38
- J as objectToString,
39
- K as resolveDefault,
40
- M as strictEqual,
41
- W as toArrayable,
42
- x as toJsonSerializable,
43
- B as toJsonable,
44
- L as toLower,
45
- C as toUpper,
46
- G as typeOf,
47
- H as upperFirst
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
48
70
  };
package/dist/keys.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Figures out if the entry key should be a number or a string.
3
+ *
4
+ * @param value - The entry key value (number, string, or symbol)
5
+ * @returns The entry key as a number if it can be converted, otherwise returns the original value
6
+ */
7
+ export declare function entriesKeyValue<T extends PropertyKey>(value: T): T extends `${number}` ? number : T;
8
+ /**
9
+ * Check if a key is unsafe for property assignment (prototype pollution).
10
+ * Returns true for `__proto__`, `constructor`, and `prototype`.
11
+ *
12
+ * @param key - The key to check
13
+ * @returns True if the key could cause prototype pollution
14
+ */
15
+ export declare function isUnsafeKey(key: string): boolean;
16
+ /**
17
+ * Check whether a value can be used as a PHP array key. PHP accepts strings
18
+ * and integers in the inclusive range [-2^63, 2^63 - 1]; numbers outside that
19
+ * range are floats there, so they are rejected rather than producing a key PHP
20
+ * could never generate.
21
+ *
22
+ * @param value - The value to check
23
+ * @returns True if the value can be used as a PHP array key
24
+ */
25
+ export declare function isPhpArrayKey(value: unknown): value is string | number;
26
+ /**
27
+ * Whether `key` is a canonical non-negative integer string ("0", "1", "23",
28
+ * but not "01", "-1", "1.5") — the key class JS always sorts ahead of string
29
+ * keys, and PHP treats as an integer array key.
30
+ *
31
+ * @param key - The key to test
32
+ * @returns True if `key` is a canonical non-negative integer string
33
+ */
34
+ export declare function isIntegerLikeKey(key: string): boolean;
35
+ /**
36
+ * Renumber the integer-like keys in `entries` to a fresh 0-based sequence, in
37
+ * the order they appear; string keys pass through unchanged.
38
+ *
39
+ * The one integer-key policy for every reordering helper, since a plain JS
40
+ * object always re-sorts integer-like keys ascending (ECMA-262). Negative keys
41
+ * are excluded — JS doesn't re-sort those. A mixed object keeps neither order:
42
+ * integer keys hoist ahead of string keys, so `sort({x: 5, 0: 9})` gives `[9, 5]`.
43
+ *
44
+ * @param entries - The entries to renumber, in their intended order
45
+ * @returns The same entries with integer-like keys renumbered from 0
46
+ */
47
+ export declare function reindexIntegerKeys<TValue>(entries: [string, TValue][]): [string, TValue][];
48
+ /**
49
+ * Define an own enumerable property on the target without going through a
50
+ * setter, so a key such as `__proto__` becomes a real own key rather than
51
+ * reaching `Object.prototype` through the inherited setter.
52
+ *
53
+ * `defineKey` is the sanctioned way to write a computed key onto a fresh result object;
54
+ * plain `result[key] = value` lets a `"__proto__"` key reparent `result`. A key that is
55
+ * already non-configurable falls back to assignment, since it cannot be a setter.
56
+ *
57
+ * @param target - The object to define the key on
58
+ * @param key - The key to define
59
+ * @param value - The value to store under the key
60
+ */
61
+ export declare function defineKey<TValue>(target: Record<string, TValue>, key: PropertyKey, value: TValue): void;
62
+ //# sourceMappingURL=keys.d.ts.map
@@ -0,0 +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"}
package/dist/keys.js ADDED
@@ -0,0 +1,40 @@
1
+ import { isPrototypeObject as o, isString as u, isInteger as f } from "./guards.js";
2
+ const i = 2 ** 63;
3
+ function p(r) {
4
+ return !isNaN(Number(r)) && !isNaN(parseFloat(String(r))) ? Number(r) : r;
5
+ }
6
+ function b(r) {
7
+ return r === "__proto__" || r === "constructor" || r === "prototype";
8
+ }
9
+ function N(r) {
10
+ return u(r) ? !0 : f(r) ? r >= -i && r < i : !1;
11
+ }
12
+ function s(r) {
13
+ return /^(0|[1-9]\d*)$/.test(r);
14
+ }
15
+ function g(r) {
16
+ let e = 0;
17
+ return r.map(([t, n]) => s(t) ? [String(e++), n] : [t, n]);
18
+ }
19
+ function m(r, e, t) {
20
+ if (!o(r)) {
21
+ if (Object.getOwnPropertyDescriptor(r, e)?.configurable === !1) {
22
+ r[e] = t;
23
+ return;
24
+ }
25
+ Object.defineProperty(r, e, {
26
+ value: t,
27
+ enumerable: !0,
28
+ writable: !0,
29
+ configurable: !0
30
+ });
31
+ }
32
+ }
33
+ 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
40
+ };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The half-open `[start, end)` window `array_slice($items, $offset, $length)`
3
+ * selects, expressed for `Array.prototype.slice`.
4
+ */
5
+ export type SliceRange = {
6
+ start: number;
7
+ end: number | undefined;
8
+ };
9
+ /**
10
+ * Resolve `array_slice`'s offset/length pair into a slice window.
11
+ *
12
+ * A negative offset is normalised against the item count BEFORE it is
13
+ * combined with the length, matching `array_slice` — a raw negative offset
14
+ * fed straight into `Array.prototype.slice` combines the two differently.
15
+ *
16
+ * @param count - The number of items being sliced.
17
+ * @param offset - The starting index, negative to count back from the end.
18
+ * @param length - How many items to take, negative to stop that many from
19
+ * the end, or `null` to run to the end.
20
+ * @returns The `start` and `end` bounds to slice with.
21
+ */
22
+ export declare function resolveSliceRange(count: number, offset: number, length: number | null): SliceRange;
23
+ //# sourceMappingURL=range.d.ts.map
@@ -0,0 +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"}
package/dist/range.js ADDED
@@ -0,0 +1,8 @@
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 };
5
+ }
6
+ export {
7
+ n as resolveSliceRange
8
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Get a more specific type description for debugging purposes.
3
+ * Differentiates between null, arrays, and other types.
4
+ *
5
+ * @param {unknown} v - The value to get the type of.
6
+ * @returns {string} A string describing the type.
7
+ * @example
8
+ * Get specific types
9
+ * typeOf(null); -> "null"
10
+ * typeOf([]); -> "array"
11
+ * typeOf({}); -> "object"
12
+ */
13
+ export declare function typeOf(v: unknown): string;
14
+ /**
15
+ * Helper function to resolve a default value (either direct value or lazy function).
16
+ *
17
+ * @param defaultValue - The default value or lazy function
18
+ * @returns The resolved default value
19
+ *
20
+ * @example
21
+ * resolveDefault('hello'); -> 'hello'
22
+ * resolveDefault(() => 'world'); -> 'world'
23
+ * resolveDefault(undefined); -> null
24
+ */
25
+ export declare function resolveDefault<D>(defaultValue?: D | (() => D)): D | null;
26
+ /**
27
+ * Render a value's type the way PHP's gettype() does, for Arr::array()-style messages.
28
+ *
29
+ * @param value - The value whose type name is needed.
30
+ * @returns The PHP type name ("NULL", "integer", "double", or the JS typeof otherwise).
31
+ */
32
+ export declare function phpTypeName(value: unknown): string;
33
+ //# sourceMappingURL=reflect.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,15 @@
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;
4
+ }
5
+ function s(r) {
6
+ return r === void 0 ? null : typeof r == "function" ? r() : r;
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;
10
+ }
11
+ export {
12
+ y as phpTypeName,
13
+ s as resolveDefault,
14
+ p as typeOf
15
+ };
@@ -0,0 +1,20 @@
1
+ import { PathKey, SortSpec } from '@tolki/types';
2
+ /**
3
+ * Reads the value a sort descriptor's key names on one item.
4
+ *
5
+ * @param item - The item being sorted.
6
+ * @param key - The descriptor's key path.
7
+ * @returns The value to compare.
8
+ */
9
+ export type SortValueResolver = (item: unknown, key: PathKey) => unknown;
10
+ /**
11
+ * Build the sort-descriptor comparator factory for one path resolver.
12
+ *
13
+ * `@tolki/utils` sits below every path package, so the caller supplies the
14
+ * resolver: `getNestedValue` for `Arr`/`Obj`, `dataGet` for `Collection`.
15
+ *
16
+ * @param resolve - Reads a descriptor's key off an item.
17
+ * @returns A function building the comparator one sort descriptor implies.
18
+ */
19
+ export declare function createSortSpecComparator(resolve: SortValueResolver): <TValue>(spec: SortSpec<TValue>, forceDescending: boolean) => (a: TValue, b: TValue) => number;
20
+ //# sourceMappingURL=sorting.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,18 @@
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;
13
+ };
14
+ };
15
+ }
16
+ export {
17
+ y as createSortSpecComparator
18
+ };
@@ -0,0 +1,27 @@
1
+ import { PathKey } from '@tolki/types';
2
+ /**
3
+ * Convert a string to lower-case.
4
+ */
5
+ export declare function toLower(value: string): string;
6
+ /**
7
+ * Convert a string to upper-case.
8
+ */
9
+ export declare function toUpper(value: string): string;
10
+ /**
11
+ * Convert the first character of a string to lower-case.
12
+ */
13
+ export declare function lowerFirst(value: string): string;
14
+ /**
15
+ * Convert the first character of a string to upper-case.
16
+ */
17
+ export declare function upperFirst(value: string): string;
18
+ /**
19
+ * Build Arr::array()'s exact "must be an array" message, shared by every
20
+ * push/array guard so arr, obj, and the path leaf-check agree on one string.
21
+ *
22
+ * @param value - The resolved value that failed the array check.
23
+ * @param key - The key or path used in the error message.
24
+ * @returns The formatted error message.
25
+ */
26
+ export declare function arrayValueMessage(value: unknown, key: PathKey): string;
27
+ //# sourceMappingURL=string.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAI5C;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAUhD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAUhD;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,CAEtE"}
package/dist/string.js ADDED
@@ -0,0 +1,25 @@
1
+ import { phpTypeName as t } from "./reflect.js";
2
+ function o(r) {
3
+ return r.toLowerCase();
4
+ }
5
+ function u(r) {
6
+ return r.toUpperCase();
7
+ }
8
+ function a(r) {
9
+ const e = [...r];
10
+ return e.length === 0 ? r : (e[0] = e[0].toLowerCase(), e.join(""));
11
+ }
12
+ function i(r) {
13
+ const e = [...r];
14
+ return e.length === 0 ? r : (e[0] = e[0].toUpperCase(), e.join(""));
15
+ }
16
+ function p(r, e) {
17
+ return `Array value for key [${String(e)}] must be an array, ${t(r)} found.`;
18
+ }
19
+ export {
20
+ p as arrayValueMessage,
21
+ a as lowerFirst,
22
+ o as toLower,
23
+ u as toUpper,
24
+ i as upperFirst
25
+ };