@sohanemon/utils 5.0.4 → 5.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/functions/object.d.ts +26 -0
- package/dist/functions/object.js +28 -0
- package/package.json +1 -1
|
@@ -64,4 +64,30 @@ export declare function getObjectValue<T, S extends string, D>(obj: T, path: S,
|
|
|
64
64
|
* getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
|
|
65
65
|
*/
|
|
66
66
|
export declare function getObjectValue<T, S extends string>(obj: T, path: S): GetValue<T, SplitPath<S>> | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Extend an object or function with additional properties while
|
|
69
|
+
* preserving the original type information.
|
|
70
|
+
*
|
|
71
|
+
* Works with both plain objects and callable functions since
|
|
72
|
+
* functions in JavaScript are objects too.
|
|
73
|
+
*
|
|
74
|
+
* @template T The base object or function type
|
|
75
|
+
* @template P The additional properties type
|
|
76
|
+
*
|
|
77
|
+
* @param base - The object or function to extend
|
|
78
|
+
* @param props - An object containing properties to attach
|
|
79
|
+
*
|
|
80
|
+
* @returns The same object/function, augmented with the given properties
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* // Extend an object
|
|
84
|
+
* const obj = extendProps({ a: 1 }, { b: "hello" });
|
|
85
|
+
* // obj has { a: number; b: string }
|
|
86
|
+
*
|
|
87
|
+
* // Extend a function
|
|
88
|
+
* const fn = (x: number) => x * 2;
|
|
89
|
+
* const enhanced = extendProps(fn, { name: "doubler" });
|
|
90
|
+
* // enhanced is callable and also has { name: string }
|
|
91
|
+
*/
|
|
92
|
+
export declare function extendProps<T extends object, P extends object>(base: T, props: P): T & P;
|
|
67
93
|
export {};
|
package/dist/functions/object.js
CHANGED
|
@@ -37,3 +37,31 @@ export function getObjectValue(obj, path, defaultValue) {
|
|
|
37
37
|
}
|
|
38
38
|
return current !== undefined ? current : defaultValue;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Extend an object or function with additional properties while
|
|
42
|
+
* preserving the original type information.
|
|
43
|
+
*
|
|
44
|
+
* Works with both plain objects and callable functions since
|
|
45
|
+
* functions in JavaScript are objects too.
|
|
46
|
+
*
|
|
47
|
+
* @template T The base object or function type
|
|
48
|
+
* @template P The additional properties type
|
|
49
|
+
*
|
|
50
|
+
* @param base - The object or function to extend
|
|
51
|
+
* @param props - An object containing properties to attach
|
|
52
|
+
*
|
|
53
|
+
* @returns The same object/function, augmented with the given properties
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // Extend an object
|
|
57
|
+
* const obj = extendProps({ a: 1 }, { b: "hello" });
|
|
58
|
+
* // obj has { a: number; b: string }
|
|
59
|
+
*
|
|
60
|
+
* // Extend a function
|
|
61
|
+
* const fn = (x: number) => x * 2;
|
|
62
|
+
* const enhanced = extendProps(fn, { name: "doubler" });
|
|
63
|
+
* // enhanced is callable and also has { name: string }
|
|
64
|
+
*/
|
|
65
|
+
export function extendProps(base, props) {
|
|
66
|
+
return Object.assign(base, props);
|
|
67
|
+
}
|