From 0665af2562e92428796a6f5590fbf6d6eab39e95 Mon Sep 17 00:00:00 2001 From: Yann Renaudin Date: Fri, 13 Dec 2019 19:03:17 -0500 Subject: [PATCH 1/2] fix: provide destination property to Selector Action to infer return type of fn --- src/types.ts | 62 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/src/types.ts b/src/types.ts index 09cb5a4..41223db 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from './morphism'; +import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from "./morphism"; /** * A structure-preserving object from a source data towards a target data. @@ -35,22 +35,38 @@ export type StrictSchema = { /** `destinationProperty` is the name of the property of the target object you want to produce */ [destinationProperty in keyof Target]: | ActionString - | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } + | { + ( + iteratee: Source, + source: Source[], + target: Target[destinationProperty] + ): Target[destinationProperty]; + } | ActionAggregator - | ActionSelector + | ActionSelector | StrictSchema; } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions }; export type Schema = { /** `destinationProperty` is the name of the property of the target object you want to produce */ [destinationProperty in keyof Target]?: | ActionString - | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } + | { + ( + iteratee: Source, + source: Source[], + target: Target[destinationProperty] + ): Target[destinationProperty]; + } | ActionAggregator - | ActionSelector + | ActionSelector | Schema; } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions }; -export type Actions = ActionFunction | ActionAggregator | ActionString | ActionSelector; +export type Actions = + | ActionFunction + | ActionAggregator + | ActionString + | ActionSelector; /** * @interface ActionFunction @@ -129,7 +145,9 @@ export type ActionString = string; // TODO: ActionString should support * //=> { fooAndBar: { foo: 'foo', bar: 'bar' } } * ``` */ -export type ActionAggregator = T extends object ? (keyof T)[] | string[] : string[]; +export type ActionAggregator = T extends object + ? (keyof T)[] | string[] + : string[]; /** * @interface ActionSelector * @typeparam Source Source/Input Type @@ -157,21 +175,41 @@ export type ActionAggregator = T extends object ? ( *``` * */ -export interface ActionSelector { +export interface ActionSelector< + Source = object, + Target = any, + TargetProperty extends keyof Target = any +> { path: ActionString | ActionAggregator; - fn: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R; + fn: ( + fieldValue: any, + object: Source, + items: Source, + objectToCompute: Target + ) => Target[TargetProperty]; } export interface Constructable { new (...args: any[]): T; } -export type SourceFromSchema = T extends StrictSchema | Schema ? U : never; -export type DestinationFromSchema = T extends StrictSchema | Schema ? U : never; +export type SourceFromSchema = T extends + | StrictSchema + | Schema + ? U + : never; +export type DestinationFromSchema = T extends + | StrictSchema + | Schema + ? U + : never; export type ResultItem = DestinationFromSchema; -export interface Mapper> { +export interface Mapper< + TSchema extends Schema | StrictSchema, + TResult = ResultItem +> { (data?: SourceFromSchema[] | null): TResult[]; (data?: SourceFromSchema | null): TResult; } From 1384bd9738bc14187e3c5ca28ef2968d1c167c19 Mon Sep 17 00:00:00 2001 From: Yann Renaudin Date: Fri, 13 Dec 2019 19:04:22 -0500 Subject: [PATCH 2/2] test: add UT to validate type validation on Action Selector fn function --- src/typescript.spec.ts | 155 +++++++++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 53 deletions(-) diff --git a/src/typescript.spec.ts b/src/typescript.spec.ts index 129eac4..5586621 100644 --- a/src/typescript.spec.ts +++ b/src/typescript.spec.ts @@ -1,47 +1,52 @@ -import Morphism, { morphism, StrictSchema, Schema, createSchema } from './morphism'; - -describe('Typescript', () => { - describe('Registry Type Checking', () => { - it('Should return a Mapper when using Register', () => { +import Morphism, { + morphism, + StrictSchema, + Schema, + createSchema +} from "./morphism"; + +describe("Typescript", () => { + describe("Registry Type Checking", () => { + it("Should return a Mapper when using Register", () => { class Foo { foo: string; } - const schema = { foo: 'bar' }; - const source = { bar: 'value' }; + const schema = { foo: "bar" }; + const source = { bar: "value" }; const mapper = Morphism.register(Foo, schema); - expect(mapper(source).foo).toEqual('value'); - expect(mapper([source][0]).foo).toEqual('value'); + expect(mapper(source).foo).toEqual("value"); + expect(mapper([source][0]).foo).toEqual("value"); }); }); - describe('Schema Type Checking', () => { - it('Should allow to type the Schema', () => { + describe("Schema Type Checking", () => { + it("Should allow to type the Schema", () => { interface IFoo { foo: string; bar: number; } - const schema: Schema = { foo: 'qux' }; - const source = { qux: 'foo' }; + const schema: Schema = { foo: "qux" }; + const source = { qux: "foo" }; const target = morphism(schema, source); expect(target.foo).toEqual(source.qux); }); - it('Should allow to use a strict Schema', () => { + it("Should allow to use a strict Schema", () => { interface IFoo { foo: string; bar: number; } - const schema: StrictSchema = { foo: 'qux', bar: () => 1 }; - const source = { qux: 'foo' }; + const schema: StrictSchema = { foo: "qux", bar: () => 1 }; + const source = { qux: "foo" }; const target = morphism(schema, source); expect(target.foo).toEqual(source.qux); expect(target.bar).toEqual(1); }); - it('should accept 2 generic parameters on StrictSchema', () => { + it("should accept 2 generic parameters on StrictSchema", () => { interface Source { inputA: string; inputB: string; @@ -53,32 +58,34 @@ describe('Typescript', () => { fooC: string; } const schema: StrictSchema = { - fooA: 'inputA', + fooA: "inputA", fooB: ({ inputB }) => inputB, - fooC: 'inputC' + fooC: "inputC" }; const mapper = morphism(schema); - expect(mapper({ inputA: 'test', inputB: 'test2', inputC: 'test3' })).toEqual({ - fooA: 'test', - fooB: 'test2', - fooC: 'test3' + expect( + mapper({ inputA: "test", inputB: "test2", inputC: "test3" }) + ).toEqual({ + fooA: "test", + fooB: "test2", + fooC: "test3" }); }); - it('should accept 2 generic parameters on Schema', () => { + it("should accept 2 generic parameters on Schema", () => { interface Source2 { inputA: string; } const schema: Schema<{ foo: string }, Source2> = { - foo: 'inputA' + foo: "inputA" }; - morphism(schema, { inputA: 'test' }); - morphism(schema, [{ inputA: '' }]); + morphism(schema, { inputA: "test" }); + morphism(schema, [{ inputA: "" }]); }); - it('should accept 2 generic parameters on Schema', () => { + it("should accept 2 generic parameters on Schema", () => { interface S { s1: string; } @@ -86,26 +93,26 @@ describe('Typescript', () => { d1: string; } const schema: StrictSchema = { - d1: 's1' + d1: "s1" }; - const a = morphism(schema)([{ s1: 'test' }]); + const a = morphism(schema)([{ s1: "test" }]); const itemA = a.shift(); expect(itemA).toBeDefined(); if (itemA) { itemA.d1; } - morphism(schema, { s1: 'teest' }).d1.toString(); - const b = morphism(schema, [{ s1: 'teest' }]); + morphism(schema, { s1: "teest" }).d1.toString(); + const b = morphism(schema, [{ s1: "teest" }]); const itemB = b.shift(); expect(itemB).toBeDefined(); if (itemB) { itemB.d1; } - morphism(schema, [{ s1: 'teest' }]); - morphism(schema, [{ s1: 'test' }]); + morphism(schema, [{ s1: "teest" }]); + morphism(schema, [{ s1: "test" }]); }); - it('should not fail with typescript', () => { + it("should not fail with typescript", () => { interface S { s1: string; } @@ -122,29 +129,45 @@ describe('Typescript', () => { namingIsHard: string; } - const a = morphism>({ namingIsHard: 'boring_api_field' }, [{ boring_api_field: 2 }]); + const a = morphism>( + { namingIsHard: "boring_api_field" }, + [{ boring_api_field: 2 }] + ); const itemA = a.pop(); expect(itemA).toBeDefined(); if (itemA) { itemA.namingIsHard; } - const b = morphism>({ namingIsHard: 'boring_api_field' }, { boring_api_field: 2 }); + const b = morphism>( + { namingIsHard: "boring_api_field" }, + { boring_api_field: 2 } + ); b.namingIsHard; - const c = morphism>({ namingIsHard: 'boring_api_field' }, [{ boring_api_field: 2 }]); + const c = morphism>( + { namingIsHard: "boring_api_field" }, + [{ boring_api_field: 2 }] + ); const itemC = c.pop(); expect(itemC).toBeDefined(); if (itemC) { itemC.namingIsHard; } - const d = morphism({ namingIsHard: 'boring_api_field' }, { boring_api_field: 2 }); + const d = morphism( + { namingIsHard: "boring_api_field" }, + { boring_api_field: 2 } + ); d.namingIsHard; - morphism({ namingIsHard: 'boring_api_field' }); - morphism>({ namingIsHard: 'boring_api_field' })({ boring_api_field: 2 }); - const e = morphism>({ namingIsHard: 'boring_api_field' })([{ boring_api_field: 2 }]); + morphism({ namingIsHard: "boring_api_field" }); + morphism>({ + namingIsHard: "boring_api_field" + })({ boring_api_field: 2 }); + const e = morphism>({ + namingIsHard: "boring_api_field" + })([{ boring_api_field: 2 }]); const itemE = e.pop(); expect(itemE).toBeDefined(); if (itemE) { @@ -162,7 +185,7 @@ describe('Typescript', () => { morphism>({ a: ({ _a }) => _a.toString() }); }); - it('shoud infer result type from source when a class is provided', () => { + it("shoud infer result type from source when a class is provided", () => { class Source { constructor(public id: number, public ugly_field: string) {} } @@ -171,13 +194,13 @@ describe('Typescript', () => { constructor(public id: number, public field: string) {} } - const source = [new Source(1, 'abc'), new Source(1, 'def')]; + const source = [new Source(1, "abc"), new Source(1, "def")]; const schema: StrictSchema = { - id: 'id', - field: 'ugly_field' + id: "id", + field: "ugly_field" }; - const expected = [new Destination(1, 'abc'), new Destination(1, 'def')]; + const expected = [new Destination(1, "abc"), new Destination(1, "def")]; const result = morphism(schema, source, Destination); result.forEach((item, idx) => { @@ -185,17 +208,20 @@ describe('Typescript', () => { }); }); - it('should accept union types as Target', () => { - const schema = createSchema<{ a: string } | { a: string; b: string }, { c: string }>({ + it("should accept union types as Target", () => { + const schema = createSchema< + { a: string } | { a: string; b: string }, + { c: string } + >({ a: ({ c }) => c }); - expect(morphism(schema, { c: 'result' }).a).toEqual('result'); + expect(morphism(schema, { c: "result" }).a).toEqual("result"); }); }); - describe('Morphism Function Type Checking', () => { - it('should infer target type from array input', () => { + describe("Morphism Function Type Checking", () => { + it("should infer target type from array input", () => { interface Source { ID: number; } @@ -206,9 +232,32 @@ describe('Typescript', () => { const rows: Array = [{ ID: 1234 }]; - const schema: StrictSchema = { id: 'ID' }; + const schema: StrictSchema = { id: "ID" }; expect(morphism(schema, rows)).toBeDefined(); expect(morphism(schema, rows)[0].id).toEqual(1234); }); }); + + describe("Selector Action", () => { + it("should match return type of fn with target property", () => { + interface Source { + foo: string; + } + + interface Target { + foo: number; + } + + const schema: StrictSchema = { + foo: { + path: "foo", + fn: val => { + return Number(val); + } + } + }; + const source: Source = { foo: "1" }; + expect(morphism(schema, source)).toEqual({ foo: 1 }); + }); + }); });