Typesafe pattern matching emulation for TypeScript and JavaScript.
classWhisky{constructor(publicbrand:string,publicage:number,publicorigin:string,publicprice:number){}}constdalwhinnie=newWhisky('Dalwhinnie',15,'Highlands',32);varresult=match<Whisky,string>(dalwhinnie).caseOf(w=>w.age>24&&w.age<=40,()=>'Epic!').caseOf(w=>w.age>14&&w.age<=24,()=>'Great').caseOf(({brand,age})=>age>9&&age<=14,()=>'Neat')// Destructuring.caseOf(w=>w.age<=9,()=>'Meh...')._(_=>'Legendary!')// Wildcard.resolve();result.should.be.equal('Great');If no wildcard value is provided and no match is provided MatchError is thrown. More examples are available in mocha.js testsuite (see below).
You need node.js. Clone repository and then:
$ cd TsPatternMatching && npm install && gulpNavigate to 'test/bin' and open index.html to see mocha tests.