instalation and import:
const patternMatcher = require('./src/pattern-matcher');
const match = patternMatcher.match;
const type = patternMatcher.type;
console.log(
match([2])
.case(type.array, val => val.map( x => x * x ))
.case(type.instance(Date), val => 'its date: ' + val)
.get()
);
console.log(
match(new Date())
.case(type.string, () => 'this is the string')
.case(type.instance(Date), val => 'its date: ' + val)
.get()
);
console.log(
match(null)
.case(type.string, str => str)
.case(type.null, () => 'Empty String')
.get()
);
console.log(
match('Hello, World')
.case(type.regex(/^.*\sHell$/), val => val + ' (hell matched)')
.case(type.regex(/^.*\sWorld$/), val => val + ' (world matched)')
.get()
);
console.log(
match(12345)
.case(type.number, val => val + ' is number')
.default(val => val + ' is not a number')
.get()
);
console.log(
match({ a: 1, b: 2 })
.case(type.custom(obj => obj.a > 1), () => 'some behavior 1')
.case(type.custom(obj => obj.b > 1), () => 'some behavior 2')
.case(type.custom(obj => obj.a === 0), () => 'some behavior 3')
.default(() => 'some behavior 4')
.get()
);