Skip to content

follower90/pattern-matcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PatternMatcher

HOW TO INSTALL

instalation and import:

const patternMatcher = require('./src/pattern-matcher');

const match = patternMatcher.match;
const type = patternMatcher.type;

HOW TO USE

Match Array

console.log(
     match([2])
        .case(type.array, val => val.map( x => x * x ))
        .case(type.instance(Date), val => 'its date: ' + val)
        .get()
);

Match Date

console.log(
     match(new Date())
        .case(type.string, () => 'this is the string')
        .case(type.instance(Date), val => 'its date: ' + val)
        .get()
);

Match null

console.log(
     match(null)
        .case(type.string, str => str)
        .case(type.null, () => 'Empty String')
        .get()
);

Regex match

console.log(
     match('Hello, World')
        .case(type.regex(/^.*\sHell$/), val => val + ' (hell matched)')
        .case(type.regex(/^.*\sWorld$/), val => val + ' (world matched)')
        .get()
);

Match numbers

console.log(
     match(12345)
        .case(type.number, val => val + ' is number')
        .default(val => val + ' is not a number')
        .get()
);

Actions switch matcher

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()
);

About

experimental pattern matching for js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published