-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2e33b4
commit da98370
Showing
8 changed files
with
617 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { commerce, random } from 'faker'; | ||
import { flow, isFunction, isUndefined, mapValues } from 'lodash'; | ||
|
||
// tslint:disable:no-invalid-this | ||
|
||
type Transformation<T> = (value: T) => T; | ||
type AddTransformation<T = any> = (transformation: Transformation<T>) => void; | ||
|
||
const resourceBuilder = (addTransformation: AddTransformation) => { | ||
return { | ||
toResource(id: string) { | ||
addTransformation( | ||
(currentValue: any) => ({ | ||
...currentValue, | ||
id | ||
}) | ||
); | ||
return this; | ||
} | ||
}; | ||
}; | ||
|
||
function createBlueprintBuilder<T>(blueprintFn: Blueprint<T> | BlueprintFactory<T>): (addTransformation: AddTransformation) => BlueprintBuilderMethods<T> { | ||
const blueprint = isFunction(blueprintFn) ? blueprintFn() : blueprintFn; | ||
return (addTransformation: AddTransformation) => generateBlueprintBuilderMethods(blueprint, addTransformation); | ||
} | ||
|
||
interface Product { | ||
name: string; | ||
price: number; | ||
} | ||
|
||
const productBlueprint: Blueprint<Product> = { | ||
name: () => commerce.productName(), | ||
price: () => random.number({ min: 0.01, max: 99.99, precision: 0.01 }) | ||
}; | ||
|
||
const productBuilder = createBlueprintBuilder(productBlueprint); | ||
|
||
type BuilderFactory<BT> = (addTransformation: AddTransformation) => Builder<BT>; | ||
|
||
type Builder<T> = { build(): any } & { | ||
[P in keyof T]: T[P] extends (...args: infer R) => any ? (...args: R) => Builder<T> : () => Builder<T> | ||
}; | ||
|
||
|
||
function combineBuilders<BT1, B1 extends BuilderFactory<BT1>, BT2, B2 extends BuilderFactory<BT2>>(builder1: B1, builder2: B2): Builder<ReturnType<B1> & ReturnType<B2>> { | ||
let transformations: Transformation<any>[] = []; | ||
|
||
const addTransformation = (transformationFn: Transformation<any>) => { | ||
transformations = [ | ||
...transformations, | ||
transformationFn | ||
]; | ||
}; | ||
|
||
return { | ||
...<any> builder1(addTransformation), | ||
...<any> builder2(addTransformation), | ||
build() { | ||
return flow(transformations)({}); | ||
} | ||
}; | ||
} | ||
|
||
const builder = combineBuilders( | ||
productBuilder, | ||
resourceBuilder | ||
); | ||
|
||
console.log(builder | ||
.fake() | ||
.name('Cars') | ||
.toResource('id') | ||
.build()); | ||
|
||
|
||
|
||
type Blueprint<T> = { | ||
[P in keyof T]: () => T[P] | ||
}; | ||
|
||
type BlueprintFactory<T> = () => Blueprint<T>; | ||
|
||
type BlueprintBuilderMethod<V, T> = (value: V) => BlueprintBuilderMethods<T>; | ||
|
||
type BlueprintBuilderMethods<T> = { fake(): void } & { | ||
[P in keyof T]: BlueprintBuilderMethod<T[P], T>; | ||
}; | ||
|
||
function fromBlueprint<T>(blueprint: Blueprint<T>, values?: Partial<T>): T { | ||
return { | ||
...<any> mapValues<Blueprint<T>, any>( | ||
blueprint, | ||
fn => fn() | ||
), | ||
...<any> (isUndefined(values) ? {} : values) | ||
}; | ||
} | ||
|
||
function generateBlueprintBuilderMethods<T>(blueprint: Blueprint<T>, addTransformation: AddTransformation): BlueprintBuilderMethods<T> { | ||
return { | ||
fake() { | ||
addTransformation(() => fromBlueprint(blueprint)); | ||
return this; | ||
}, | ||
...<any> mapValues( | ||
blueprint, | ||
(blueprintFn, prop) => generateBlueprintBuilderMethod(prop, addTransformation) | ||
) | ||
}; | ||
} | ||
|
||
function generateBlueprintBuilderMethod<T>(prop: keyof T, addTransformation: AddTransformation): BlueprintBuilderMethod<T[keyof T], T> { | ||
return function (this: BlueprintBuilderMethods<T>, value: T[keyof T]) { | ||
addTransformation((currentValue: T) => ({ | ||
...<any> currentValue, | ||
[prop]: value | ||
})); | ||
return this; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { flow } from 'lodash'; | ||
|
||
// tslint:disable:no-invalid-this | ||
|
||
type Transformation<CVT, RVT> = (value: CVT) => RVT; | ||
|
||
type AddTransformation<CVT, RVT = CVT> = (transformation: Transformation<CVT, RVT>) => void; | ||
|
||
type BuilderPluginMethods<BP> = { | ||
[M in keyof BP]: BP[M] extends (...args: infer R) => any ? (...args: R) => BuilderPluginMethods<BP> : (...args: any[]) => BuilderPluginMethods<BP> | ||
}; | ||
|
||
type BuilderPlugin<CVT = any, RVT = any> = (addTransformation: AddTransformation<CVT, RVT>) => BuilderPluginMethods<ReturnType<BuilderPlugin<CVT, RVT>>>; | ||
|
||
interface Builder<T> { | ||
addPlugin<CVT, RVT, BP extends BuilderPlugin<CVT, RVT>>(plugin: BP & BuilderPlugin<CVT, RVT>): ReturnType<BP> & Builder<T & RVT> & this; | ||
build(): T; | ||
} | ||
|
||
type BuilderFactory<T = {}> = (initialValue?: T) => Builder<T>; | ||
|
||
/* ---------- */ | ||
|
||
export interface Resource { | ||
_id: string; | ||
} | ||
|
||
interface Product { | ||
name: string; | ||
price: number; | ||
} | ||
|
||
/* ---------- */ | ||
|
||
const resourceBuilderPlugin = (addTransformation: AddTransformation<any, Resource>) => ({ | ||
toResource(id: string) { | ||
addTransformation(currentValue => ({ | ||
...currentValue, | ||
_id: id | ||
})); | ||
return this; | ||
} | ||
}); | ||
|
||
const productBuilderPlugin = (addTransformation: AddTransformation<any, Product>) => ({ | ||
toProduct() { | ||
addTransformation(currentValue => ({ | ||
...currentValue, | ||
name: 'name', | ||
price: 1.23 | ||
})); | ||
return this; | ||
} | ||
}); | ||
|
||
const builder: BuilderFactory = (initialValue = {}) => { | ||
let transformations: Transformation<any, any>[] = []; | ||
|
||
const addTransformation = (transformationFn: Transformation<any, any>) => { | ||
transformations = [ | ||
...transformations, | ||
transformationFn | ||
]; | ||
}; | ||
|
||
return { | ||
addPlugin<CVT, RVT>(plugin: BuilderPlugin<CVT, RVT>) { | ||
return <any> { | ||
...this, | ||
...plugin(addTransformation) | ||
}; | ||
}, | ||
build() { | ||
return flow(transformations)(initialValue); | ||
} | ||
}; | ||
}; | ||
|
||
const productBuilder = builder() | ||
.addPlugin(resourceBuilderPlugin) | ||
.addPlugin(productBuilderPlugin); | ||
|
||
const product = productBuilder.build(); |
Oops, something went wrong.