solid-choice
TypeScript icon, indicating that this package has built-in type declarations

1.3.3 • Public • Published

Solid Choice

NPM

Build Status Maintainability Test Coverage

Pattern match execution for JavaScript

  1. Install
  2. Import
  3. Usage
  4. Helpers
  5. Where

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'

Package Sidebar

Install

npm i solid-choice

Weekly Downloads

1

Version

1.3.3

License

MIT

Unpacked Size

8.54 kB

Total Files

5

Last publish

Collaborators

  • mateus-oli