booleanlab
Version:
A comprehensive TypeScript utility library for advanced boolean operations. Simplify your boolean logic with easy-to-use functions.
149 lines (148 loc) • 3.41 kB
JavaScript
/**
* Return `true` for truthy values
* @returns {boolean}
*/
export function trueValue() {
return true;
}
/**
* Return `false` for truthy values
* @returns {boolean}
*/
export function falseValue() {
return false;
}
/**
* Return `true` if both booleans are equal and `false` otherwise
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function isEqual(a, b) {
return a === b;
}
/**
* Return `true` if both booleans are not equal and `false` otherwise
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function isNotEqual(a, b) {
return a !== b;
}
/**
* Return the result of a logical AND operation between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function andOperation(a, b) {
return a && b;
}
/**
* Return the result of a logical OR operation between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function orOperation(a, b) {
return a || b;
}
/**
* Return the result of a logical XOR (exclusive OR) operation between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function xorOperation(a, b) {
return (a || b) && !(a && b);
}
/**
* Return the negation (inverse) of the boolean value
* @param {a: boolean}
* @returns {boolean}
*/
export function negate(a) {
return !a;
}
/**
* Check if a boolean value is exactly `true`
* @param {a: boolean}
* @returns {boolean}
*/
export function isTrue(a) {
return a === true;
}
/**
* Check if a boolean value is exactly `false`
* @param {a: boolean}
* @returns {boolean}
*/
export function isFalse(a) {
return a === false;
}
/**
* Convert a boolean to binary (1 for true, 0 for false)
* @param {a: boolean}
* @returns {number}
*/
export function toBinary(a) {
return a ? 1 : 0;
}
/**
* Convert any value to a boolean (falsey values become false, others become true)
* @param {a: any}
* @returns {boolean}
*/
export function toBoolean(a) {
return Boolean(a);
}
/**
* Return the result of a logical NAND operation (NOT AND) between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function nandOperation(a, b) {
return !(a && b);
}
/**
* Return the result of a logical NOR operation (NOT OR) between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function norOperation(a, b) {
return !(a || b);
}
/**
* Convert a boolean value to a string representation ("True" or "False")
* @param {value: boolean}
* @returns {string}
*/
export const toString = (value) => (value ? "True" : "False");
/**
* Check if a value is truthy (not null, undefined, false, 0, NaN, or '')
* @param {value: any}
* @returns {boolean}
*/
export function isTruthy(value) {
return !!value;
}
/**
* Check if a value is falsy (null, undefined, false, 0, NaN, '')
* @param {value: any}
* @returns {boolean}
*/
export function isFalsy(value) {
return !value;
}
/**
* Return the result of a logical Implication (A → B) operation between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function implication(a, b) {
return !a || b;
}
/**
* Return the result of a logical Biconditional (A ↔ B) operation between two booleans
* @param {a: boolean, b: boolean}
* @returns {boolean}
*/
export function biConditional(a, b) {
return a === b;
}