JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 51
  • Score
    100M100P100Q47827F
  • License MIT

Simple helper functions for writing composable business rules

Package Exports

  • @csvenke/compose-rules

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@csvenke/compose-rules) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm package min bundle size master workflow pull request workflow semantic release

@csvenke/compose-rules

Simple helper functions for writing composable business rules

Install

Using yarn

yarn add @csvenke/compose-rules

Using npm

npm install --save @csvenke/compose-rules

Usage

Try it out online with replit

import { and } from "@csvenke/compose-rules";

const isNumber = n => typeof n === "number";
const isLargerThanOne = n => n > 1;
const isLessThanTen = n => n < 10;

const isValidValue = and(isNumber, isLargerThanOne, isLessThanTen);

console.log(isValidValue(4)); // true
console.log(isValidValue(14)); // false

API

and

Returns a rule function that returns true if all rules are true.

Example

import { and } from "@csvenke/compose-rules";

const isLargerThanOne = n => n > 1;
const isLessThanTen = n => n < 10;

const hasValidValue = and(isLargerThanOne, isLessThanTen);

console.log(hasValidValue(11)); // false
console.log(hasValidValue(5)); // true

or

Returns a rule function that returns true if some rules are true.

Example

import { or } from "@csvenke/compose-rules";

const isNamedJohn = name => name === "John";
const isNamedJane = name => name === "Jane";

const hasValidName = or(isNamedJohn, isNamedJane);

console.log(hasValidName("Bill")); // false
console.log(hasValidName("Jane")); // true

not

Returns a rule function that returns true if all rules are false.

Example

import { not } from "@csvenke/compose-rules";

const isNamedJohn = name => name === "John";
const isNamedJane = name => name === "Jane";

const hasValidName = not(isNamedJohn, isNamedJane);

console.log(hasValidName("Bill")); // true
console.log(hasValidName("Jane")); // false
console.log(hasValidName("John")); // false

Development

Installing dependencies

yarn install

Building project

yarn build

Running tests

yarn test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style.

License

MIT