Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Created July 22, 2019 13:11
Show Gist options
  • Select an option

  • Save mildfuzz/4352dd942cc491d66d1bc49f44b35a47 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/4352dd942cc491d66d1bc49f44b35a47 to your computer and use it in GitHub Desktop.
ComposableMutator
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