@homedev/object-utils 1.0.4 → 1.0.5
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.d.ts +63 -3
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,96 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Maps an array asynchronously using Promise.all for parallel execution
|
|
3
|
+
* @typeParam T - Type of input array items
|
|
4
|
+
* @typeParam U - Type of async result items
|
|
5
|
+
* @param arr - Array to map
|
|
6
|
+
* @param fn - Async mapping function
|
|
7
|
+
* @returns Promise of mapped array
|
|
2
8
|
* @public
|
|
3
9
|
*/
|
|
4
10
|
export declare const asyncMap: <T, U>(arr: T[], fn: (item: T, index: number, array: T[]) => Promise<U>) => Promise<U[]>;
|
|
5
11
|
|
|
6
12
|
/**
|
|
13
|
+
* Deep clones an object using structuredClone (faster) with JSON fallback
|
|
14
|
+
* - Uses structuredClone for better performance and broader type support (Date, RegExp, etc.)
|
|
15
|
+
* - Falls back to JSON serialization if structuredClone fails or is unavailable
|
|
16
|
+
* - Note: Functions, symbols, and undefined values are lost in JSON fallback
|
|
17
|
+
* @typeParam T - Type of object to clone
|
|
18
|
+
* @param model - Object to clone
|
|
19
|
+
* @returns Deep clone of the object
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare const deepClone: <T>(model: T) => T;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that a value is defined, throwing an error if undefined
|
|
26
|
+
* @typeParam T - Type of the value
|
|
27
|
+
* @param value - Value to check
|
|
28
|
+
* @param msg - Error message to throw if value is undefined
|
|
29
|
+
* @returns The value if defined
|
|
30
|
+
* @throws Error if value is undefined
|
|
7
31
|
* @public
|
|
8
32
|
*/
|
|
9
33
|
export declare const defined: <T>(value: T | undefined, msg?: string) => T;
|
|
10
34
|
|
|
11
35
|
/**
|
|
36
|
+
* Maps an array while filtering out null/undefined values (performance optimized)
|
|
37
|
+
* Combines filter and map operations to avoid double iteration
|
|
38
|
+
* @typeParam T - Type of input array items
|
|
39
|
+
* @typeParam U - Type of mapped result items
|
|
40
|
+
* @param arr - Array to map (can be undefined)
|
|
41
|
+
* @param fn - Mapping function
|
|
42
|
+
* @returns Mapped array with nulls/undefined filtered out, or empty array if input is undefined
|
|
12
43
|
* @public
|
|
13
44
|
*/
|
|
14
45
|
export declare const nonNullMap: <T, U>(arr: T[] | undefined, fn: (item: T, index: number, array: T[]) => U) => U[];
|
|
15
46
|
|
|
16
47
|
/**
|
|
48
|
+
* Sorts an array by a selector function with custom comparison
|
|
49
|
+
* Uses a shallow copy to avoid mutating the original array
|
|
50
|
+
* @typeParam T - Type of array items
|
|
51
|
+
* @typeParam U - Type of comparison values
|
|
52
|
+
* @param arr - Array to sort
|
|
53
|
+
* @param selector - Function to extract comparison value from item
|
|
54
|
+
* @param compare - Comparison function (default: numeric comparison)
|
|
55
|
+
* @returns New sorted array
|
|
17
56
|
* @public
|
|
18
57
|
*/
|
|
19
58
|
export declare const sortBy: <T, U>(arr: T[], selector: (item: T) => U, compare?: (a: U, b: U) => number) => T[];
|
|
20
59
|
|
|
21
60
|
/**
|
|
61
|
+
* Converts an object's entries into an array of objects with a key property
|
|
62
|
+
* @typeParam T - The type of items in the resulting array
|
|
63
|
+
* @param from - Object to convert
|
|
64
|
+
* @param key - Property name to use for keys (default: 'name')
|
|
65
|
+
* @returns Array of objects with key properties
|
|
66
|
+
* @example
|
|
67
|
+
* toList({ a: { value: 1 }, b: { value: 2 } }) // [{ name: 'a', value: 1 }, { name: 'b', value: 2 }]
|
|
22
68
|
* @public
|
|
23
69
|
*/
|
|
24
|
-
export declare const toList: <T>(from:
|
|
70
|
+
export declare const toList: <T>(from: Record<string, unknown>, key?: string) => T[];
|
|
25
71
|
|
|
26
72
|
/**
|
|
73
|
+
* Converts an array into an object indexed by a key property, excluding the key from items
|
|
74
|
+
* @typeParam T - The type of the resulting object values
|
|
75
|
+
* @param from - Array to convert
|
|
76
|
+
* @param key - Property name to use as key (default: 'name')
|
|
77
|
+
* @returns Object indexed by key values, without the key property in values
|
|
78
|
+
* @example
|
|
79
|
+
* toObject([{ name: 'a', value: 1 }]) // { a: { value: 1 } }
|
|
27
80
|
* @public
|
|
28
81
|
*/
|
|
29
|
-
export declare const toObject: <T
|
|
82
|
+
export declare const toObject: <T extends Record<string, unknown>>(from: T[], key?: string) => Record<string, Omit<T, typeof key>>;
|
|
30
83
|
|
|
31
84
|
/**
|
|
85
|
+
* Converts an array into an object indexed by a key property, keeping the full items
|
|
86
|
+
* @typeParam T - The type of the resulting object
|
|
87
|
+
* @param from - Array to convert
|
|
88
|
+
* @param key - Property name to use as key (default: 'name')
|
|
89
|
+
* @returns Object indexed by key values
|
|
90
|
+
* @example
|
|
91
|
+
* toObjectWithKey([{ name: 'a', value: 1 }]) // { a: { name: 'a', value: 1 } }
|
|
32
92
|
* @public
|
|
33
93
|
*/
|
|
34
|
-
export declare const toObjectWithKey: <T
|
|
94
|
+
export declare const toObjectWithKey: <T extends Record<string, unknown>>(from: T[], key?: string) => Record<string, T>;
|
|
35
95
|
|
|
36
96
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var A=(h,j="name")=>Object.entries(h).map(([q,w])=>({[j]:q,...w})),B=(h,j="name")=>Object.fromEntries(h.map((q)=>[q[j],q])),D=(h,j="name")=>Object.fromEntries(h.map((q)=>{let{[j]:w,...z}=q;return[w,z]})),E=(h,j)=>{if(!h)return[];let q=[],w=0;for(let z of h)if(z!==null&&z!==void 0)q.push(j(z,w,h)),w++;return q},F=(h,j="Value is undefined")=>{if(h===void 0)throw Error(j);return h},G=(h,j,q=(w,z)=>w-z)=>h.slice().sort((w,z)=>q(j(w),j(z))),H=async(h,j)=>Promise.all(h.map(j)),I=(h)=>{if(typeof structuredClone<"u")try{return structuredClone(h)}catch{}return JSON.parse(JSON.stringify(h))};export{B as toObjectWithKey,D as toObject,A as toList,G as sortBy,E as nonNullMap,F as defined,I as deepClone,H as asyncMap};
|