Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/drag13/whendo
Small function that can be used instead if-then statement in functional style programming with JavaScript
https://github.com/drag13/whendo
conditional-statements development functional-programming ifthen javascript
Last synced: about 2 months ago
JSON representation
Small function that can be used instead if-then statement in functional style programming with JavaScript
- Host: GitHub
- URL: https://github.com/drag13/whendo
- Owner: Drag13
- License: mit
- Created: 2018-02-05T12:15:10.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T02:45:30.000Z (almost 2 years ago)
- Last Synced: 2024-12-08T21:43:32.080Z (about 2 months ago)
- Topics: conditional-statements, development, functional-programming, ifthen, javascript
- Language: JavaScript
- Homepage:
- Size: 146 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WhenDo
[![Coverage Status](https://coveralls.io/repos/github/Drag13/WhenDo/badge.svg?branch=coverage)](https://coveralls.io/github/Drag13/WhenDo?branch=coverage)
[![Build Status](https://travis-ci.org/Drag13/WhenDo.svg?branch=master)](https://travis-ci.org/Drag13/WhenDo)
[![npm](https://img.shields.io/npm/dt/@drag13/when-do.svg)](https://github.com/Drag13/WhenDo)
[![TypeSCript](https://img.shields.io/badge/TypeScript-Ready-brightgreen.svg)](https://github.com/Drag13/WhenDo)
[![GitHub license](https://img.shields.io/github/license/Drag13/WhenDo.svg)](https://github.com/Drag13/WhenDo/blob/master/LICENSE)## Description
Small function that can be used instead of 'if-then' statement in functional style programming with JavaScript. Takes a predicate and one or two handlers. Returns new function that will check the predicate and execute a true/false handler.
No external dependencies, uses ES6 inside, TypeScript ready.
## Installation
`npm install @drag13/when-do`
## Usage
Usage is simple: WhenDo function returns a new function that will be executed depending on a predicate. You can pass params and expect return result.
``` javascript
const wd = require('@drag13/when-do');
const myComposedFunction = wd(() => true, (name) => `hello ${name}`);
console.assert(myComposedFunction('mate') === 'hello mate');
```If you pass a predicate as a function, it will not be invoked until composed function call is executed.
``` javascript
const wd = require('@drag13/when-do');
const log = console.log;
const demo = wd(()=> { log('predicate calculated'); return true;},
()=> log('trueFunction executed'));
log('start');
demo();
```Result will be
`start`
`predicate calculated`
`trueFunction executed`
## React
If you want to have if-then-else statement inside react - try this example
```jsx
import iftrue from '@drag13/when-do';render() {
return (
{
iftrue(this.state.counter % 2,
() =>Odd,
() =>Even)()
}
this.setState({ counter: this.state.counter + 1 })}>CLICK
);
}```
*Tip:* You can name WhenDo function whatever you want, just change name inside the import!
```javascript
import ifthenelse from '@drag13/when-do';
import iftrue from '@drag13/when-do'```
## TypeScriptFeel free to use it with TypeScript
``` typescript
import * as whenDo from '@drag13/when-do';
const myComposedFunction = wd(() => true, (name) => `hello ${name}`);
console.assert(myComposedFunction('mate') === 'hello mate');
```## Tests
`npm test`
## Contributing
Any bug fixing is appreciated. If you want to add new functionality - you're welcome. But KISS it please.