1.3.2 • Published 6 years ago
@quenk/match v1.3.2
Match
Runtime pattern matching for ECMAScript.
Installation
npm install --save @quenk/match
Usage
match
exports a single function match
as well as two classes UnMatchedResult
and MatchedResult
(See typescript for interface).
match
constructs a new UnMatchedResult
with the value provided, use the caseOf
to test the shape
of the value or the orElse
method for the default case.
Complete the pattern matching by calling the end()
method which will return the final value
(after applying any matching functions).
If none of the matches succeed an error will be thrown.
Example
import {match} from '@quenk/match';
class CLA {}
class CLB {}
class CLC {}
// result is 3
let result =
match(new CLC())
.caseOf(CLA, (v:CLA)=> 1)
.caseOf(CLB, (v:CLB)=> 2)
.caseOf(CLC, (v:CLC)=> 3)
.end();
// result is 24
let result =
match(12)
.caseOf(CLA, (v:CLA)=> 1)
.caseOf(CLB, (v:CLB)=> 2)
.caseOf(CLC, (v:CLC)=> 3)
.orElse((n:number)=> n * 2)
.end();
// throws an error
let result =
match(new Date())
.caseOf(Array, (n:Array)=> n.length)
.end();
For more information on the pattern matching process see the kindof library.
License
Apache-2.0 (C) Quenk Technologies Limited.