@@ -19,17 +19,17 @@ describe("ts-object-transformer", () => {
19
19
// - Values should be function taking SRC[key] and returning a new type NEW_TYPE[key] we want to capture in
20
20
// order to reference it in transformObject()'s result type
21
21
// Let's call this type COMPUTED_MAP
22
- { computed : ( obj ) => ` ${ obj ? ` ${ obj . aString } __ ${ obj . idempotentValue } ` : '' } ` , strlen : ( obj ) => obj . aString . length }
22
+ { year : ( obj ) => obj . date . substr ( 0 , 4 ) , strlen : ( obj ) => obj . aString . length }
23
23
) ;
24
24
// Result type (NEW_TYPE) should be a map with its keys being the union of SRC keys and COMPUTED_MAP keys with following rules :
25
25
// - If key exists only in SRC, then NEW_TYPE[key] = SRC[key]
26
26
// - If key is a computed key (belonging to COMPUTED_MAP), then NEW_TYPE[key] = ResultType<COMPUTED_MAP[key]>
27
27
// - Otherwise (key existing in FIELD_MAP), then NEW_TYPE[key] = ResultType<FIELD_MAP[key]>
28
28
// In this example, expecting
29
- // mappedResult = { date: Date.parse("2018-10-04T00:00:00+0200"), date2: new Date(1538604000000), aString: unescape("Hello%20World"), idempotentValue: "foo", computed : "Hello%20World__foo" }
30
- // .. meaning that expected type would be { date: number, date2: Date, aString: string, idempotentValue: string, computed : string }
29
+ // mappedResult = { date: Date.parse("2018-10-04T00:00:00+0200"), date2: new Date(1538604000000), aString: unescape("Hello%20World"), idempotentValue: "foo", year : "2018", strlen: 13 }
30
+ // .. meaning that expected type would be { date: number, date2: Date, aString: string, idempotentValue: string, year : string, strlen: number }
31
31
32
- expect ( Object . keys ( transformedResult ) ) . toEqual ( [ 'date' , 'date2' , 'aString' , 'idempotentValue' , 'computed ' , 'strlen' ] ) ;
32
+ expect ( Object . keys ( transformedResult ) ) . toEqual ( [ 'date' , 'date2' , 'aString' , 'idempotentValue' , 'year ' , 'strlen' ] ) ;
33
33
34
34
let v1 : number = transformedResult . date ; // number, expected
35
35
expect ( typeof v1 ) . toEqual ( 'number' ) ;
@@ -44,9 +44,9 @@ describe("ts-object-transformer", () => {
44
44
let v4 : string = transformedResult . idempotentValue ; // string, expected
45
45
expect ( typeof v4 ) . toEqual ( 'string' ) ;
46
46
expect ( v4 ) . toEqual ( 'foo' ) ;
47
- let v5 : string = transformedResult . computed ; // string, expected
47
+ let v5 : string = transformedResult . year ; // string, expected
48
48
expect ( typeof v5 ) . toEqual ( 'string' ) ;
49
- expect ( v5 ) . toEqual ( 'Hello%20World__foo ' ) ;
49
+ expect ( v5 ) . toEqual ( '2018 ' ) ;
50
50
let v6 : number = transformedResult . strlen ; // number, expected
51
51
expect ( typeof v6 ) . toEqual ( 'number' ) ;
52
52
expect ( v6 ) . toEqual ( 13 ) ;
@@ -75,17 +75,17 @@ describe("ts-object-transformer", () => {
75
75
let v4 : string = transformedResult . idempotentValue ; // string, expected
76
76
expect ( typeof v4 ) . toEqual ( 'string' ) ;
77
77
expect ( v4 ) . toEqual ( 'foo' ) ;
78
- // let v5: string = transformedResult.computed ; // doesn't compile, property 'computed ' doesn't exist on type
78
+ // let v5: string = transformedResult.year ; // doesn't compile, property 'year ' doesn't exist on type
79
79
} ) ;
80
80
81
81
function jsonMappingsWithFieldMappings ( fieldMapping : { } | undefined ) {
82
82
let transformedResult = transformObject (
83
83
{ date : "2018-10-04T00:00:00+0200" , date2 : 1538604000000 , aString : "Hello%20World" , idempotentValue : "foo" } ,
84
84
fieldMapping ,
85
- { computed : ( obj ) => ` ${ obj ? ` ${ obj . aString } __ ${ obj . idempotentValue } ` : '' } ` }
85
+ { year : ( obj ) => obj . date . substr ( 0 , 4 ) }
86
86
) ;
87
87
88
- expect ( Object . keys ( transformedResult ) ) . toEqual ( [ 'date' , 'date2' , 'aString' , 'idempotentValue' , 'computed ' ] ) ;
88
+ expect ( Object . keys ( transformedResult ) ) . toEqual ( [ 'date' , 'date2' , 'aString' , 'idempotentValue' , 'year ' ] ) ;
89
89
90
90
let v1 : string = transformedResult . date ; // string expected
91
91
expect ( typeof v1 ) . toEqual ( 'string' ) ;
@@ -99,9 +99,9 @@ describe("ts-object-transformer", () => {
99
99
let v4 : string = transformedResult . idempotentValue ; // string, expected
100
100
expect ( typeof v4 ) . toEqual ( 'string' ) ;
101
101
expect ( v4 ) . toEqual ( 'foo' ) ;
102
- let v5 : string = transformedResult . computed ; // string, expected
102
+ let v5 : string = transformedResult . year ; // string, expected
103
103
expect ( typeof v5 ) . toEqual ( 'string' ) ;
104
- expect ( v5 ) . toEqual ( 'Hello%20World__foo ' ) ;
104
+ expect ( v5 ) . toEqual ( '2018 ' ) ;
105
105
}
106
106
it ( "object transformation with no field mappings" , ( ) => {
107
107
jsonMappingsWithFieldMappings ( { } ) ;
@@ -130,7 +130,7 @@ describe("ts-object-transformer", () => {
130
130
let v4 : string = transformedResult . idempotentValue ; // string, expected
131
131
expect ( typeof v4 ) . toEqual ( 'string' ) ;
132
132
expect ( v4 ) . toEqual ( 'foo' ) ;
133
- // let v5: string = transformedResult.computed ; // doesn't compile, property 'computed ' doesn't exist on type
133
+ // let v5: string = transformedResult.year ; // doesn't compile, property 'year ' doesn't exist on type
134
134
} ) ;
135
135
136
136
interface NestedNode {
@@ -159,7 +159,7 @@ describe("ts-object-transformer", () => {
159
159
let transformedResult = transformObject (
160
160
{ date : "2018-10-04T00:00:00+0200" , date2 : 1538604000000 , aString : "Hello%20World" , idempotentValue : "foo" } ,
161
161
{ date : Date . parse , date2 : ( ts : number ) => new Date ( ts ) , aString : unescape } ,
162
- { computed : ( obj ) => ` ${ obj ? ` ${ obj . aString } __ ${ obj . idempotentValue } ` : '' } ` }
162
+ { year : ( obj ) => obj . date . substr ( 0 , 4 ) }
163
163
) ;
164
164
165
165
// Doesn't compile : Argument of type "blah" doesn't exist on type
@@ -173,7 +173,7 @@ describe("ts-object-transformer", () => {
173
173
log ( transformedResult . date2 ) ; // 2018-10-03T22:00:00.000Z (new Date(1538604000000))
174
174
log ( transformedResult . aString ) ; // Hello world
175
175
log ( transformedResult . idempotentValue ) ; // foo
176
- log ( transformedResult . computed ) ; // Hello%20World__foo
176
+ log ( transformedResult . year ) ; // 2018
177
177
178
178
let transformedResult2 = transformObject (
179
179
{ date : "2018-10-04T00:00:00+0200" , date2 : 1538604000000 , aString : "Hello%20World" , idempotentValue : "foo" } ,
@@ -188,13 +188,13 @@ describe("ts-object-transformer", () => {
188
188
let transformedResult3 = transformObject (
189
189
{ date : "2018-10-04T00:00:00+0200" , date2 : 1538604000000 , aString : "Hello%20World" , idempotentValue : "foo" } ,
190
190
undefined ,
191
- { computed : ( obj ) => ` ${ obj ? ` ${ obj . aString } __ ${ obj . idempotentValue } ` : '' } ` }
191
+ { year : ( obj ) => obj . date . substr ( 0 , 4 ) }
192
192
) ;
193
193
log ( transformedResult3 . date ) ; // 2018-10-04T00:00:00+0200
194
194
log ( transformedResult3 . date2 ) ; // 1538604000000
195
195
log ( transformedResult3 . aString ) ; // Hello%20world
196
196
log ( transformedResult3 . idempotentValue ) ; // foo
197
- log ( transformedResult3 . computed ) ; // Hello%20World__foo
197
+ log ( transformedResult3 . year ) ; // 2018
198
198
} ) ;
199
199
200
200
function log ( message ?: any , ...optionalParams : any [ ] ) : void {
@@ -203,4 +203,4 @@ describe("ts-object-transformer", () => {
203
203
console . log ( message , optionalParams ) ;
204
204
}
205
205
}
206
- } ) ;
206
+ } ) ;
0 commit comments