(TypeScript) typeswitcher, like switch, but with constructor based cases and expressions with inferred types
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
/** | |
* Usage: | |
typeSwitch(someInstance, defaultvalue) | |
.when(MyClass, i => result) | |
.when(OtherClass, i => otherResult) | |
.get() | |
In the expressions, the type of the 'i' param should be inferred correctly, see the attribute default value checks | |
*/ | |
export function typeSwitch<T, R>(instance: T, defaultValue?: R): TypeSwitcher<T, R> { | |
return new TypeSwitcher<T, R>(instance, defaultValue); | |
} | |
interface ITypeSwitchEntry<T, R> { | |
type: new (...args: any[]) => T; | |
expr: (instance: T) => R; | |
} | |
interface IConstructor<T> { | |
new (...args): T; | |
} | |
class TypeSwitcher<T, R> { | |
private cases: ITypeSwitchEntry<T, R>[] = []; | |
constructor(private instance: T, private _defaultValue?: R) { } | |
when<Z extends T>(clazz: IConstructor<Z>, expr: (instance: Z) => R): TypeSwitcher<T, R> { | |
this.cases.push({ type: clazz, expr: expr }); | |
return this; | |
} | |
get(): R { | |
for (var i = 0, l = this.cases.length; i < l; i++) | |
if (this.instance instanceof this.cases[i].type) | |
return this.cases[i].expr(this.instance); | |
if (this._defaultValue !== undefined) | |
return this._defaultValue; | |
throw new Error("Unsupported type: " + this.instance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment