Skip to content

Commit 290e42d

Browse files
committedOct 8, 2018
fixed bug when having more than 2 computed properties (only first was considered), thanks gamebuster for spotting it
1 parent d43c45a commit 290e42d

3 files changed

+5
-4
lines changed
 

‎dist/ts-object-transformer.js

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function transformObject(src, fieldMap, computedMap) {
1616
for (let key in computedMap) {
1717
let transformer = computedMap[key];
1818
result[key] = transformer(src);
19-
return result;
2019
}
2120
}
2221
return result;

‎src/ts-object-transformer.spec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("ts-object-transformer", () => {
1919
// - Values should be function taking SRC[key] and returning a new type NEW_TYPE[key] we want to capture in
2020
// order to reference it in transformObject()'s result type
2121
// Let's call this type COMPUTED_MAP
22-
{ computed: (obj) => `${obj?`${obj.aString}__${obj.idempotentValue}`:''}` }
22+
{ computed: (obj) => `${obj?`${obj.aString}__${obj.idempotentValue}`:''}`, strlen: (obj) => obj.aString.length }
2323
);
2424
// Result type (NEW_TYPE) should be a map with its keys being the union of SRC keys and COMPUTED_MAP keys with following rules :
2525
// - If key exists only in SRC, then NEW_TYPE[key] = SRC[key]
@@ -29,7 +29,7 @@ describe("ts-object-transformer", () => {
2929
// mappedResult = { date: Date.parse("2018-10-04T00:00:00+0200"), date2: new Date(1538604000000), aString: unescape("Hello%20World"), idempotentValue: "foo", computed: "Hello%20World__foo" }
3030
// .. meaning that expected type would be { date: number, date2: Date, aString: string, idempotentValue: string, computed: string }
3131

32-
expect(Object.keys(transformedResult)).toEqual(['date', 'date2', 'aString', 'idempotentValue', 'computed']);
32+
expect(Object.keys(transformedResult)).toEqual(['date', 'date2', 'aString', 'idempotentValue', 'computed', 'strlen']);
3333

3434
let v1: number = transformedResult.date; // number, expected
3535
expect(typeof v1).toEqual('number');
@@ -47,6 +47,9 @@ describe("ts-object-transformer", () => {
4747
let v5: string = transformedResult.computed; // string, expected
4848
expect(typeof v5).toEqual('string');
4949
expect(v5).toEqual('Hello%20World__foo');
50+
let v6: number = transformedResult.strlen; // number, expected
51+
expect(typeof v6).toEqual('number');
52+
expect(v6).toEqual(13);
5053

5154
// transformedResult.blah // doesn't compile, Property 'blah' doesn't exist on type
5255
});

‎src/ts-object-transformer.ts

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export function transformObject<
3434
for(let key in computedMap) {
3535
let transformer = <(obj: SRC) => any>computedMap[<string>key];
3636
result[key] = transformer(src);
37-
return result;
3837
}
3938
}
4039

0 commit comments

Comments
 (0)