@putout/plugin-maybe
🐊Putout plugin applies Maybe monad
Last updated a year ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @putout/plugin-maybe 
SYNC missed versions from official npm registry.

@putout/plugin-maybe NPM version

????Putout plugin helps with maybe monad.

Install

npm i @putout/plugin-maybe -D

Rules

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

Declares:

  • βœ… maybeArray;
  • βœ… maybeEmptyArray;
  • βœ… maybeArrayFrom;
  • βœ… maybeFn;
  • βœ… maybeFirst;
  • βœ… maybeCall;

❌ 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

Current Tags

  • 5.0.0                                ...           latest (2 months ago)

11 Versions

  • 5.0.0                                ...           2 months ago
  • 4.0.0                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.1.4                                ...           3 years ago
  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (10)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |