@putout/plugin-maybe
Version:
πPutout plugin applies Maybe monad
145 lines (100 loc) β’ 2.38 kB
Markdown
# @putout/plugin-maybe [![NPM version][NPMIMGURL]][NPMURL]
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-maybe.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-maybe "npm"
π[**Putout**](https://github.com/coderaiser/putout) plugin helps with `maybe` monad.
## Install
```
npm i @putout/plugin-maybe -D
```
## Rules
- β
[array](#array);
- β
[empty-array](#empty-array);
- β
[fn](#fn);
- β
[noop](#noop);
- β
[declare](#declare);
## Config
```json
{
"rules": {
"maybe/array": "on",
"maybe/empty-array": "on",
"maybe/fn": "on",
"maybe/noop": "on",
"maybe/declare": "on"
}
}
```
## array
### β Example of incorrect code
```js
const {isArray} = Array;
const array = isArray(a) ? a : [a];
```
### β
Example of correct code
```js
const {isArray} = Array;
const maybeArray = (a) => isArray(a) ? a : [a];
const array = maybeArray(a);
```
## empty-array
### β Example of incorrect code
```js
const array = !a ? [] : a;
```
### β
Example of correct code
```js
const maybeArray = (a) => !a ? [] : a;
const array = maybeEmptyArray(a);
```
## fn
### β Example of incorrect code
```js
const isFn = (a) => typeof a === 'function';
const fn = isFn(a) ? a : () => {};
```
### β
Example of correct code
```js
const isFn = (a) => typeof a === 'function';
const noop = () => {};
const maybeFn = isFn(a) ? a : noop;
const fn = maybeFn(a);
```
## noop
### β Example of incorrect code
```js
const fn = f || (() => {});
```
### β
Example of correct code
```js
const noop = () => {};
const fn = fn || noop;
```
## declare
### β Example of incorrect code
When you not sure is `f` a function, but you want to use it as function anyways:
```js
const fn = maybeFn(f);
maybeCall(fn);
```
### β
Example of correct code
```js
const isFn = (a) => typeof a === 'function';
const noop = () => {};
const maybeFn = (a) => isFn(a) ? a : noop;
const maybeCall = (a, ...b) => isFn(a) && a(...b);
const fn = maybeFn(f);
maybeCall(fn);
```
When you not sure is `a` is an array or not, but you want to get first element of it.
### β Example of incorrect code
```js
const b = maybeFirst(a);
```
### β
Example of correct code
```js
const {isArray} = Array;
const maybeFirst = (a) => isArray(a) ? a[0] : a;
const b = maybeFirst(a);
```
## License
MIT