Options
All
  • Public
  • Public/Protected
  • All
Menu

@csvenke/compose-rules

Index

Core Type aliases

Core Variables

Extension Variables

Core Type aliases

Rule

Rule<T>: (...args: T) => boolean

A function that takes n arguments and returns true or false

since

v2.3.1

Type parameters

  • T: any[]

Type declaration

    • (...args: T): boolean
    • Parameters

      • Rest ...args: T

      Returns boolean

RuleComposer

RuleComposer: <T>(...rules: Rule<T>[]) => Rule<T>

A function that takes n Rules and composes them into one Rule

since

v3.1.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Core Variables

Const and

and: <T>(...rules: Rule<T>[]) => Rule<T> = makeRuleComposer((rules, args) => {const results = resolveRules(rules, args);return results.every(isTrue);})

Returns composer that returns true if all rules returns true

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

// Returns true if number is larger than one
const isLargerThanOne = n => n > 1;

// Returns true if number is less than ten
const isLessThanTen = n => n < 10;

// Returns true if all rules returns true
const isValidValue = and(isLargerThanOne, isLessThanTen);

console.log(isValidValue(11)); // false
console.log(isValidValue(5)); // true
since

v1.0.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Const not

not: <T>(...rules: Rule<T>[]) => Rule<T> = makeRuleComposer((rules, args) => {const results = resolveRules(rules, args, true);return results.every(isFalse);})

Returns composer that returns true if all rules returns false

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

// Returns true if name is John
const isNamedJohn = name => name === "John"

// Returns true if name is Jane
const isNamedJane = name => name === "Jane";

// Returns true if name is not John or Jane
const isValidName = not(isNamedJohn, isNamedJane);

console.log(isValidName("Billy")) // true
console.log(isValidName("John")) // false
console.log(isValidName("Jane")) // false
since

v1.1.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Const or

or: <T>(...rules: Rule<T>[]) => Rule<T> = makeRuleComposer((rules, args) => {const results = resolveRules(rules, args, true);return results.some(isTrue);})

Returns composer that returns true if some rules returns true

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

// Returns true if name is John
const isNamedJohn = name => name === "John"

// Returns true if name is Jane
const isNamedJane = name => name === "Jane";

// Returns true if name is John or Jane
const isValidName = or(isNamedJohn, isNamedJane);

console.log(isValidName("Billy")) // false
console.log(isValidName("John")) // true
console.log(isValidName("Jane")) // true
since

v1.1.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Extension Variables

Const nand

nand: <T>(...rules: Rule<T>[]) => Rule<T> = makeRuleComposer((rules, args) => {const rule = not(and(...rules));return rule(...args);})

Returns composer that returns false if all rules returns true

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

// Returns true if number is larger than one
const isLargerThanOne = n => n > 1;

// Returns true if number is less than ten
const isLessThanTen = n => n < 10;

// Returns false if all rules returns true
const isValidValue = nand(isLargerThanOne, isLessThanTen);

console.log(isValidValue(11)); // true
console.log(isValidValue(5)); // false
since

v2.2.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Const nor

nor: <T>(...rules: Rule<T>[]) => Rule<T> = makeRuleComposer((rules, args) => {const rule = not(or(...rules));return rule(...args);})

Returns composer that returns false if some rules returns true

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

// Returns true if name is John
const isNamedJohn = name => name === "John"

// Returns true if name is Jane
const isNamedJane = name => name === "Jane";

// Returns false if name is John or Jane
const isValidName = nor(isNamedJohn, isNamedJane);

console.log(isValidName("Billy")) // true
console.log(isValidName("John")) // false
console.log(isValidName("Jane")) // false
since

v2.3.0

Type declaration

    • Type parameters

      • T: any[]

      Parameters

      • Rest ...rules: Rule<T>[]

      Returns Rule<T>

Generated using TypeDoc