Standard Toolkit
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);
// false

Reference

and

function and<A>(a: A): <B>(b: B) => boolean

Parameters

ParameterTypeDescription
aAThe 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 value
  • B - The type of the second input value

andFn

function andFn<T, A>(
  a: (x: T) => A
): <B>(b: (y: T) => B) => (c: T) => boolean

Parameters

ParameterTypeDescription
a(x: T) => AThe 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 functions
  • A - The return type of the first function
  • B - 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));
// true

Example: 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);
// false

Example: Partial application

import { and } from '@accelint/core';

// Create a reusable validator
const isEnabled = and(true);

isEnabled(someCondition);
// true if someCondition is truthy

Good to know: Both and and andFn convert their operands to boolean values using Boolean(), so they work with any truthy/falsy values.

  • or - Logical disjunction (||)
  • not - Logical negation (!)
  • nand - Logical NAND (!(a && b))
  • xor - Logical exclusive OR

On this page