@putout/plugin-maybe 
đPutout plugin helps with maybe
monad.
Install
npm i @putout/plugin-maybe -D
Rules
- â array;
- â empty-array;
- â fn;
- â noop;
- â declare;
Config
{
"rules": {
"maybe/array": "on",
"maybe/empty-array": "on",
"maybe/fn": "on",
"maybe/noop": "on",
"maybe/declare": "on"
}
}
array
â Example of incorrect code
const {isArray} = Array;
const array = isArray(a) ? a : [a];
â Example of correct code
const {isArray} = Array;
const maybeArray = (a) => isArray(a) ? a : [a];
const array = maybeArray(a);
empty-array
â Example of incorrect code
const array = !a ? [] : a;
â Example of correct code
const maybeArray = (a) => !a ? [] : a;
const array = maybeEmptyArray(a);
fn
â Example of incorrect code
const isFn = (a) => typeof a === 'function';
const fn = isFn(a) ? a : () => {};
â Example of correct code
const isFn = (a) => typeof a === 'function';
const noop = () => {};
const maybeFn = isFn(a) ? a : noop;
const fn = maybeFn(a);
noop
â Example of incorrect code
const fn = f || (() => {});
â Example of correct code
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:
const fn = maybeFn(f);
maybeCall(fn);
â Example of correct code
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
const b = maybeFirst(a);
â Example of correct code
const {isArray} = Array;
const maybeFirst = (a) => isArray(a) ? a[0] : a;
const b = maybeFirst(a);
License
MIT