2
2
* @module morphism
3
3
*/
4
4
import { isString , get , isFunction , isObject , zipObject , isUndefined , assignInWith , aggregator } from './helpers' ;
5
-
6
- /**
7
- * A String path that indicates where to find the property in the source input
8
- *
9
- * @example
10
- * ```typescript
11
- *
12
- * const source = {
13
- * foo: 'baz',
14
- * bar: ['bar', 'foo'],
15
- * baz: {
16
- * qux: 'bazqux'
17
- * }
18
- * };
19
- * const schema = {
20
- * foo: 'foo', // Simple Projection
21
- * bazqux: 'baz.qux' // Grab a value from a nested property
22
- * };
23
- *
24
- * morphism(schema, source);
25
- * //=> { foo: 'baz', bazqux: 'bazqux' }
26
- * ```
27
- *
28
- */
29
- export type ActionString = string ;
30
5
export type ActionFunction = {
31
6
/**
32
7
* A Function invoked per iteration
@@ -55,6 +30,30 @@ export type ActionFunction = {
55
30
*/
56
31
( iteratee : any , source : any | any [ ] , target : any ) : any ;
57
32
} ;
33
+ /**
34
+ * A String path that indicates where to find the property in the source input
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ *
39
+ * const source = {
40
+ * foo: 'baz',
41
+ * bar: ['bar', 'foo'],
42
+ * baz: {
43
+ * qux: 'bazqux'
44
+ * }
45
+ * };
46
+ * const schema = {
47
+ * foo: 'foo', // Simple Projection
48
+ * bazqux: 'baz.qux' // Grab a value from a nested property
49
+ * };
50
+ *
51
+ * morphism(schema, source);
52
+ * //=> { foo: 'baz', bazqux: 'bazqux' }
53
+ * ```
54
+ *
55
+ */
56
+ export type ActionString = string ;
58
57
/**
59
58
* An Array of String that allows to perform a function over source property
60
59
*
@@ -140,12 +139,6 @@ export type Schema<Target> = {
140
139
[ destinationProperty in keyof Target ] ?: ActionString | ActionFunction | ActionAggregator | ActionSelector
141
140
} ;
142
141
143
- function transformValuesFromObject < TDestination , Source > (
144
- object : Source ,
145
- schema : Schema < TDestination > ,
146
- items : Source [ ] ,
147
- objectToCompute : TDestination
148
- ) : TDestination ;
149
142
/**
150
143
* Low Level transformer function.
151
144
* Take a plain object as input and transform its values using a specified schema.
@@ -154,7 +147,7 @@ function transformValuesFromObject<TDestination, Source>(
154
147
* @param {Array } items Items to be forwarded to Actions
155
148
* @param { } objectToCompute Created tranformed object of a given type
156
149
*/
157
- function transformValuesFromObject < TDestination , Source > (
150
+ function transformValuesFromObject < Source , TDestination > (
158
151
object : Source ,
159
152
schema : Schema < TDestination > ,
160
153
items : Source [ ] ,
@@ -224,7 +217,7 @@ function transformItems<T, TSchema extends Schema<T>>(
224
217
type : Constructable < T >
225
218
) : Mapper < { [ P in keyof TSchema ] : any } > ;
226
219
227
- function transformItems < T , TSchema extends Schema < T > > ( schema : TSchema , type ?: Constructable < T > ) {
220
+ function transformItems < T , TSchema extends Schema < T | { } > > ( schema : TSchema , type ?: Constructable < T > ) {
228
221
function mapper ( source : any ) : any {
229
222
if ( ! source ) {
230
223
return source ;
@@ -243,7 +236,7 @@ function transformItems<T, TSchema extends Schema<T>>(schema: TSchema, type?: Co
243
236
const object = source ;
244
237
if ( type ) {
245
238
const classObject = new type ( ) ;
246
- return transformValuesFromObject ( object , schema , [ object ] , classObject ) ;
239
+ return transformValuesFromObject < any , T > ( object , schema , [ object ] , classObject ) ;
247
240
} else {
248
241
const jsObject = { } ;
249
242
return transformValuesFromObject ( object , schema , [ object ] , jsObject ) ;
0 commit comments