barbero
v1.3.0
Published
A lightweight utility library for Boolean evaluations, semantic string parsing, comparisons, and predicate utilities.
Maintainers
Readme
Barbero
Barbero is a lightweight JavaScript utility library providing tools for Boolean evaluations, semantic string parsing, 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.
- Semantic Parsing: Recognise boolean meaning in strings and numbers —
'yes','enabled','on',1, and many more. - TypeScript-ready: Ships a full declaration file for first-class TypeScript support, generated from JSDoc annotations.
- 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 barberoMigrating from boolean?
If you're migrating from the deprecated boolean package, Barbero is a drop-in successor — same behaviour, same zero-dependency philosophy, plus ESM support and stricter TypeScript declarations. See the migration guide for the full import change, function mapping, and behavioural comparison.
Getting Started
Barbero ships three formats to suit any environment.
ES Modules (recommended)
import { isTruthy, parseBoolean } from 'barbero';Tree-shakable — only the functions you import are included in your bundle.
CommonJS (Node.js)
const { isTruthy, parseBoolean } = require('barbero');Default import
import Barbero from 'barbero';
Barbero.isTruthy(1); // true
Barbero.parseBoolean('yes'); // trueBrowser (UMD via script tag)
<script src="node_modules/barbero/dist/index.umd.js"></script>
<script>
Barbero.isTruthy(1); // true
Barbero.parseBoolean('yes'); // true
</script>Usage
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); // falseBoolean Parsing
| Function | Description |
|----------|-------------|
| parseBoolean(value) | Returns true if the value is a recognised truthy representation ('yes', 'true', 'on', '1', 1, etc.); false for everything else |
| isBooleanable(value) | Returns true if the value is any recognised boolean representation — either truthy or falsy |
| isTruthyString(value) | Returns true if the value is one of the recognised truthy semantic representations |
| isFalseyString(value) | Returns true if the value is one of the recognised falsy semantic representations |
parseBoolean(value)
Returns true if the value is a recognised truthy semantic representation. Direct replacement for the deprecated boolean() function.
parseBoolean('yes'); // true
parseBoolean('enabled'); // true
parseBoolean(1); // true
parseBoolean('no'); // false
parseBoolean('maybe'); // falseisBooleanable(value)
Returns true if the value is any recognised boolean representation — either truthy or falsy. Note that 'no', 'false', '0', etc. are booleanable even though they represent false.
isBooleanable('yes'); // true
isBooleanable('no'); // true — 'no' is booleanable even though it's falsy
isBooleanable('maybe'); // false
isBooleanable(null); // falseisTruthyString(value)
Returns true if the value is one of the recognised truthy semantic representations. Complements isTruthy for string/number input forms.
isTruthyString('yes'); // true
isTruthyString('enabled'); // true
isTruthyString('no'); // false
isTruthyString(1); // trueisFalseyString(value)
Returns true if the value is one of the recognised falsy semantic representations. Complements isFalsey for string/number input forms.
isFalseyString('no'); // true
isFalseyString('disabled'); // true
isFalseyString('yes'); // false
isFalseyString(0); // trueString Content Functions
| Function | Description |
|----------|-------------|
| isNumericString(value) | Returns true if the value is a non-empty string that converts to a valid number |
| isAlpha(value) | Returns true if the string contains only ASCII letters |
| isAlphanumeric(value) | Returns true if the string contains only ASCII letters and digits |
| isUpperCase(value) | Returns true if the string is non-empty and all characters are upper case |
| isLowerCase(value) | Returns true if the string is non-empty and all characters are lower case |
| isPalindrome(value) | Returns true if the string reads the same forwards and backwards (case-sensitive) |
| isBlankString(value) | Returns true if the string is empty or whitespace-only |
| stringStartsWith(value, prefix) | Returns true if the string begins with the given prefix |
| stringEndsWith(value, suffix) | Returns true if the string ends with the given suffix |
| stringIncludes(value, substring) | Returns true if the string contains the given substring |
isNumericString(value)
Returns true if the value is a non-empty string that converts to a valid number.
isNumericString('42'); // true
isNumericString('3.14'); // true
isNumericString('abc'); // false
isNumericString(''); // falseisAlpha(value)
Returns true if the string contains only ASCII letters (a-z, A-Z). Empty string returns false.
isAlpha('hello'); // true
isAlpha('hello1'); // false
isAlpha(''); // falseisAlphanumeric(value)
Returns true if the string contains only ASCII letters and digits. Empty string returns false.
isAlphanumeric('hello1'); // true
isAlphanumeric('hello!'); // false
isAlphanumeric(''); // falseisUpperCase(value)
Returns true if the string is non-empty and all characters are upper case. Strings with no alphabetical characters (e.g. '123') return true.
isUpperCase('HELLO'); // true
isUpperCase('123'); // true — no lower-case characters present
isUpperCase('Hello'); // false
isUpperCase(''); // falseisLowerCase(value)
Returns true if the string is non-empty and all characters are lower case. Strings with no alphabetical characters (e.g. '123') return true.
isLowerCase('hello'); // true
isLowerCase('123'); // true — no upper-case characters present
isLowerCase('Hello'); // false
isLowerCase(''); // falseisPalindrome(value)
Returns true if the string reads the same forwards and backwards. Comparison is case-sensitive. Empty string returns true.
isPalindrome('racecar'); // true
isPalindrome(''); // true — empty string is vacuously a palindrome
isPalindrome('Racecar'); // false — case-sensitive
isPalindrome('hello'); // falseisBlankString(value)
Returns true if the string is empty or contains only whitespace.
isBlankString(''); // true
isBlankString(' '); // true
isBlankString('hello'); // falsestringStartsWith(value, prefix)
Returns true if the string begins with the given prefix. Both arguments must be strings.
stringStartsWith('hello', 'hel'); // true
stringStartsWith('hello', ''); // true — empty prefix matches everything
stringStartsWith('hello', 'world'); // falsestringEndsWith(value, suffix)
Returns true if the string ends with the given suffix. Both arguments must be strings.
stringEndsWith('hello', 'llo'); // true
stringEndsWith('hello', ''); // true — empty suffix matches everything
stringEndsWith('hello', 'world'); // falsestringIncludes(value, substring)
Returns true if the string contains the given substring. Both arguments must be strings.
stringIncludes('hello', 'ell'); // true
stringIncludes('hello', ''); // true — empty substring matches everything
stringIncludes('hello', 'xyz'); // 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)); // falseDocumentation
- Overview — quick start, module summary, installation
- Architecture — build system, module structure, design decisions
- API Reference — full per-function documentation for every module
- Migrating from
boolean— drop-in replacement guide forbooleanpackage users - Contributing — adding functions, testing, publishing
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.
