ifty
v1.2.0
Published
Matches through piped operations
Downloads
1
Readme
Ifty
Ifty makes statements easy to read and write.
Installation
npm install ifty
Implementation
import { match } from 'ifty'; // Sync
import { matchAsync } from 'ifty'; // Async
Usage
normal statement
const result = match(1)
.when(1, 'one')
.when(2, 'two')
.when(3, 'three')
.default('other')
.exec();
statement with in operator
const result = match(1)
.when({in: [1]}, 'one')
.default('other')
.exec();
statement with range operator
const result = match(1)
.when({range: [1, 3]}, 'one')
.default('other')
.exec();
statement with regex operator
const result = match('foo')
.when(/foo/, 'one')
.default('other')
.exec();
statement with function operator
const result = match(1)
.when((value) => value === 1, 'one')
.default('other')
.exec();
statement with starts with operator
const result = match('foo')
.when({startsWith: 'f'}, 'one')
.default('other')
.exec();
statement with ends with operator
const result = match('foo')
.when({endsWith: 'o'}, 'one')
.default('other')
.exec();
statement with contains operator
const result = match('foo')
.when({contains: 'oo'}, 'one')
.default('other')
.exec();
statement with deep operator
const result = match({foo: {bar: 'baz'}})
.when({deep: {foo: {bar: 'baz'}}}, 'one')
.default('other')
.exec();
statement with partial operator
const result = match({foo: {bar: 'baz'}, other: 'value'})
.when({partial: {foo: {bar: 'baz'}}}, 'one')
.default('other')
.exec();
statement with multiple operators
const result = match(1)
.when({in: [1]}, 'one')
.when({range: [1, 3]}, 'two')
.when(/foo/, 'three')
.when((value) => value === 1, 'four')
.default('other')
.exec();
statement with throw instead of else
const result = match(99)
.when({in: [1]}, 'one')
.when({range: [1, 3]}, 'two')
.when(/foo/, 'three')
.when((value) => value === 1, 'four')
.throw('Not found')
.exec();
async statement
const result = await matchAsync(1)
.when(Promise.resolve({in: [1]}), Promise.resolve('one'))
.default('other')
.exec();
async statement with throw instead of else
const result = await matchAsync(99)
.when(Promise.resolve({in: [1]}), Promise.resolve('one'))
.when(Promise.resolve({range: [1, 3]}), Promise.resolve('two'))
.when(Promise.resolve(/foo/), Promise.resolve('three'))
.when(Promise.resolve((value) => value === 1), Promise.resolve('four'))
.throw('Not found')
.exec();