@sohanemon/utils 4.1.2 → 4.1.3
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/functions/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { clsx } from 'clsx';
|
|
|
8
8
|
import { extendTailwindMerge } from 'tailwind-merge';
|
|
9
9
|
import { withFluid } from '@fluid-tailwind/tailwind-merge';
|
|
10
10
|
export * from './cookie';
|
|
11
|
+
export * from './object';
|
|
11
12
|
export function cn(...inputs) {
|
|
12
13
|
const twMerge = extendTailwindMerge(withFluid);
|
|
13
14
|
return twMerge(clsx(inputs));
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type representing a path split into segments
|
|
3
|
+
* @template S - The original path string type
|
|
4
|
+
*/
|
|
5
|
+
type SplitPath<S extends string> = S extends `${infer First}.${infer Rest}` ? [First, ...SplitPath<Rest>] : [S];
|
|
6
|
+
/**
|
|
7
|
+
* Recursive type to resolve nested object types based on path
|
|
8
|
+
* @template T - Current object type
|
|
9
|
+
* @template K - Array of path segments
|
|
10
|
+
*/
|
|
11
|
+
type GetValue<T, K extends Array<string | number>> = K extends [
|
|
12
|
+
infer First,
|
|
13
|
+
...infer Rest
|
|
14
|
+
] ? First extends keyof T ? GetValue<T[First], Rest extends Array<string | number> ? Rest : []> : First extends `${number}` ? T extends any[] ? GetValue<T[number], Rest extends Array<string | number> ? Rest : []> : undefined : undefined : T;
|
|
15
|
+
/**
|
|
16
|
+
* Get a nested value from an object using array path segments
|
|
17
|
+
* @template T - Object type
|
|
18
|
+
* @template K - Path segments array type
|
|
19
|
+
* @template D - Default value type
|
|
20
|
+
* @param obj - Source object
|
|
21
|
+
* @param path - Array of path segments
|
|
22
|
+
* @param defaultValue - Fallback value if path not found
|
|
23
|
+
* @returns Value at path or default value
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* getObjectValue({a: [{b: 1}]}, ['a', 0, 'b']) // 1
|
|
27
|
+
*/
|
|
28
|
+
export declare function getObjectValue<T, K extends Array<string | number>, D>(obj: T, path: K, defaultValue: D): Exclude<GetValue<T, K>, undefined> | D;
|
|
29
|
+
/**
|
|
30
|
+
* Get a nested value from an object using array path segments
|
|
31
|
+
* @template T - Object type
|
|
32
|
+
* @template K - Path segments array type
|
|
33
|
+
* @param obj - Source object
|
|
34
|
+
* @param path - Array of path segments
|
|
35
|
+
* @returns Value at path or undefined
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* getObjectValue({a: [{b: 1}]}, ['a', 0, 'b']) // 1
|
|
39
|
+
*/
|
|
40
|
+
export declare function getObjectValue<T, K extends Array<string | number>>(obj: T, path: K): GetValue<T, K> | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Get a nested value from an object using dot notation path
|
|
43
|
+
* @template T - Object type
|
|
44
|
+
* @template S - Path string literal type
|
|
45
|
+
* @template D - Default value type
|
|
46
|
+
* @param obj - Source object
|
|
47
|
+
* @param path - Dot-separated path string
|
|
48
|
+
* @param defaultValue - Fallback value if path not found
|
|
49
|
+
* @returns Value at path or default value
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b', 2) // 1
|
|
53
|
+
*/
|
|
54
|
+
export declare function getObjectValue<T, S extends string, D>(obj: T, path: S, defaultValue: D): Exclude<GetValue<T, SplitPath<S>>, undefined> | D;
|
|
55
|
+
/**
|
|
56
|
+
* Get a nested value from an object using dot notation path
|
|
57
|
+
* @template T - Object type
|
|
58
|
+
* @template S - Path string literal type
|
|
59
|
+
* @param obj - Source object
|
|
60
|
+
* @param path - Dot-separated path string
|
|
61
|
+
* @returns Value at path or undefined
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
|
|
65
|
+
*/
|
|
66
|
+
export declare function getObjectValue<T, S extends string>(obj: T, path: S): GetValue<T, SplitPath<S>> | undefined;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implementation of deep object value retrieval with type safety
|
|
3
|
+
* @param obj - Source object
|
|
4
|
+
* @param path - Path specification (string or array)
|
|
5
|
+
* @param defaultValue - Optional fallback value
|
|
6
|
+
* @returns Value at path or default/undefined
|
|
7
|
+
*/
|
|
8
|
+
export function getObjectValue(obj, path, defaultValue) {
|
|
9
|
+
// Validate path type and handle edge cases
|
|
10
|
+
if (typeof path !== 'string' && !Array.isArray(path)) {
|
|
11
|
+
return defaultValue;
|
|
12
|
+
}
|
|
13
|
+
// Ensure pathArray is always an array
|
|
14
|
+
const pathArray = (() => {
|
|
15
|
+
if (Array.isArray(path))
|
|
16
|
+
return path;
|
|
17
|
+
if (path === '')
|
|
18
|
+
return [];
|
|
19
|
+
return String(path)
|
|
20
|
+
.split('.')
|
|
21
|
+
.filter((segment) => segment !== '');
|
|
22
|
+
})();
|
|
23
|
+
// Final safety check for array type
|
|
24
|
+
if (!Array.isArray(pathArray)) {
|
|
25
|
+
return defaultValue;
|
|
26
|
+
}
|
|
27
|
+
let current = obj;
|
|
28
|
+
for (const key of pathArray) {
|
|
29
|
+
if (current === null || current === undefined) {
|
|
30
|
+
return defaultValue;
|
|
31
|
+
}
|
|
32
|
+
// Convert numeric strings to numbers for arrays
|
|
33
|
+
const actualKey = typeof key === 'string' && Array.isArray(current) && /^\d+$/.test(key)
|
|
34
|
+
? Number.parseInt(key, 10)
|
|
35
|
+
: key;
|
|
36
|
+
current = current[actualKey];
|
|
37
|
+
}
|
|
38
|
+
return current !== undefined ? current : defaultValue;
|
|
39
|
+
}
|