README
Solid Choice
Pattern match execution for JavaScript
Install
$ npm i solid-choice
Import
Browser
<!-- Instaled: --><script src="node_modules/solid-choice/src/index.js"></script>
<!-- CDN(unpkg): --><script src="https://unpkg.com/solid-choice"></script>
<script>
choose([]);
</script>
CommonJS
const choose = require('solid-choice');
ES6 Modules
import choose from 'solid-choice';
Usage
import choose from 'solid-choice';
const choice = choose([
[{ object: { value: 'match' } }, object => 'object match'],
[string => string === 'valid', string => 'validation function match'],
[{ valid: v => v === 'valid', str: 'str' }, object => 'multiple type match']
]);
choice({ object: { value: 'match' } }); // 'object match'
choice('valid'); // 'validation function match'
choice({ valid: 'valid', str: 'str' }); // 'multiple type match'
choice(3); // 'is match'
choice('invalid'); // undefined
Helpers
import choose, {
is,
type,
empty,
any,
not,
and,
or
} from 'solid-choice';
const choice = choose([
[ or([ is(Number), is(String) ]), () => 'is number or string' ],
[ and([ type('object'), empty() ]), () => 'is null' ],
[ not(type('function')), () => 'not function' ],
[ any(), () => 'any non matched value' ]
]);
choice(1);// 'is number or string'
choice(null);// 'is null'
choice({});// 'not function'
choice(() => {});// 'any non matched value'
Where
import choose from 'solid-choice';
const choice = choose();
choice.where({ fromWhere: true }, () => 'from where');
choice({ fromWhere: true });// 'from where'
Default
import choose, { is } from 'solid-choice';
const choice = choose([ [is(Number), () => 'is number'] ]);
choice.def(() => 'last resource');
choice(3);// 'is number'
choice('');// 'last resource'