https://github.com/tungv/brn
Functional flow control
https://github.com/tungv/brn
conditional functional if-else nodejs
Last synced: 11 months ago
JSON representation
Functional flow control
- Host: GitHub
- URL: https://github.com/tungv/brn
- Owner: tungv
- License: mit
- Created: 2017-12-06T07:13:09.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-06T13:24:22.000Z (almost 3 years ago)
- Last Synced: 2025-03-17T13:52:37.537Z (11 months ago)
- Topics: conditional, functional, if-else, nodejs
- Language: TypeScript
- Homepage: https://npm.im/brn
- Size: 1.04 MB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Strongly Typed Functional Branching
`brn` exports only one function that takes a `test`, a `left`, and a `right`
function and return a combined one that will run `left` if `test` returns truthy
and `right` otherwise
# Example
```js
const brn = require('brn');
const isOdd = x => x % 2;
const left = x => `${x} is odd`;
const right = x => `${x} is even`;
const fn = brn(isOdd, left, right);
console.log(fn(2)); // returns 2 is even
console.log(fn(1)); // returns 1 is odd
```
# TypeScript
```ts
import brn from 'brn'
const isOdd = (x: number) => x % 2;
const fn = brn(
isOdd,
x => `${x} is odd` as const,
x => `${x} is even` as const
);
expect(fn(2, 'two')).toBe('2 is even'); // returns 2 is even
expect(fn(1, 'one')).toBe('1 is odd'); // returns 1 is odd
```
In this example, parameter `x` in the "left" and "right" functions is typed as `number`.
`fn` also has return type of `` `${number} is even` | `${number} is odd` ``
```ts
// another example
function always(x: X) {
return () => x;
}
const fn = brn(
(x: number) => x >= 10,
always(`double digit`),
brn(
x => x < 0,
always('negative'),
always('single digit')
)
)
```
In this example, return type of `fn` is one of `"single digit"`, `"double digit"` or `"negative"`.