anyone

Anyone

Usage no npm install needed!

<script type="module">
  import anyone from 'https://cdn.skypack.dev/anyone';
</script>

README

anyone

Anyone contains a small group of functions that check whether a number of given expressions evaluate to be truthy based on the number of times a truthy value appears.

Anyone has four functions:

  • any - Checks that at least one of the supplied expressions evaluates to true.
  • one - Checks that only one of the supplied expressions evaluates to true (Mutual exclusion).
  • all - Checks that all of the supplied expressions evaluate to true.
  • none - Checks that none of the supplied expressions evaluates to true.

They all accept any amount of arguments, or no arguments at all. If a function is passed, it will be run and its value will be evaluated. Return value is always a boolean.

All functions other than one will short circuit when realizing the condition is not met.

Why use it

In most cases (other than one) you can do just fine using Array.prototype.some and Array.prototype.every, but I do this often enough that I thought I'd share it.

  • You can use these functions as conditionals:
if (one( var1, var2, var3 )) {
    // will reach here if ONLY ONE of the arguments is true
}
  • You can use these functions to pause execution of some code after a condition is met
all(
    validateInput1, // returns true
    validateInput2, // returns false
    validateInput3  // no need to run this, we already know our validation failed
);

Installation

npm i anyone
yarn add anyone

Usage Examples

import { any, all, none, one } from 'anyone';

any(
    someFunction, // evaluates to false
    1,
    someVar // truthy
);
// true

// --------

any(
    someFunction, // evaluates to false
    0,
    someVar // falsy
);
// false

// --------

one(
    someFunction, // evaluates to false
    0,
    someVar // truthy
);
// true

// --------

none(
    someFunction, // evaluates to false
    1,
    someVar // truthy
);
// false

// --------

none(
    someFunction, // evaluates to false
    0,
    someVar // falsy
);
// true

// --------

all(
    someFunction, // evaluates to false
    0,
    someVar // truthy
);
// false

// --------

all(
    someFunction, // evaluates to true
    1,
    someVar // truthy
);
// true

You can also import any of the functions directly. If you use more than one, you should opt import them together, as they share most of their code.

import any from 'anyone/any';
import one from 'anyone/one';
import all from 'anyone/all';
import none from 'anyone/none';