πPutout plugin applies Maybe monad
npm install @putout/plugin-maybe[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 plugin helps with maybe monad.
```
npm i @putout/plugin-maybe -D
- β
array;
- β
empty-array;
- β
fn;
- β
noop;
- β
declare;
`json`
{
"rules": {
"maybe/array": "on",
"maybe/empty-array": "on",
"maybe/fn": "on",
"maybe/noop": "on",
"maybe/declare": "on"
}
}
`js`
const {isArray} = Array;
const array = isArray(a) ? a : [a];
`js`
const {isArray} = Array;
const maybeArray = (a) => isArray(a) ? a : [a];
const array = maybeArray(a);
`js`
const array = !a ? [] : a;
`js`
const maybeArray = (a) => !a ? [] : a;
const array = maybeEmptyArray(a);
`js`
const isFn = (a) => typeof a === 'function';
const fn = isFn(a) ? a : () => {};
`js`
const isFn = (a) => typeof a === 'function';
const noop = () => {};
const maybeFn = isFn(a) ? a : noop;
const fn = maybeFn(a);
`js`
const fn = f || (() => {});
`js`
const noop = () => {};
const fn = fn || noop;
When you not sure is f a function, but you want to use it as function anyways:
`js
const fn = maybeFn(f);
maybeCall(fn);
`
`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.
`js`
const b = maybeFirst(a);
`js
const {isArray} = Array;
const maybeFirst = (a) => isArray(a) ? a[0] : a;
const b = maybeFirst(a);
``
MIT