1.3.3 • Published 6 years ago

solid-choice v1.3.3

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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'
1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago