Created
July 22, 2019 13:11
-
-
Save mildfuzz/4352dd942cc491d66d1bc49f44b35a47 to your computer and use it in GitHub Desktop.
ComposableMutator
This file contains hidden or 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
| type ComposableMutatorFn<T> = (t: T) => T; | |
| export class ComposableMutator<T> { | |
| constructor(private mutators: ComposableMutatorFn<T>[], private data?: T) { | |
| this.mutators = mutators || [data => data]; | |
| } | |
| public addMutator(fn: ComposableMutatorFn<T>): ComposableMutator<T> { | |
| return this.dupe(fn); | |
| } | |
| public exec(data?: T): T { | |
| if (!data && !this.data) { | |
| throw new Error('No data set in ComposableMutator'); | |
| } | |
| return this.mutators.reduce((s, factory) => { | |
| return factory(s); | |
| }, data || this.data); | |
| } | |
| private dupe(fn: ComposableMutatorFn<T>): ComposableMutator<T> { | |
| return ComposableMutator.for<T>([...this.mutators, fn], this.data); | |
| } | |
| static for<T>(mutators?: ComposableMutatorFn<T>[], data?: T) { | |
| return new ComposableMutator<T>(mutators, data); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment