@@ -6,57 +6,41 @@ import { DeepObject } from './types/utils';
6
6
* @returns {boolean }
7
7
*/
8
8
function isObject ( item : unknown ) : item is DeepObject {
9
- return Boolean ( item && typeof item === 'object' && ! Array . isArray ( item ) ) ;
9
+ return Boolean ( item && typeof item === 'object' && ! Array . isArray ( item ) ) ;
10
10
}
11
11
12
- /**
13
- * Deep merge two objects.
14
- * @param target The target object to merge into
15
- * @param sources The source objects to merge from
16
- * @returns The merged object
17
- */
18
- export function mergeDeep < T extends DeepObject > ( target : T , ...sources : DeepObject [ ] ) : T {
19
- if ( ! sources . length ) return target ;
20
- const source = sources . shift ( ) ;
21
-
22
- if ( isObject ( target ) && isObject ( source ) ) {
23
- for ( const key in source ) {
24
- if ( isObject ( source [ key ] ) ) {
25
- if ( ! target [ key ] ) Object . assign ( target , { [ key ] : { } } ) ;
26
- mergeDeep ( target [ key ] , source [ key ] ) ;
27
- } else {
28
- Object . assign ( target , { [ key ] : source [ key ] } ) ;
29
- }
30
- }
31
- }
32
-
33
- return mergeDeep ( target , ...sources ) ;
12
+ export function deepFlattenObject (
13
+ obj : DeepObject ,
14
+ prefix : string = '' ,
15
+ ) : Record < string , any > {
16
+ return Object . entries ( obj ) . reduce (
17
+ ( acc , [ key , value ] ) => {
18
+ if ( isObject ( value ) ) {
19
+ Object . assign ( acc , deepFlattenObject ( value , `${ prefix } ${ key } .` ) ) ;
20
+ } else {
21
+ acc [ `${ prefix } ${ key } ` ] = value ;
22
+ }
23
+ return acc ;
24
+ } ,
25
+ { } as Record < string , any > ,
26
+ ) ;
34
27
}
35
28
36
- export function deepFlattenObject ( obj : DeepObject , prefix : string = '' ) : Record < string , any > {
37
- return Object . entries ( obj ) . reduce ( ( acc , [ key , value ] ) => {
38
- if ( isObject ( value ) ) {
39
- Object . assign ( acc , deepFlattenObject ( value , `${ prefix } ${ key } .` ) ) ;
40
- } else {
41
- acc [ `${ prefix } ${ key } ` ] = value ;
42
- }
43
- return acc ;
44
- } , { } as Record < string , any > ) ;
45
- }
46
-
47
- export function buildObjectFromFlattenedObject ( flattenedObject : Record < string , any > ) : DeepObject {
48
- return Object . entries ( flattenedObject ) . reduce ( ( acc , [ key , value ] ) => {
49
- const keys = key . split ( '.' ) ;
50
- let lastKey = keys . pop ( ) ;
51
- if ( ! lastKey ) return acc ;
52
-
53
- let current = acc ;
54
- for ( const k of keys ) {
55
- if ( ! current [ k ] ) current [ k ] = { } ;
56
- current = current [ k ] ;
57
- }
29
+ export function buildObjectFromFlattenedObject (
30
+ flattenedObject : Record < string , any > ,
31
+ ) : DeepObject {
32
+ return Object . entries ( flattenedObject ) . reduce ( ( acc , [ key , value ] ) => {
33
+ const keys = key . split ( '.' ) ;
34
+ let lastKey = keys . pop ( ) ;
35
+ if ( ! lastKey ) return acc ;
36
+
37
+ let current = acc ;
38
+ for ( const k of keys ) {
39
+ if ( ! current [ k ] ) current [ k ] = { } ;
40
+ current = current [ k ] ;
41
+ }
58
42
59
- current [ lastKey ] = value ;
60
- return acc ;
61
- } , { } as DeepObject ) ;
43
+ current [ lastKey ] = value ;
44
+ return acc ;
45
+ } , { } as DeepObject ) ;
62
46
}
0 commit comments