1
- import { set } from 'lodash' ;
2
-
3
1
const aggregator = ( paths : any , object : any ) => {
4
2
return paths . reduce ( ( delta : any , path : any ) => {
5
3
return set ( delta , path , get ( object , path ) ) ;
@@ -49,7 +47,20 @@ function isString(value: any): value is string {
49
47
function isFunction ( value : any ) : value is ( ...args : any [ ] ) => any {
50
48
return typeof value === 'function' ;
51
49
}
50
+ function set ( object : object , path : string , value : any ) {
51
+ path = path . replace ( / \[ ( \w + ) \] / g, '.$1' ) ; // convert indexes to properties
52
+ path = path . replace ( / ^ \. / , '' ) ; // strip a leading dot
53
+ const paths = path . split ( '.' ) ;
54
+ const lastProperty = paths . pop ( ) ;
55
+ const finalValue = paths . reduceRight (
56
+ ( finalObject , prop ) => {
57
+ return { [ prop ] : finalObject } ;
58
+ } ,
59
+ { [ lastProperty ] : value }
60
+ ) ;
52
61
62
+ return { ...object , ...finalValue } ;
63
+ }
53
64
function get ( object : object , path : string ) {
54
65
path = path . replace ( / \[ ( \w + ) \] / g, '.$1' ) ; // convert indexes to properties
55
66
path = path . replace ( / ^ \. / , '' ) ; // strip a leading dot
@@ -70,13 +81,13 @@ function zipObject(props: string[], values: any[]) {
70
81
return { ...prev , [ prop ] : values [ i ] } ;
71
82
} , { } ) ;
72
83
}
73
-
84
+ export type Many < T > = T | T [ ] ;
74
85
export interface Schema {
75
86
[ targetProperty : string ] :
76
87
| string
77
- | ( ( iteratee : object | object [ ] , source : object | object [ ] , target : any ) => any )
88
+ | ( ( iteratee : Many < object > , source : Many < object > , target : any ) => any )
78
89
| string [ ]
79
- | { path : string | string [ ] ; fn : ( fieldValue : object | object [ ] , items : object | object [ ] ) => any } ;
90
+ | { path : string | string [ ] ; fn : ( fieldValue : Many < object > , items : Many < object > ) => any } ;
80
91
}
81
92
82
93
/**
0 commit comments