@putout/plugin-maybe

2.0.1 • Public • Published

@putout/plugin-maybe NPM version

🐊Putout plugin helps with maybe monad.

Install

npm i @putout/plugin-maybe -D

Rules

{
    "rules": {
        "maybe/array": "on",
        "maybe/empty-array": "on",
        "maybe/fn": "on",
        "maybe/noop": "on",
        "maybe/declare": "on"
    }
}

array

❌ Example of incorrect code

const array = isArray(a) ? a : [a];

✅ Example of correct code

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);

✅ Example of correct code

const isFn = (a) => typeof a === 'function';
const noop = () => {};
const maybeFn = isFn(a) ? a : noop;

const fn = maybeFn(f);

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

Package Sidebar

Install

npm i @putout/plugin-maybe

Weekly Downloads

3,443

Version

2.0.1

License

MIT

Unpacked Size

6.31 kB

Total Files

9

Last publish

Collaborators

  • coderaiser