diff --git a/classes/switchcontext.ts b/classes/switchcontext.ts new file mode 100644 index 0000000..7dcfe27 --- /dev/null +++ b/classes/switchcontext.ts @@ -0,0 +1,27 @@ +class SwitchContext { + private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = []; + + addCase(match: any, handler: () => void): void { + this.Cases.push({ Match: match, Handler: handler }); + } + + addDefault(handler: () => void): void { + this.Cases.push({ Match: null, Handler: handler, IsDefault: true }); + } + + execute(value: any): void { + let matched = false; + for (let cb of this.Cases) { + if (cb.Match === value || cb.Match == value) { + cb.Handler(); + matched = true; + break; + } + } + if (!matched) { + let defaultCase = this.Cases.find(cb => cb.IsDefault); + if (defaultCase) defaultCase.Handler(); + } + this.Cases = []; // clear after execution + } +}