@ajomuch92/when.js
v1.0.0
Published
A package to use as a switch with vitamins
Downloads
13
Maintainers
Readme
When.js
A simple and light package to use as a switch but with some vitamins. Inspired on the match function from PHP.
Installation
Install when.js:
npm install when.js
pnpm install when.js
yarn add when.js
Usage
import { createEvaluator, PredicateValue } from 'when.js'
const cases: PredicateValue<number | string | object, string>[] = [
[(x: number | string) => typeof x === 'number' && x > 10, 'greater than 10'],
['hello', 'greeting'],
[{ key: 'value' }, 'object match'],
[[1, 2, 3], 'array match'],
[5, 'number five']
];
const defaultCase = 'default value';
const evaluator = createEvaluator(cases, defaultCase);
console.log(evaluator(12)); // 'greater than 10'
console.log(evaluator('hello')); // 'greeting'
console.log(evaluator({ key: 'value' })); // 'object match'
console.log(evaluator([1, 2, 3])); // 'array match'
console.log(evaluator(5)); // 'number five'
console.log(evaluator(0)); // 'default value'
How does it work?
The createEvaluator function generates an evaluator function that takes a series of predicate-value pairs and an optional default case. When invoked with an input, the evaluator iterates through the predicates, applying each to the input. If a predicate is a function, it checks if the input satisfies the condition. If it's a value, it compares it directly to the input. The evaluator returns the corresponding value of the first true predicate. If no predicates match, it returns the value from the default case. This approach allows for flexible, reusable evaluation logic.
Parameters used on createEvaluator method
| Name | Type | Description | | --------- | --------- | --------- | | cases | PredicateValue<T, V>[] | Array of predicate-value to be evaluate on the returned method. | | defaultValue | any | Optional value returned when all cases are false. |