fp-booleans
Version:
Utilities to apply boolean logic (not, and, or) to functions and higher-order functions. Tiny, tested and composable
43 lines (42 loc) • 1.86 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-unused-vars */
Object.defineProperty(exports, "__esModule", { value: true });
const index_js_1 = require("./index.js");
/* SOME EXAMPLE FUNCTIONS */
// Predicates
const isPositive = (n) => n > 0;
const isEven = (n) => n % 2 === 0;
const isEmptyString = (arg) => arg === '';
// Higher order functions
const is = (comparison) => (arg) => arg === comparison;
/* SAMPLES */
/* not() */
// on a boolean
const resultNotOnBoolean = (0, index_js_1.not)(true);
// on a predicate
const isNotPositive = (0, index_js_1.not)(isPositive);
const resultNotOnPredicate = isNotPositive(-1);
// on a predicate that comes from a partially-applied HoF
const isFive = is(5);
const isNotFive_onAPredicate = (0, index_js_1.not)(isFive);
const resultNotOnPartiallyAppliedHoF = isNotFive_onAPredicate(4);
// on a higher order function that returns a predicate
const isNot = (0, index_js_1.not)(is);
const isNotFive_onAHigherOrderFunction = isNot(5);
const resultNotOnHoF = isNotFive_onAHigherOrderFunction(4);
/* and() */
// on booleans
const resultAndOnBooleans = (0, index_js_1.and)(true, 1 > 0);
// on predicates
const isEvenAndPositive = (0, index_js_1.and)(isEven, isPositive);
const resultAndOnPredicates = isEvenAndPositive(4);
// when the arity or the types of the functions that we want to combine are different, it should produce a TS error
// const isEvenAndEmptyString = and(isEven, isEmptyString); // <- TS Error
/* or() */
// on booleans
const resultOrOnBooleans = (0, index_js_1.or)(true, 1 < 0);
// on predicates
const isEvenOrPositive = (0, index_js_1.or)(isEven, isPositive);
const resultOrOnPredicates = isEvenOrPositive(3);
// when the arity or the types of the functions that we want to combine are different, it should produce a TS error
// const isEvenOrEmptyString = or(isEven, isEmptyString); // <- TS Error