barbero
v1.1.0
Published
A lightweight utility library for Boolean logic.
Downloads
179
Maintainers
Readme
Barbero
Barbero is a lightweight JavaScript utility library providing tools for Boolean evaluations, comparisons, logical operations, and predicate utilities. Simplify your true/false logic with this modern, easy-to-use toolkit.
Features
- Lightweight: Small and efficient, designed to integrate seamlessly with your projects.
- Boolean Utilities: Perform truthy/falsey checks, comparisons, validations, and predicate compositions.
- Easy to Use: Clear and intuitive functions for common tasks.
- Tree-shakable: Import only what you need to minimize bundle size.
Installation
Install Barbero via NPM:
npm install barberoOr using Yarn:
yarn add barberoUsage
Import and use individual functions or the entire library:
Example 1: Truthy/Falsey Checks
import { isTruthy, isFalsey } from 'barbero';
console.log(isTruthy(1)); // true
console.log(isFalsey(0)); // trueExample 2: Null/Undefined/Empty Checks
import { isNullOrUndefined, isEmptyArray } from 'barbero';
console.log(isNullOrUndefined(null)); // true
console.log(isEmptyArray([])); // true
console.log(isEmptyArray([1, 2])); // falseExample 3: Comparisons
import { isEqual, isStrictEqual } from 'barbero';
console.log(isEqual({ a: 1 }, { a: 1 })); // true
console.log(isStrictEqual(1, '1')); // falseExample 4: Logical Operations
import { xor, nand } from 'barbero';
console.log(xor(true, false)); // true
console.log(nand(true, true)); // falseExample 5: Predicate Utilities
import { allPass, anyPass, nonePass, composePredicates } from 'barbero';
const isPositive = (x) => x > 0;
const isEven = (x) => x % 2 === 0;
const isPositiveAndEven = allPass(isPositive, isEven);
console.log(isPositiveAndEven(4)); // true
console.log(isPositiveAndEven(-2)); // false
const isPositiveOrEven = anyPass(isPositive, isEven);
console.log(isPositiveOrEven(-2)); // true
const isNeitherPositiveNorEven = nonePass(isPositive, isEven);
console.log(isNeitherPositiveNorEven(-3)); // true
const customCheck = composePredicates(isPositive, isEven);
console.log(customCheck(4)); // true
console.log(customCheck(3)); // falseAPI
Core Functions
isTruthy(value)
Returns true if the value is truthy.
isTruthy(1); // true
isTruthy(0); // falseisFalsey(value)
Returns true if the value is falsey.
isFalsey(0); // true
isFalsey('hello'); // falseValidation Functions
isNullOrUndefined(value)
Returns true if the value is null or undefined.
isNullOrUndefined(null); // true
isNullOrUndefined(undefined); // true
isNullOrUndefined(''); // falseisUndefinedNullOrEmpty(value)
Returns true if the value is undefined, null, NaN, an empty string (including whitespace-only), an empty array, or an empty plain object.
isUndefinedNullOrEmpty(null); // true
isUndefinedNullOrEmpty(''); // true
isUndefinedNullOrEmpty([]); // true
isUndefinedNullOrEmpty({}); // true
isUndefinedNullOrEmpty(0); // false
isUndefinedNullOrEmpty(false); // falseisEmptyArray(array)
Returns true if the array is empty.
isEmptyArray([]); // true
isEmptyArray([1]); // falseisEmptyObject(object)
Returns true if the object has no keys.
isEmptyObject({}); // true
isEmptyObject({ a: 1 }); // falseisArray(value)
Returns true if the value is an array.
isArray([1, 2, 3]); // true
isArray('hello'); // falseisObject(value)
Returns true if the value is a non-null, non-array object.
isObject({ key: 'value' }); // true
isObject([1, 2, 3]); // false
isObject(null); // falseisFunction(value)
Returns true if the value is a function.
isFunction(() => {}); // true
isFunction(123); // falseisDate(value)
Returns true if the value is a Date instance.
isDate(new Date()); // true
isDate('2026-01-01'); // falseisRegExp(value)
Returns true if the value is a regular expression.
isRegExp(/abc/); // true
isRegExp('abc'); // falseType System Functions
isNumber(value)
Returns true if the value has type 'number' (including NaN).
isNumber(42); // true
isNumber(NaN); // true
isNumber('42'); // falseisString(value)
Returns true for primitive strings. Returns false for boxed String objects.
isString('hello'); // true
isString(new String('hi')); // falseisBoolean(value)
Returns true for primitive booleans. Returns false for boxed Boolean objects.
isBoolean(true); // true
isBoolean(new Boolean(false)); // falseisSymbol(value)
Returns true for Symbol values.
isSymbol(Symbol()); // true
isSymbol('symbol'); // falseisBigInt(value)
Returns true for BigInt values.
isBigInt(9n); // true
isBigInt(BigInt(9)); // true
isBigInt(9); // falseisPrimitive(value)
Returns true for all primitive types including null and undefined.
isPrimitive(1); // true
isPrimitive(null); // true
isPrimitive(undefined); // true
isPrimitive({}); // falseisClassInstance(value)
Returns true for instances of non-Object constructors (e.g. Date, Map, custom classes).
isClassInstance(new Date()); // true
isClassInstance({}); // false
isClassInstance([]); // falseNumber / Math Functions
isInteger(value)
Returns true if the value is an integer. Does not coerce — strings and floats return false.
isInteger(4); // true
isInteger(4.5); // false
isInteger(NaN); // falseisFloat(value)
Returns true if the value is a finite number that is not an integer.
isFloat(4.5); // true
isFloat(4); // false
isFloat(Infinity); // falseisPositive(value)
Returns true if the value is strictly greater than zero. 0, -0, and NaN return false.
isPositive(1); // true
isPositive(0); // false
isPositive(NaN); // falseisNegative(value)
Returns true if the value is strictly less than zero. -0 returns false.
isNegative(-1); // true
isNegative(0); // false
isNegative(-0); // false — -0 < 0 is false in JavaScriptisZero(value)
Returns true if the value is 0 or -0.
isZero(0); // true
isZero(-0); // true
isZero(1); // falseisEven(value)
Returns true if the value is an even integer. Non-integers return false.
isEven(4); // true
isEven(3); // false
isEven(2.5); // falseisOdd(value)
Returns true if the value is an odd integer. Non-integers return false.
isOdd(3); // true
isOdd(4); // false
isOdd(3.1); // falseisInRange(value, min, max)
Returns true if the value is within [min, max], inclusive on both bounds.
isInRange(5, 1, 10); // true
isInRange(1, 1, 10); // true — inclusive
isInRange(11, 1, 10); // falseisDivisibleBy(value, divisor)
Returns true if value is an integer evenly divisible by divisor. Guards against division by zero and non-integer inputs.
isDivisibleBy(9, 3); // true
isDivisibleBy(9, 0); // false — division by zero guard
isDivisibleBy(9.5, 3); // false — non-integer valueisFiniteNumber(value)
Returns true if the value is a finite number. Returns false for NaN, Infinity, -Infinity, and non-numbers.
isFiniteNumber(42); // true
isFiniteNumber(Infinity); // false
isFiniteNumber(NaN); // falseisNaNValue(value)
Returns true if the value is exactly NaN. Uses Number.isNaN — does not coerce, so isNaNValue(undefined) returns false.
isNaNValue(NaN); // true
isNaNValue(undefined); // false — unlike global isNaN, no coercion
isNaNValue(42); // falseComparison Functions
isEqual(a, b)
Performs deep equality comparison between two values.
isEqual({ a: 1 }, { a: 1 }); // true
isEqual([1, 2], [1, 2]); // true
isEqual(1, '1'); // falseisStrictEqual(a, b)
Performs strict equality comparison (no type coercion).
isStrictEqual(1, 1); // true
isStrictEqual(1, '1'); // falseLogical Functions
xor(a, b)
Returns true if only one of the arguments is true.
xor(true, false); // true
xor(true, true); // falsenand(a, b)
Returns true if not both arguments are true.
nand(true, true); // false
nand(true, false); // truePredicate Utilities
allPass(...predicates)
Returns a function that checks if all predicates pass for the given input.
const isPositive = (x) => x > 0;
const isEven = (x) => x % 2 === 0;
const isPositiveAndEven = allPass(isPositive, isEven);
console.log(isPositiveAndEven(4)); // true
console.log(isPositiveAndEven(-2)); // falseanyPass(...predicates)
Returns a function that checks if any predicate passes for the given input.
const isPositiveOrEven = anyPass(isPositive, isEven);
console.log(isPositiveOrEven(-2)); // true
console.log(isPositiveOrEven(-3)); // falsenonePass(...predicates)
Returns a function that checks if no predicates pass for the given input.
const isNeitherPositiveNorEven = nonePass(isPositive, isEven);
console.log(isNeitherPositiveNorEven(-3)); // true
console.log(isNeitherPositiveNorEven(4)); // falsecomposePredicates(...predicates)
Composes multiple predicates into a single predicate function that tests them sequentially.
const customCheck = composePredicates(isPositive, isEven);
console.log(customCheck(4)); // true
console.log(customCheck(3)); // falseContributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
Barbero is licensed under the MIT License.
Acknowledgements
Inspired by the need for clear and simple Boolean utilities in JavaScript.
