@stryke/helpers 0.1.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.
Files changed (84) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +295 -0
  3. package/dist/arg-identity.cjs +9 -0
  4. package/dist/arg-identity.d.ts +10 -0
  5. package/dist/arg-identity.mjs +1 -0
  6. package/dist/debounce.cjs +24 -0
  7. package/dist/debounce.d.ts +41 -0
  8. package/dist/debounce.mjs +1 -0
  9. package/dist/deep-clone.cjs +61 -0
  10. package/dist/deep-clone.d.ts +63 -0
  11. package/dist/deep-clone.mjs +1 -0
  12. package/dist/deep-merge.cjs +35 -0
  13. package/dist/deep-merge.d.ts +12 -0
  14. package/dist/deep-merge.mjs +1 -0
  15. package/dist/delay.cjs +28 -0
  16. package/dist/delay.d.ts +72 -0
  17. package/dist/delay.mjs +1 -0
  18. package/dist/errors.cjs +18 -0
  19. package/dist/errors.d.ts +12 -0
  20. package/dist/errors.mjs +1 -0
  21. package/dist/flatten-object.cjs +20 -0
  22. package/dist/flatten-object.d.ts +33 -0
  23. package/dist/flatten-object.mjs +1 -0
  24. package/dist/get-field.cjs +41 -0
  25. package/dist/get-field.d.ts +251 -0
  26. package/dist/get-field.mjs +1 -0
  27. package/dist/get-ordered-by.cjs +18 -0
  28. package/dist/get-ordered-by.d.ts +36 -0
  29. package/dist/get-ordered-by.mjs +1 -0
  30. package/dist/get-unique.cjs +17 -0
  31. package/dist/get-unique.d.ts +22 -0
  32. package/dist/get-unique.mjs +1 -0
  33. package/dist/identity.cjs +9 -0
  34. package/dist/identity.d.ts +15 -0
  35. package/dist/identity.mjs +1 -0
  36. package/dist/index.cjs +280 -0
  37. package/dist/index.d.ts +25 -0
  38. package/dist/index.mjs +1 -0
  39. package/dist/is-equal.cjs +60 -0
  40. package/dist/is-equal.d.ts +18 -0
  41. package/dist/is-equal.mjs +1 -0
  42. package/dist/is-production.cjs +12 -0
  43. package/dist/is-production.d.ts +30 -0
  44. package/dist/is-production.mjs +1 -0
  45. package/dist/is-runtime-server.cjs +10 -0
  46. package/dist/is-runtime-server.d.ts +11 -0
  47. package/dist/is-runtime-server.mjs +1 -0
  48. package/dist/match-sorter.cjs +184 -0
  49. package/dist/match-sorter.d.ts +77 -0
  50. package/dist/match-sorter.mjs +1 -0
  51. package/dist/noop.cjs +10 -0
  52. package/dist/noop.d.ts +18 -0
  53. package/dist/noop.mjs +1 -0
  54. package/dist/remove-accents.cjs +411 -0
  55. package/dist/remove-accents.d.ts +8 -0
  56. package/dist/remove-accents.mjs +1 -0
  57. package/dist/remove-empty-items.cjs +8 -0
  58. package/dist/remove-empty-items.d.ts +7 -0
  59. package/dist/remove-empty-items.mjs +1 -0
  60. package/dist/set-field.cjs +20 -0
  61. package/dist/set-field.d.ts +10 -0
  62. package/dist/set-field.mjs +1 -0
  63. package/dist/throttle.cjs +13 -0
  64. package/dist/throttle.d.ts +28 -0
  65. package/dist/throttle.mjs +1 -0
  66. package/dist/timeout.cjs +11 -0
  67. package/dist/timeout.d.ts +8 -0
  68. package/dist/timeout.mjs +1 -0
  69. package/dist/to-deep-key.cjs +14 -0
  70. package/dist/to-deep-key.d.ts +35 -0
  71. package/dist/to-deep-key.mjs +1 -0
  72. package/dist/to-path.cjs +23 -0
  73. package/dist/to-path.d.ts +18 -0
  74. package/dist/to-path.mjs +1 -0
  75. package/dist/unflatten-object.cjs +10 -0
  76. package/dist/unflatten-object.d.ts +33 -0
  77. package/dist/unflatten-object.mjs +1 -0
  78. package/dist/union.cjs +10 -0
  79. package/dist/union.d.ts +20 -0
  80. package/dist/union.mjs +1 -0
  81. package/dist/with-timeout.cjs +10 -0
  82. package/dist/with-timeout.d.ts +20 -0
  83. package/dist/with-timeout.mjs +1 -0
  84. package/package.json +423 -0
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Creates a deep clone of the given object.
3
+ *
4
+ * @param obj - The object to clone.
5
+ * @returns A deep clone of the given object.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Clone a primitive values
10
+ * const num = 29;
11
+ * const clonedNum = clone(num);
12
+ * console.log(clonedNum); // 29
13
+ * console.log(clonedNum === num) ; // true
14
+ *
15
+ * // Clone an array
16
+ * const arr = [1, 2, 3];
17
+ * const clonedArr = clone(arr);
18
+ * console.log(clonedArr); // [1, 2, 3]
19
+ * console.log(clonedArr === arr); // false
20
+ *
21
+ * // Clone an array with nested objects
22
+ * const arr = [1, { a: 1 }, [1, 2, 3]];
23
+ * const clonedArr = clone(arr);
24
+ * arr[1].a = 2;
25
+ * console.log(arr); // [2, { a: 2 }, [1, 2, 3]]
26
+ * console.log(clonedArr); // [1, { a: 1 }, [1, 2, 3]]
27
+ * console.log(clonedArr === arr); // false
28
+ *
29
+ * // Clone an object
30
+ * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };
31
+ * const clonedObj = clone(obj);
32
+ * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }
33
+ * console.log(clonedObj === obj); // false
34
+ *
35
+ *
36
+ * // Clone an object with nested objects
37
+ * const obj = { a: 1, b: { c: 1 } };
38
+ * const clonedObj = clone(obj);
39
+ * obj.b.c = 2;
40
+ * console.log(obj); // { a: 1, b: { c: 2 } }
41
+ * console.log(clonedObj); // { a: 1, b: { c: 1 } }
42
+ * console.log(clonedObj === obj); // false
43
+ * ```
44
+ */
45
+ export declare function deepClone<T>(obj: T): Resolved<T>;
46
+ export type Resolved<T> = Equal<T, ResolvedMain<T>> extends true ? T : ResolvedMain<T>;
47
+ type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
48
+ type ResolvedMain<T> = T extends [never] ? never : ValueOf<T> extends boolean | number | bigint | string ? ValueOf<T> : T extends (...args: any[]) => any ? never : T extends object ? ResolvedObject<T> : ValueOf<T>;
49
+ type ResolvedObject<T extends object> = T extends (infer U)[] ? IsTuple<T> extends true ? ResolvedTuple<T> : ResolvedMain<U>[] : T extends Set<infer U> ? Set<ResolvedMain<U>> : T extends Map<infer K, infer V> ? Map<ResolvedMain<K>, ResolvedMain<V>> : T extends WeakSet<any> | WeakMap<any, any> ? never : T extends Date | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | BigUint64Array | Int8Array | Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array | ArrayBuffer | SharedArrayBuffer | DataView | Blob | File ? T : {
50
+ [P in keyof T]: ResolvedMain<T[P]>;
51
+ };
52
+ type ResolvedTuple<T extends readonly any[]> = T extends [] ? [] : T extends [infer F] ? [ResolvedMain<F>] : T extends [infer F, ...infer Rest extends readonly any[]] ? [ResolvedMain<F>, ...ResolvedTuple<Rest>] : T extends [(infer F)?] ? [ResolvedMain<F>?] : T extends [(infer F)?, ...infer Rest extends readonly any[]] ? [ResolvedMain<F>?, ...ResolvedTuple<Rest>] : [];
53
+ type IsTuple<T extends readonly any[] | {
54
+ length: number;
55
+ }> = [T] extends [
56
+ never
57
+ ] ? false : T extends readonly any[] ? number extends T["length"] ? false : true : false;
58
+ type ValueOf<Instance> = IsValueOf<Instance, boolean> extends true ? boolean : IsValueOf<Instance, number> extends true ? number : IsValueOf<Instance, string> extends true ? string : Instance;
59
+ type IsValueOf<Instance, O extends IValueOf<any>> = Instance extends O ? O extends IValueOf<infer Primitive> ? Instance extends Primitive ? false : true : false : false;
60
+ interface IValueOf<T> {
61
+ valueOf(): T;
62
+ }
63
+ export {};
@@ -0,0 +1 @@
1
+ import{isPrimitive as l,isTypedArray as i}from"@stryke/types";export function deepClone(e){if(l(e))return e;if(Array.isArray(e))return e.map(n=>deepClone(n));if(e instanceof Date)return new Date(e.getTime());if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(e instanceof Map){const n=new Map;for(const[t,s]of e.entries())n.set(t,deepClone(s));return n}if(e instanceof Set){const n=new Set;for(const t of e.values())n.add(deepClone(t));return n}if(i(e)){const n=new(Object.getPrototypeOf(e)).constructor(e.length);for(const[t,s]of e.entries())n[t]=deepClone(s);return n}if(e instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer)return[...e];if(e instanceof DataView){const n=new DataView([...e.buffer]);return r(e,n),n}if(typeof File<"u"&&e instanceof File){const n=new File([e],e.name,{type:e.type});return r(e,n),n}if(e instanceof Blob){const n=new Blob([e],{type:e.type});return r(e,n),n}if(e instanceof Error){const n=new e.constructor;return n.message=e.message,n.name=e.name,n.stack=e.stack,n.cause=e.cause,r(e,n),n}if(typeof e=="object"&&e!==null){const n={};return r(e,n),n}return e}function r(e,n){const t=Object.keys(e);for(const s of t){const a=Object.getOwnPropertyDescriptor(e,s);(a?.writable||a?.set)&&(n[s]=deepClone(e[s]))}}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.deepMerge = void 0;
7
+ var _types = require("@stryke/types");
8
+ const b = e => Array.isArray(e) ? [] : {},
9
+ s = (e, r) => r.clone !== !1 && r.isMergeableObject(e) ? deepMerge(b(e), e, r) : e,
10
+ l = (e, r, n) => [...e, ...r].map(t => s(t, n)),
11
+ f = (e, r) => {
12
+ if (!r.customMerge) return deepMerge;
13
+ const n = r.customMerge(e);
14
+ return (0, _types.isFunction)(n) ? n : deepMerge;
15
+ },
16
+ y = e => [...Object.keys(e), ...(Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(e).filter(r => Object.propertyIsEnumerable.call(e, r)) : [])],
17
+ u = (e, r, n) => {
18
+ const t = {};
19
+ if (n.isMergeableObject(e)) for (const a of y(e)) t[a] = s(e[a], n);
20
+ for (const a of y(r)) t[a] = (0, _types.propertyUnsafe)(e, a) && n.isMergeableObject(r[a]) ? f(a, n)(e[a], r[a], n) : s(r[a], n);
21
+ return t;
22
+ };
23
+ const deepMerge = (e, r, n = {}) => {
24
+ if (!e || !r) return e || r;
25
+ const t = n || {};
26
+ t.arrayMerge = n.arrayMerge || l, t.isMergeableObject = t.isMergeableObject || _types.isMergeableObject, t.cloneUnlessOtherwiseSpecified = s;
27
+ const a = Array.isArray(r),
28
+ c = Array.isArray(e);
29
+ return a === c ? a ? t.arrayMerge(e, r, t) : u(e, r, t) : s(r, t);
30
+ };
31
+ exports.deepMerge = deepMerge;
32
+ deepMerge.all = function (r, n) {
33
+ if (!Array.isArray(r)) throw new TypeError("first argument should be an array");
34
+ return r.reduce((t, a) => deepMerge(t, a, n), {});
35
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Deep merge two objects
3
+ *
4
+ * @param target - The target object
5
+ * @param source - The source object
6
+ * @param options - The options object
7
+ * @returns The merged object
8
+ */
9
+ export declare const deepMerge: {
10
+ <X = any, Y = any, Z = X & Y>(target: X, source: Y, options?: any): Z;
11
+ all(array: any[], options?: any): any;
12
+ };
@@ -0,0 +1 @@
1
+ import{isFunction as i,isMergeableObject as o,propertyUnsafe as g}from"@stryke/types";const b=e=>Array.isArray(e)?[]:{},s=(e,r)=>r.clone!==!1&&r.isMergeableObject(e)?deepMerge(b(e),e,r):e,l=(e,r,n)=>[...e,...r].map(t=>s(t,n)),f=(e,r)=>{if(!r.customMerge)return deepMerge;const n=r.customMerge(e);return i(n)?n:deepMerge},y=e=>[...Object.keys(e),...Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(r=>Object.propertyIsEnumerable.call(e,r)):[]],u=(e,r,n)=>{const t={};if(n.isMergeableObject(e))for(const a of y(e))t[a]=s(e[a],n);for(const a of y(r))t[a]=g(e,a)&&n.isMergeableObject(r[a])?f(a,n)(e[a],r[a],n):s(r[a],n);return t};export const deepMerge=(e,r,n={})=>{if(!e||!r)return e||r;const t=n||{};t.arrayMerge=n.arrayMerge||l,t.isMergeableObject=t.isMergeableObject||o,t.cloneUnlessOtherwiseSpecified=s;const a=Array.isArray(r),c=Array.isArray(e);return a===c?a?t.arrayMerge(e,r,t):u(e,r,t):s(r,t)};deepMerge.all=function(r,n){if(!Array.isArray(r))throw new TypeError("first argument should be an array");return r.reduce((t,a)=>deepMerge(t,a,n),{})};
package/dist/delay.cjs ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.delay = delay;
7
+ exports.sleep = sleep;
8
+ var _errors = require("./errors.cjs");
9
+ function delay(r, {
10
+ signal: e
11
+ } = {}) {
12
+ return new Promise((t, n) => {
13
+ const o = () => {
14
+ n(new _errors.AbortError());
15
+ },
16
+ i = () => {
17
+ clearTimeout(a), o();
18
+ };
19
+ if (e?.aborted) return o();
20
+ const a = setTimeout(t, r);
21
+ e?.addEventListener("abort", i, {
22
+ once: !0
23
+ });
24
+ });
25
+ }
26
+ function sleep(r, e) {
27
+ return delay(r, e);
28
+ }
@@ -0,0 +1,72 @@
1
+ interface DelayOptions {
2
+ signal?: AbortSignal;
3
+ }
4
+ /**
5
+ * Delays the execution of code for a specified number of milliseconds.
6
+ *
7
+ * This function returns a Promise that resolves after the specified delay, allowing you to use it
8
+ * with async/await to pause execution.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * async function foo() {
13
+ * console.log('Start');
14
+ * await delay(1000); // Delays execution for 1 second
15
+ * console.log('End');
16
+ * }
17
+ *
18
+ * foo();
19
+ *
20
+ * // With AbortSignal
21
+ * const controller = new AbortController();
22
+ * const { signal } = controller;
23
+ *
24
+ * setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
25
+ * try {
26
+ * await delay(100, { signal });
27
+ * } catch (error) {
28
+ * console.error(error); // Will log 'AbortError'
29
+ * }
30
+ * }
31
+ * ```
32
+ *
33
+ * @param ms - The number of milliseconds to delay.
34
+ * @param options - The options object.
35
+ * @returns A Promise that resolves after the specified delay.
36
+ */
37
+ export declare function delay(ms: number, { signal }?: DelayOptions): Promise<void>;
38
+ /**
39
+ * Delays the execution of code for a specified number of milliseconds.
40
+ *
41
+ * This function returns a Promise that resolves after the specified delay, allowing you to use it
42
+ * with async/await to pause execution.
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * async function foo() {
47
+ * console.log('Start');
48
+ * await sleep(1000); // Delays execution for 1 second
49
+ * console.log('End');
50
+ * }
51
+ *
52
+ * foo();
53
+ *
54
+ * // With AbortSignal
55
+ * const controller = new AbortController();
56
+ * const { signal } = controller;
57
+ *
58
+ * setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
59
+ * try {
60
+ * await sleep(100, { signal });
61
+ * } catch (error) {
62
+ * console.error(error); // Will log 'AbortError'
63
+ * }
64
+ * }
65
+ * ```
66
+ *
67
+ * @param ms - The number of milliseconds to sleep.
68
+ * @param options - The options object.
69
+ * @returns A Promise that resolves after the specified sleep.
70
+ */
71
+ export declare function sleep(ms: number, options?: DelayOptions): Promise<void>;
72
+ export {};
package/dist/delay.mjs ADDED
@@ -0,0 +1 @@
1
+ import{AbortError as s}from"./errors";export function delay(r,{signal:e}={}){return new Promise((t,n)=>{const o=()=>{n(new s)},i=()=>{clearTimeout(a),o()};if(e?.aborted)return o();const a=setTimeout(t,r);e?.addEventListener("abort",i,{once:!0})})}export function sleep(r,e){return delay(r,e)}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TimeoutError = exports.AbortError = void 0;
7
+ class AbortError extends Error {
8
+ constructor(r = "The operation was aborted") {
9
+ super(r), this.name = "AbortError";
10
+ }
11
+ }
12
+ exports.AbortError = AbortError;
13
+ class TimeoutError extends Error {
14
+ constructor(r = "The operation was timed out") {
15
+ super(r), this.name = "TimeoutError";
16
+ }
17
+ }
18
+ exports.TimeoutError = TimeoutError;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * An error class representing an aborted operation.
3
+ */
4
+ export declare class AbortError extends Error {
5
+ constructor(message?: string);
6
+ }
7
+ /**
8
+ * An error class representing an timeout operation.
9
+ */
10
+ export declare class TimeoutError extends Error {
11
+ constructor(message?: string);
12
+ }
@@ -0,0 +1 @@
1
+ export class AbortError extends Error{constructor(r="The operation was aborted"){super(r),this.name="AbortError"}}export class TimeoutError extends Error{constructor(r="The operation was timed out"){super(r),this.name="TimeoutError"}}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.flattenObject = flattenObject;
7
+ var _types = require("@stryke/types");
8
+ function flattenObject(t) {
9
+ return T(t);
10
+ }
11
+ function T(t, s = "") {
12
+ const n = {},
13
+ O = Object.keys(t);
14
+ for (const c of O) {
15
+ const e = t[c],
16
+ y = s ? `${s}.${c}` : c;
17
+ if ((0, _types.isPlainObject)(e) && Object.keys(e).length > 0) Object.assign(n, T(e, y));else if (Array.isArray(e)) for (const [a, b] of e.entries()) n[`${y}.${a}`] = b;else n[y] = e;
18
+ }
19
+ return n;
20
+ }
@@ -0,0 +1,33 @@
1
+ import { type DeepKey, type DeepValue } from "@stryke/types";
2
+ /**
3
+ * Flattens a nested object into a single level object with dot-separated keys.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * const nestedObject = {
8
+ * a: {
9
+ * b: {
10
+ * c: 1
11
+ * }
12
+ * },
13
+ * d: [2, 3]
14
+ * };
15
+ *
16
+ * const flattened = flattenObject(nestedObject);
17
+ * console.log(flattened);
18
+ * // Output:
19
+ * // {
20
+ * // 'a.b.c': 1,
21
+ * // 'd.0': 2,
22
+ * // 'd.1': 3
23
+ * // }
24
+ * ```
25
+ *
26
+ * @param object - The object to flatten.
27
+ * @returns - The flattened object.
28
+ */
29
+ export declare function flattenObject<TObject extends Record<string, any> = Record<string, any>, TDeepKeyObject extends {
30
+ [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;
31
+ } = {
32
+ [TKey in DeepKey<TObject>]: DeepValue<TObject, TKey>;
33
+ }>(object: TObject): TDeepKeyObject;
@@ -0,0 +1 @@
1
+ import{isPlainObject as j}from"@stryke/types";export function flattenObject(t){return T(t)}function T(t,s=""){const n={},O=Object.keys(t);for(const c of O){const e=t[c],y=s?`${s}.${c}`:c;if(j(e)&&Object.keys(e).length>0)Object.assign(n,T(e,y));else if(Array.isArray(e))for(const[a,b]of e.entries())n[`${y}.${a}`]=b;else n[y]=e}return n}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getField = getField;
7
+ var _types = require("@stryke/types");
8
+ var _toPath = require("./to-path.cjs");
9
+ function getField(t, e, d) {
10
+ if (t === null) return d;
11
+ switch (typeof e) {
12
+ case "string":
13
+ {
14
+ const n = t[e];
15
+ return n === void 0 ? (0, _types.isDeepKey)(e) ? getField(t, (0, _toPath.toPath)(e), d) : d : n;
16
+ }
17
+ case "number":
18
+ case "symbol":
19
+ {
20
+ (0, _types.isNumber)(e) && (e = (0, _types.toStringKey)(e));
21
+ const n = Array.isArray(e) ? void 0 : t[e];
22
+ return n === void 0 ? d : n;
23
+ }
24
+ default:
25
+ {
26
+ if (Array.isArray(e)) return u(t, e, d);
27
+ e = Object.is(e?.valueOf(), -0) ? "-0" : String(e);
28
+ const n = t[e];
29
+ return n === void 0 ? d : n;
30
+ }
31
+ }
32
+ }
33
+ function u(t, e, d) {
34
+ if (e.length === 0) return d;
35
+ let n = t;
36
+ for (const i of e) {
37
+ if (n === null) return d;
38
+ n = n[i];
39
+ }
40
+ return n === void 0 ? d : n;
41
+ }
@@ -0,0 +1,251 @@
1
+ /**
2
+ * See the definition of `@types/lodash`.
3
+ */
4
+ type GetIndexedField<T, K> = K extends keyof T ? T[K] : K extends `${number}` ? "length" extends keyof T ? number extends T["length"] ? number extends keyof T ? T[number] : undefined : undefined : undefined : undefined;
5
+ type FieldWithPossiblyUndefined<T, Key> = GetField<Exclude<T, undefined>, Key> | Extract<T, undefined>;
6
+ type IndexedFieldWithPossiblyUndefined<T, Key> = GetIndexedField<Exclude<T, undefined>, Key> | Extract<T, undefined>;
7
+ export type GetField<T, P> = P extends `${infer Left}.${infer Right}` ? Left extends keyof Exclude<T, undefined> ? FieldWithPossiblyUndefined<Exclude<T, undefined>[Left], Right> | Extract<T, undefined> : Left extends `${infer FieldKey}[${infer IndexKey}]` ? FieldKey extends keyof T ? FieldWithPossiblyUndefined<IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey>, Right> : undefined : undefined : P extends keyof T ? T[P] : P extends `${infer FieldKey}[${infer IndexKey}]` ? FieldKey extends keyof T ? IndexedFieldWithPossiblyUndefined<T[FieldKey], IndexKey> : undefined : IndexedFieldWithPossiblyUndefined<T, P>;
8
+ /**
9
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
10
+ *
11
+ * @template T - The type of the object.
12
+ * @template K - The type of the key in the object.
13
+ * @template D - The type of the default value.
14
+ *
15
+ * @param {T} object - The object to query.
16
+ * @param {K | [K]} path - The path of the property to get.
17
+ * @returns {T[K]} - Returns the resolved value.
18
+ */
19
+ export declare function getField<T extends object, K extends keyof T>(object: T, path: K | readonly [K]): T[K];
20
+ /**
21
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
22
+ *
23
+ * @template T - The type of the object.
24
+ * @template K - The type of the key in the object.
25
+ *
26
+ * @param {T | null | undefined} object - The object to query.
27
+ * @param {K | [K]} path - The path of the property to get.
28
+ * @returns {T[K] | undefined} - Returns the resolved value.
29
+ */
30
+ export declare function getField<T extends object, K extends keyof T>(object: T | null | undefined, path: K | readonly [K]): T[K] | undefined;
31
+ /**
32
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
33
+ *
34
+ * @template T - The type of the object.
35
+ * @template K - The type of the key in the object.
36
+ * @template D - The type of the default value.
37
+ *
38
+ * @param {T | null | undefined} object - The object to query.
39
+ * @param {K | [K]} path - The path of the property to get.
40
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
41
+ * @returns {Exclude<T[K], undefined> | D} - Returns the resolved value.
42
+ */
43
+ export declare function getField<T extends object, K extends keyof T, D>(object: T | null | undefined, path: K | readonly [K], defaultValue: D): Exclude<T[K], undefined> | D;
44
+ /**
45
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
46
+ *
47
+ * @template T - The type of the object.
48
+ * @template K1 - The type of the first key in the object.
49
+ * @template K2 - The type of the second key in the object.
50
+ *
51
+ * @param {T} object - The object to query.
52
+ * @param {[K1, K2]} path - The path of the property to get.
53
+ * @returns {T[K1][K2]} - Returns the resolved value.
54
+ */
55
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(object: T, path: readonly [K1, K2]): T[K1][K2];
56
+ /**
57
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
58
+ *
59
+ * @template T - The type of the object.
60
+ * @template K1 - The type of the first key in the object.
61
+ * @template K2 - The type of the second key in the object.
62
+ *
63
+ * @param {T | null | undefined} object - The object to query.
64
+ * @param {[K1, K2]} path - The path of the property to get.
65
+ * @returns {T[K1][K2] | undefined} - Returns the resolved value.
66
+ */
67
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(object: T | null | undefined, path: readonly [K1, K2]): T[K1][K2] | undefined;
68
+ /**
69
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
70
+ *
71
+ * @template T - The type of the object.
72
+ * @template K1 - The type of the first key in the object.
73
+ * @template K2 - The type of the second key in the object.
74
+ * @template D - The type of the default value.
75
+ *
76
+ * @param {T | null | undefined} object - The object to query.
77
+ * @param {[K1, K2]} path - The path of the property to get.
78
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
79
+ * @returns {Exclude<T[K1][K2], undefined> | D} - Returns the resolved value.
80
+ */
81
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], D>(object: T | null | undefined, path: readonly [K1, K2], defaultValue: D): Exclude<T[K1][K2], undefined> | D;
82
+ /**
83
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
84
+ *
85
+ * @template T - The type of the object.
86
+ * @template K1 - The type of the first key in the object.
87
+ * @template K2 - The type of the second key in the object.
88
+ * @template K3 - The type of the third key in the object.
89
+ *
90
+ * @param {T} object - The object to query.
91
+ * @param {[K1, K2, K3]} path - The path of the property to get.
92
+ * @returns {T[K1][K2][K3]} - Returns the resolved value.
93
+ */
94
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(object: T, path: readonly [K1, K2, K3]): T[K1][K2][K3];
95
+ /**
96
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
97
+ *
98
+ * @template T - The type of the object.
99
+ * @template K1 - The type of the first key in the object.
100
+ * @template K2 - The type of the second key in the object.
101
+ * @template K3 - The type of the third key in the object.
102
+ *
103
+ * @param {T | null | undefined} object - The object to query.
104
+ * @param {[K1, K2, K3]} path - The path of the property to get.
105
+ * @returns {T[K1][K2][K3] | undefined} - Returns the resolved value.
106
+ */
107
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(object: T | null | undefined, path: readonly [K1, K2, K3]): T[K1][K2][K3] | undefined;
108
+ /**
109
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
110
+ *
111
+ * @template T - The type of the object.
112
+ * @template K1 - The type of the first key in the object.
113
+ * @template K2 - The type of the second key in the object.
114
+ * @template K3 - The type of the third key in the object.
115
+ * @template D - The type of the default value.
116
+ *
117
+ * @param {T | null | undefined} object - The object to query.
118
+ * @param {[K1, K2, K3]} path - The path of the property to get.
119
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
120
+ * @returns {Exclude<T[K1][K2][K3], undefined> | D} - Returns the resolved value.
121
+ */
122
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], D>(object: T | null | undefined, path: readonly [K1, K2, K3], defaultValue: D): Exclude<T[K1][K2][K3], undefined> | D;
123
+ /**
124
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
125
+ *
126
+ * @template T - The type of the object.
127
+ * @template K1 - The type of the first key in the object.
128
+ * @template K2 - The type of the second key in the object.
129
+ * @template K3 - The type of the third key in the object.
130
+ * @template K4 - The type of the fourth key in the object.
131
+ *
132
+ * @param {T} object - The object to query.
133
+ * @param {[K1, K2, K3, K4]} path - The path of the property to get.
134
+ * @returns {T[K1][K2][K3][K4]} - Returns the resolved value.
135
+ */
136
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(object: T, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4];
137
+ /**
138
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
139
+ *
140
+ * @template T - The type of the object.
141
+ * @template K1 - The type of the first key in the object.
142
+ * @template K2 - The type of the second key in the object.
143
+ * @template K3 - The type of the third key in the object.
144
+ * @template K4 - The type of the fourth key in the object.
145
+ *
146
+ * @param {T | null | undefined} object - The object to query.
147
+ * @param {[K1, K2, K3, K4]} path - The path of the property to get.
148
+ * @returns {T[K1][K2][K3][K4] | undefined} - Returns the resolved value.
149
+ */
150
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(object: T | null | undefined, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4] | undefined;
151
+ /**
152
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
153
+ *
154
+ * @template T - The type of the object.
155
+ * @template K1 - The type of the first key in the object.
156
+ * @template K2 - The type of the second key in the object.
157
+ * @template K3 - The type of the third key in the object.
158
+ * @template K4 - The type of the fourth key in the object.
159
+ * @template D - The type of the default value.
160
+ *
161
+ * @param {T | null | undefined} object - The object to query.
162
+ * @param {[K1, K2, K3, K4]} path - The path of the property to get.
163
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
164
+ * @returns {Exclude<T[K1][K2][K3][K4], undefined> | D} - Returns the resolved value.
165
+ */
166
+ export declare function getField<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], D>(object: T | null | undefined, path: readonly [K1, K2, K3, K4], defaultValue: D): Exclude<T[K1][K2][K3][K4], undefined> | D;
167
+ /**
168
+ * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.
169
+ *
170
+ * @template T - The type of the value.
171
+ *
172
+ * @param {Record<number, T>} object - The object to query.
173
+ * @param {number} path - The path of the property to get.
174
+ * @returns {T} - Returns the resolved value.
175
+ */
176
+ export declare function getField<T>(object: Record<number, T>, path: number): T;
177
+ /**
178
+ * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.
179
+ *
180
+ * @template T - The type of the value.
181
+ *
182
+ * @param {Record<number, T> | null | undefined} object - The object to query.
183
+ * @param {number} path - The path of the property to get.
184
+ * @returns {T | undefined} - Returns the resolved value.
185
+ */
186
+ export declare function getField<T>(object: Record<number, T> | null | undefined, path: number): T | undefined;
187
+ /**
188
+ * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead.
189
+ *
190
+ * @template T - The type of the value.
191
+ * @template D - The type of the default value.
192
+ *
193
+ * @param {Record<number, T> | null | undefined} object - The object to query.
194
+ * @param {number} path - The path of the property to get.
195
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
196
+ * @returns {T | D} - Returns the resolved value.
197
+ */
198
+ export declare function getField<T, D>(object: Record<number, T> | null | undefined, path: number, defaultValue: D): T | D;
199
+ /**
200
+ * Retrieves the value at a given path from a null or undefined object, returning the default value.
201
+ *
202
+ * @template D - The type of the default value.
203
+ *
204
+ * @param {null | undefined} object - The object to query.
205
+ * @param {PropertyKey} path - The path of the property to get.
206
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
207
+ * @returns {D} - Returns the default value.
208
+ */
209
+ export declare function getField<D>(object: null | undefined, path: PropertyKey, defaultValue: D): D;
210
+ /**
211
+ * Retrieves the value at a given path from a null or undefined object, returning undefined.
212
+ *
213
+ * @param {null | undefined} object - The object to query.
214
+ * @param {PropertyKey} path - The path of the property to get.
215
+ * @returns {undefined} - Returns undefined.
216
+ */
217
+ export declare function getField(object: null | undefined, path: PropertyKey): undefined;
218
+ /**
219
+ * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.
220
+ *
221
+ * @template T - The type of the object.
222
+ * @template P - The type of the path.
223
+ *
224
+ * @param {T} data - The object to query.
225
+ * @param {P} path - The path of the property to get.
226
+ * @returns {string extends P ? any : GetField<T, P>} - Returns the resolved value.
227
+ */
228
+ export declare function getField<T, P extends string>(data: T, path: P): string extends P ? any : GetField<T, P>;
229
+ /**
230
+ * Retrieves the value at a given path from a string-keyed object. If the resolved value is undefined, the defaultValue is returned instead.
231
+ *
232
+ * @template T - The type of the object.
233
+ * @template P - The type of the path.
234
+ * @template D - The type of the default value.
235
+ *
236
+ * @param {T} data - The object to query.
237
+ * @param {P} path - The path of the property to get.
238
+ * @param {D} defaultValue - The value returned if the resolved value is undefined.
239
+ * @returns {Exclude<GetField<T, P>, null | undefined> | D} - Returns the resolved value.
240
+ */
241
+ export declare function getField<T, P extends string, D = GetField<T, P>>(data: T, path: P, defaultValue: D): Exclude<GetField<T, P>, null | undefined> | D;
242
+ /**
243
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
244
+ *
245
+ * @param {unknown} object - The object to query.
246
+ * @param {PropertyKey | readonly PropertyKey[]} path - The path of the property to get.
247
+ * @param {unknown} [defaultValue] - The value returned if the resolved value is undefined.
248
+ * @returns {any} - Returns the resolved value.
249
+ */
250
+ export declare function getField(object: unknown, path: PropertyKey | readonly PropertyKey[], defaultValue?: unknown): any;
251
+ export {};
@@ -0,0 +1 @@
1
+ import{isDeepKey as o,isNumber as K,toStringKey as f}from"@stryke/types";import{toPath as r}from"./to-path";export function getField(t,e,d){if(t===null)return d;switch(typeof e){case"string":{const n=t[e];return n===void 0?o(e)?getField(t,r(e),d):d:n}case"number":case"symbol":{K(e)&&(e=f(e));const n=Array.isArray(e)?void 0:t[e];return n===void 0?d:n}default:{if(Array.isArray(e))return u(t,e,d);e=Object.is(e?.valueOf(),-0)?"-0":String(e);const n=t[e];return n===void 0?d:n}}}function u(t,e,d){if(e.length===0)return d;let n=t;for(const i of e){if(n===null)return d;n=n[i]}return n===void 0?d:n}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getOrderedBy = getOrderedBy;
7
+ function getOrderedBy(f, n, o) {
8
+ const u = (r, e, t) => r < e ? t === "asc" ? -1 : 1 : r > e ? t === "asc" ? 1 : -1 : 0,
9
+ T = n.map((r, e) => o[e] ?? o.at(-1));
10
+ return [...f].sort((r, e) => {
11
+ for (const [t, c] of n.entries()) {
12
+ const i = T[t],
13
+ s = u(r[c], e[c], i);
14
+ if (s !== 0) return s;
15
+ }
16
+ return 0;
17
+ });
18
+ }