Packages@accelint/coreLogical
and
Logical conjunction (&&) for values and function results
Usage
import { and, andFn } from '@accelint/core';
// Value-based conjunction
and(true)(1);
// true
and(true)(0);
// false
// Function-based conjunction
const isPositive = (x: number) => x > 0;
const isLessThan10 = (x: number) => x < 10;
andFn(isPositive)(isLessThan10)(5);
// true
andFn(isPositive)(isLessThan10)(15);
// falseReference
and
function and<A>(a: A): <B>(b: B) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
a | A | The first value to compare |
Returns
Returns a curried function that accepts a second value and returns true if both values are truthy, false otherwise.
Type Parameters
A- The type of the first input valueB- The type of the second input value
andFn
function andFn<T, A>(
a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
a | (x: T) => A | The first function to evaluate |
Returns
Returns a curried function that accepts a second function, then accepts a value to pass to both functions, returning true if both function results are truthy, false otherwise.
Type Parameters
T- The type of the input value passed to both functionsA- The return type of the first functionB- The return type of the second function
Examples
Example: Validating conditions
import { and } from '@accelint/core';
const hasValue = (value: string | null) => value !== null;
const isNonEmpty = (value: string) => value.length > 0;
// Both conditions must be true
const value = "hello";
const isValid = and(hasValue(value))(isNonEmpty(value));
// trueExample: Combining predicates
import { andFn } from '@accelint/core';
const isEven = (x: number) => x % 2 === 0;
const isPositive = (x: number) => x > 0;
const isEvenAndPositive = andFn(isEven)(isPositive);
isEvenAndPositive(4);
// true
isEvenAndPositive(-4);
// false
isEvenAndPositive(3);
// falseExample: Partial application
import { and } from '@accelint/core';
// Create a reusable validator
const isEnabled = and(true);
isEnabled(someCondition);
// true if someCondition is truthyGood to know: Both
andandandFnconvert their operands to boolean values usingBoolean(), so they work with any truthy/falsy values.