Predicate overview
Instances
Predicate
export interface Predicate<T> {
(value: T): boolean;
}
Combinators
not
export const not = <T>(self: Predicate<T>): Predicate<T>
Returns Predicate<T>
with negates result.
import { not, isNumber } from "perlica/Predicate"
const isNotNumber = not(isNumber);
expect(isNotNumber(4)).toBeFalse();
expect(isNotNumber("hello")).toBeTrue();
and
export const and = <T>(self: Predicate<T>, that: Predicate<T>): Predicate<T>
Combines two predicates (self
and that
) into a new Predicate<T>
.
Returns true
if both returns true
, otherwise returns false
.
import { and } from "perlica/Predicate"
import { lt, gt } from "perlica/Predicate/Number"
const between5And10 = and(lt(10), gt(5));
expect(between5And10(7)).toBeTrue();
expect(between5And10(2)).toBeFalse();
expect(between5And10(12)).toBeFalse();
or
export const or = <T>(self: Predicate<T>, that: Predicate<T>): Predicate<T>
Combines two predicates (self
and that
) into a new Predicate<T>
.
Returns true
if at least one returns true
, otherwise returns false
.
import { or } from "perlica/Predicate"
import { lt, gt} from "perlica/Predicate/Number"
const less5OrGreater10 = or(lt(5), gt(10));
expect(less5OrGreater10(7)).toBeFalse();
expect(less5OrGreater10(2)).toBeTrue();
expect(less5OrGreater10(12)).toBeTrue();
all
export const all = <T>(predicates: Iterable<Predicate<T>>): Predicate<T>
Combining multiple predicates
into a new Predicate<T>
.
Returns true
if all returns true
, otherwise returns false
.
import { all, isNumber } from "perlica/Predicate"
import { lt, gt } from "perlica/Predicate/Number"
const myNumber = all([isNumber, lt(10), gt(5)]);
expect(myNumber(7)).toBeTrue();
expect(myNumber(2)).toBeFalse();
expect(myNumber(12)).toBeFalse();
any
export const any = <T>(predicates: Iterable<Predicate<T>>): Predicate<T>
Combining multiple predicates
into a new Predicate<T>
.
Returns true
if at least one returns true
, otherwise returns false
.
import { any, isNumber, isString, isBoolean } from "perlica/Predicate"
const isNumberOrStringOrBoolean = any([isNumber, isString, isBoolean]);
expect(isNumberOrStringOrBoolean(4)).toBeTrue();
expect(isNumberOrStringOrBoolean("test")).toBeTrue();
expect(isNumberOrStringOrBoolean(true)).toBeTrue();
expect(isNumberOrStringOrBoolean(null)).toBeFalse();
expect(isNumberOrStringOrBoolean(undefined)).toBeFalse();
expect(isNumberOrStringOrBoolean({})).toBeFalse();
eq
export const eq = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is equal to a pre-defined value
.
import { eq } from "perlica/Predicate"
const predicate = eq(5);
expect(predicate(5)).toBeTrue();
expect(predicate(4)).toBeFalse();
ne
export const ne = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is not equal to a pre-defined value
.
import { ne } from "perlica/Predicate"
const predicate = ne(5);
expect(predicate(5)).toBeFalse();
expect(predicate(4)).toBeTrue();
Guards
isNull
export const isNull = (value: unknown): value is null
Returns true
if value
is null
, otherwise returns false
.
import { isNull } from "perlica/Predicate"
expect(isNull(null)).toBeTrue();
expect(isNull(undefined)).toBeFalse();
expect(isNull(4)).toBeFalse();
expect(isNull("hello")).toBeFalse();
isNotNull
export const isNotNull = <T>(value: T): value is Exclude<T, null>
Returns true
if value
is not null
, otherwise returns false
.
import { isNotNull } from "perlica/Predicate"
expect(isNotNull(null)).toBeFalse();
expect(isNotNull(undefined)).toBeTrue();
expect(isNotNull(4)).toBeTrue();
expect(isNotNull("hello")).toBeTrue();
isUndefined
export const isUndefined = (value: unknown): value is undefined
Returns true
if value
is undefined
, otherwise returns false
.
import { isUndefined } from "perlica/Predicate"
expect(isUndefined(null)).toBeFalse();
expect(isUndefined(undefined)).toBeTrue();
expect(isUndefined(4)).toBeFalse();
expect(isUndefined("hello")).toBeFalse();
isNotUndefined
export const isNotUndefined = <T>(value: T): value is Exclude<T, undefined>
Returns true
if value
is not undefined
, otherwise returns false
.
import { isNotUndefined } from "perlica/Predicate"
expect(isNotUndefined(null)).toBeTrue();
expect(isNotUndefined(undefined)).toBeFalse();
expect(isNotUndefined(4)).toBeTrue();
expect(isNotUndefined("hello")).toBeTrue();
isBoolean
export const isBoolean = (value: unknown): value is boolean
Returns true
if value
is true
or false
, otherwise returns false
.
import { isBoolean } from "perlica/Predicate"
expect(isBoolean(true)).toBeTrue();
expect(isBoolean(false)).toBeTrue();
expect(isBoolean(null)).toBeFalse();
expect(isBoolean(4)).toBeFalse();
isNumber
export const isNumber = (value: unknown): value is number
Returns true
if value
is number
, otherwise returns false
.
import { isNumber } from "perlica/Predicate"
expect(isNumber(4)).toBeTrue();
expect(isNumber(4.4)).toBeTrue();
expect(isNumber(null)).toBeFalse();
expect(isNumber(true)).toBeFalse();
expect(isNumber("hello")).toBeFalse();
isInteger
export const isInteger = (value: unknown): value is number
Returns true
if value
is integer
, otherwise returns false
.
import { isInteger } from "perlica/Predicate"
expect(isInteger(4)).toBeTrue();
expect(isInteger(4.4)).toBeFalse();
isBigInt
export const isInteger = (value: unknown): value is number
Returns true
if value
is bigint
, otherwise returns false
.
import { isBigInt } from "perlica/Predicate"
expect(isBigInt(4)).toBeFalse();
expect(isBigInt(4n)).toBeTrue();
isString
export const isString = (value: unknown): value is string
Returns true
if value
is string
, otherwise returns false
.
import { isString } from "perlica/Predicate"
expect(isString("hello")).toBeTrue();
expect(isString(4)).toBeFalse();
expect(isString(null)).toBeFalse();
expect(isString(true)).toBeFalse();
isFunction
export const isFunction = (value: unknown): value is Function
Returns true
if value
is Function
, otherwise returns false
.
import { isFunction } from "perlica/Predicate"
expect(isFunction(isFunction)).toBeTrue();
expect(isFunction("isFunction")).toBeFalse();
isSymbol
export const isSymbol = (value: unknown): value is symbol
Returns true
if value
is symbol
, otherwise returns false
.
import { isSymbol } from "perlica/Predicate"
expect(isSymbol(Symbol.for("a"))).toBeTrue();
expect(isSymbol("a")).toBeFalse();
isTruthy
export const isTruthy = (value: unknown): boolean
Returns true
if value
is not symbol
, otherwise returns false
.
import { isTruthy } from "perlica/Predicate"
expect(isTruthy(1)).toBeTrue();
expect(isTruthy("a")).toBeTrue();
expect(isTruthy(0)).toBeFalse();
expect(isTruthy("")).toBeFalse();
isSet
export const isSet = (value: unknown): value is Set<unknown>
Returns true
if value
is Set
, otherwise returns false
.
import { isSet } from "perlica/Predicate"
expect(isSet(new Set())).toBeTrue();
expect(isSet({})).toBeFalse();
isMap
export const isMap = (value: unknown): value is Map<unknown, unknown>
Returns true
if value
is Map
, otherwise returns false
.
import { isMap } from "perlica/Predicate"
expect(isMap(new Map())).toBeTrue();
expect(isMap({})).toBeFalse();
isError
export const isError = (value: unknown): value is Error
Returns true
if value
is Error
, otherwise returns false
.
import { isError } from "perlica/Predicate"
expect(isError(new Error())).toBeTrue();
expect(isError({})).toBeFalse();
expect(isError(null)).toBeFalse();
expect(isError(undefined)).toBeFalse();
isNullable
export const isNullable = <T>(value: T): value is Extract<T, null | undefined>
Returns true
if value
is null
or undefined
, otherwise returns false
.
import { isNullable } from "perlica/Predicate"
expect(isNullable(null)).toBeTrue();
expect(isNullable(undefined)).toBeTrue();
expect(isNullable(4)).toBeFalse();
isNotNullable
export const isNotNullable = <T>(value: T): value is NonNullable<T>
Returns true
if value
is not null
and not undefined
, otherwise returns false
.
import { isNotNullable } from "perlica/Predicate"
expect(isNotNullable(null)).toBeFalse();
expect(isNotNullable(undefined)).toBeFalse();
expect(isNotNullable(4)).toBeTrue();
isPromise
export const isPromise = (value: unknown): value is Promise<unknown>
Returns true
if value
is Promise
, otherwise returns false
.
import { IsPromise } from "perlica/Predicate"
expect(IsPromise(Promise.resolve(4))).toBeTrue();
expect(IsPromise(4)).toBeFalse();
isRegExp
export const isRegExp = (value: unknown): value is RegExp
Returns true
if value
is RegExp
, otherwise returns false
.
import { isRegExp } from "perlica/Predicate"
expect(isRegExp(/4/)).toBeTrue();
expect(isRegExp(4)).toBeFalse();
isDate
export const isDate = (value: unknown): value is Date
Returns true
if value
is Date
, otherwise returns false
.
import { isDate } from "perlica/Predicate"
expect(isDate(new Date())).toBeTrue();
expect(isDate({})).toBeFalse();
isRecordOrArray
export const isRecordOrArray = (value: unknown): value is { [x: PropertyKey]: unknown }
Returns true
if value
is record
or array
, otherwise returns false
.
import { isRecordOrArray } from "perlica/Predicate"
expect(isRecordOrArray({})).toBeTrue();
expect(isRecordOrArray([])).toBeTrue();
expect(isRecordOrArray(null)).toBeFalse();
expect(isRecordOrArray(undefined)).toBeFalse();
expect(isRecordOrArray(4)).toBeFalse();
isObject
export const isObject = (value: unknown): value is object
Returns true
if value
is object
(record
, array
, function
), otherwise returns false
.
import { isObject } from "perlica/Predicate"
expect(isObject({})).toBeTrue();
expect(isObject([])).toBeTrue();
expect(isObject(() => {})).toBeTrue();
expect(isObject(null)).toBeFalse();
expect(isObject(undefined)).toBeFalse();
expect(isObject(4)).toBeFalse();
hasProperty
export const hasProperty = <P extends PropertyKey>(
value: unknown, property: P,
): value is { [K in P]: unknown }
Returns true
if value
is object and containing a property
key, otherwise returns false
.
import { hasProperty } from "perlica/Predicate"
expect(hasProperty({}, "test")).toBeFalse();
expect(hasProperty("test", "test")).toBeFalse();
expect(hasProperty({ test: 4 }, "test")).toBeTrue();
isIterable
export const isIterable = (value: unknown): value is Iterable<unknown>
Returns true
if value
is object and containing a Symbol.iterator
key, otherwise returns false
.
import { isIterable } from "perlica/Predicate"
expect(isIterable([])).toBeTrue();
expect(isIterable({})).toBeFalse();
isAsyncIterable
export const isAsyncIterable = (value: unknown): value is AsyncIterable<unknown>
Returns true
if value
is object and containing a Symbol.asyncIterator
key, otherwise returns false
.
import { isAsyncIterable } from "perlica/Predicate"
expect(isAsyncIterable({ [Symbol.asyncIterator]() {} })).toBeTrue();
expect(isAsyncIterable({})).toBeFalse();
isTypedArray
export const isTypedArray = (value: unknown): value is TypedArray
Returns true
if value
is TypedArray
, otherwise returns false
.
isInt8Array
export const isInt8Array = (value: unknown): value is Int8Array
Returns true
if value
is Int8Array
, otherwise returns false
.
isUint8Array
export const isUint8Array = (value: unknown): value is Uint8Array
Returns true
if value
is Uint8Array
, otherwise returns false
.
isUint8ClampedArray
export const isUint8ClampedArray = (value: unknown): value is Uint8ClampedArray
Returns true
if value
is Uint8ClampedArray
, otherwise returns false
.
isInt16Array
export const isInt16Array = (value: unknown): value is Int16Array
Returns true
if value
is Int16Array
, otherwise returns false
.
isUint16Array
export const isUint16Array = (value: unknown): value is Uint16Array
Returns true
if value
is Uint16Array
, otherwise returns false
.
isInt32Array
export const isInt32Array = (value: unknown): value is Int32Array
Returns true
if value
is Int32Array
, otherwise returns false
.
isUint32Array
export const isUint32Array = (value: unknown): value is Uint32Array
Returns true
if value
is Uint32Array
, otherwise returns false
.
isFloat32Array
export const isFloat32Array = (value: unknown): value is Float32Array
Returns true
if value
is Float32Array
, otherwise returns false
.
isFloat64Array
export const isFloat64Array = (value: unknown): value is Float64Array
Returns true
if value
is Float64Array
, otherwise returns false
.
isBigInt64Array
export const isBigInt64Array = (value: unknown): value is BigInt64Array
Returns true
if value
is BigInt64Array
, otherwise returns false
.
isBigUint64Array
export const isBigUint64Array = (value: unknown): value is BigUint64Array
Returns true
if value
is BigUint64Array
, otherwise returns false
String
isEmpty
export const isEmpty = (self: string): self is ""
Returns true
if self
is empty, otherwise returns false
.
import { isEmpty } from "perlica/Predicate/String"
expect(isEmpty("hello")).toBeFalse();
expect(isEmpty("")).toBeTrue();
includes
export const includes = (searchString: string, position?: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if searchString
appears as a substring of self
, otherwise returns false
.
import { includes } from "perlica/Predicate/String"
const predicate = includes("test");
expect(predicate("Hello test world")).toBeTrue();
expect(predicate("Hello world")).toBeFalse();
startsWith
export const startsWith = (searchString: string, position?: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if searchString
starts with a self
, otherwise returns false
.
import { startsWith } from "perlica/Predicate/String"
const predicate = startsWith("test");
expect(predicate("test world")).toBeTrue();
expect(predicate("world")).toBeFalse();
endsWith
export const endsWith = (searchString: string, position?: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if searchString
ends with a self
, otherwise returns false
.
import { endsWith } from "perlica/Predicate/String"
const predicate = endsWith("test");
expect(predicate("world test")).toBeTrue();
expect(predicate("world")).toBeFalse();
isMatch
export const isMatch = (regexp: RegExp): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if a regular expression (regexp
) to match self
, otherwise returns false
.
import { isMatch } from "perlica/Predicate/String"
const predicate = isMatch(/Hello.*/);
expect(predicate("Hello world")).toBeTrue();
expect(predicate("Fello world")).toBeFalse();
max
export const maxLen = (count: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if value
is length less or equal to a pre-defined count
.
import { max } from "perlica/Predicate/String"
const predicate = max(5);
expect(predicate("Hello")).toBeTrue();
expect(predicate("Hello world")).toBeFalse();
min
export const minLen = (count: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if value
is length greater or equal to a pre-defined count
.
import { min } from "perlica/Predicate/String"
const predicate = min(5);
expect(predicate("Hello")).toBeTrue();
expect(predicate("Hello world")).toBeFalse();
len
export const len = (count: number): Predicate<string>
Returns a new Predicate<T>
.
Returns true
if value
is length equal to a pre-defined count
.
import { len } from "perlica/Predicate/String"
const predicate = len(5);
expect(predicate("Hello")).toBeTrue();
expect(predicate("Hi")).toBeFalse();
expect(predicate("Hello world")).toBeFalse();
Array
isEmpty
export const isEmpty = <T>(self: T[]): self is []
Returns true
if self
is empty, otherwise returns false
.
import { isEmpty } from "perlica/Predicate/Array"
*
expect(isEmpty([4])).toBeFalse();
expect(isEmpty([])).toBeTrue();
isNonEmpty
export const isNonEmpty = <T>(self: T[]): self is [T, ...T[]]
Returns true
if self
is not empty, otherwise returns false
.
import { isNonEmpty } from "perlica/Predicate/Array"
*
expect(isEmpty([4])).toBeTrue();
expect(isEmpty([])).toBeFalse();
max
export const maxLen = <T>(count: number): predicate.Predicate<T[]>
Returns a new Predicate<T>
.
Returns true
if value
is length less or equal to a pre-defined count
.
import { max } from "perlica/Predicate/Array"
const predicate = max(5);
expect(predicate([1, 2, 3, 4])).toBeTrue();
expect(predicate([1, 2, 3, 4, 5])).toBeFalse();
min
export const minLen = <T>(count: number): Predicate<T[]>
Returns a new Predicate<T>
.
Returns true
if value
is length greater or equal to a pre-defined count
.
import { min } from "perlica/Predicate/Array"
const predicate = min(4);
expect(predicate([1, 2, 3, 4])).toBeTrue();
expect(predicate([1, 2, 3])).toBeFalse();
len
export const len = <T>(count: number): Predicate<T[]>
Returns a new Predicate<T>
.
Return true
if value
is length equal to a pre-defined count
.
import { len } from "perlica/Predicate/Array"
const predicate = len(4);
expect(predicate([1, 2, 3, 4])).toBeTrue();
expect(predicate([1, 2])).toBeFalse();
expect(predicate([1, 2, 3, 4, 5])).toBeFalse();
Number
isPositive
export const isPositive = (value: number): boolean
Returns true
if value > 0
, otherwise returns false
.
import { isPositive } from "perlica/Predicate/Number"
expect(isPositive(0)).toBeFalse();
expect(isPositive(4)).toBeTrue();
expect(isPositive(-4)).toBeFalse();
isNonNegative
export const isNonNegative = (value: number): boolean
Returns true
if value >= 0
, otherwise returns false
.
import { isNonNegative } from "perlica/Predicate/Number"
expect(isNonNegative(0)).toBeTrue();
expect(isNonNegative(4)).toBeTrue();
expect(isNonNegative(-4)).toBeFalse();
isNegative
export const isNegative = (value: number): boolean
Returns true
if value < 0
, otherwise returns false
.
import { isNegative } from "perlica/Predicate/Number"
expect(isNegative(0)).toBeFalse();
expect(isNegative(4)).toBeFalse();
expect(isNegative(-4)).toBeTrue();
isNonPositive
export const isNonPositive = (value: number): boolean
Returns true
if value <= 0
, otherwise returns false
.
import { isNonPositive } from "perlica/Predicate/Number"
expect(isNonPositive(0)).toBeTrue();
expect(isNonPositive(4)).toBeFalse();
expect(isNonPositive(-4)).toBeTrue();
isFinite
export const isFinite = (value: number): boolean
Returns true
if value
is finite, not Infinity
or -Infinity
, otherwise returns false
.
import { isFinite } from "perlica/Predicate/Number"
expect(isFinite(4 / 2)).toBeTrue();
expect(isFinite(4 / 0)).toBeFalse();
multipleOf
export const multipleOf = (that: number): Predicate<number>
Returns a new Predicate<T>
.
Returns true
if value
is evenly divisible by a pre-defined that
.
import { multipleOf } from "perlica/Predicate/Number"
const predicate = multipleOf(2);
expect(predicate(4)).toBeTrue();
expect(predicate(3)).toBeFalse();
lt
export const lt = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is less than a pre-defined value
.
import { lt } from "perlica/Predicate/Number"
const predicate = lt(4);
expect(predicate(5)).toBeFalse();
expect(predicate(4)).toBeFalse();
expect(predicate(3)).toBeTrue();
gt
export const gt = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is greater than a pre-defined value
.
import { gt } from "perlica/Predicate/Number"
const predicate = gt(4);
expect(predicate(5)).toBeTrue();
expect(predicate(4)).toBeFalse();
expect(predicate(3)).toBeFalse();
le
export const le = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is less than or equal to a pre-defined value
.
import { le } from "perlica/Predicate/Number"
const predicate = le(4);
expect(predicate(5)).toBeFalse();
expect(predicate(4)).toBeTrue();
expect(predicate(3)).toBeTrue();
ge
export const ge = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is greater than or equal to a pre-defined value
.
import { ge } from "perlica/Predicate/Number"
const predicate = ge(4);
expect(predicate(5)).toBeTrue();
expect(predicate(4)).toBeTrue();
expect(predicate(3)).toBeFalse();
BigInt
isPositive
export const isPositive = (value: number): boolean
Returns true
if value > 0
, otherwise returns false
.
import { isPositive } from "perlica/Predicate/BigInt"
expect(isPositive(0n)).toBeFalse();
expect(isPositive(4n)).toBeTrue();
expect(isPositive(-4n)).toBeFalse();
isNonNegative
export const isNonNegative = (value: number): boolean
Returns true
if value >= 0
, otherwise returns false
.
import { isNonNegative } from "perlica/Predicate/BigInt"
expect(isNonNegative(0n)).toBeTrue();
expect(isNonNegative(4n)).toBeTrue();
expect(isNonNegative(-4n)).toBeFalse();
isNegative
export const isNegative = (value: number): boolean
Returns true
if value < 0
, otherwise returns false
.
import { isNegative } from "perlica/Predicate/BigInt"
expect(isNegative(0n)).toBeFalse();
expect(isNegative(4n)).toBeFalse();
expect(isNegative(-4n)).toBeTrue();
isNonPositive
export const isNonPositive = (value: number): boolean
Returns true
if value <= 0
, otherwise returns false
.
import { isNonPositive } from "perlica/Predicate/BigInt"
expect(isNonPositive(0n)).toBeTrue();
expect(isNonPositive(4n)).toBeFalse();
expect(isNonPositive(-4n)).toBeTrue();
multipleOf
export const multipleOf = (that: number): Predicate<number>
Returns a new Predicate<T>
.
Returns true
if value
is evenly divisible by a pre-defined that
.
import { multipleOf } from "perlica/Predicate/BigInt"
const predicate = multipleOf(2);
expect(predicate(4n)).toBeTrue();
expect(predicate(3n)).toBeFalse();
lt
export const lt = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is less than a pre-defined value
.
import { lt } from "perlica/Predicate/BigInt"
const predicate = lt(4);
expect(predicate(5n)).toBeFalse();
expect(predicate(4n)).toBeFalse();
expect(predicate(3n)).toBeTrue();
gt
export const gt = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is greater than a pre-defined value
.
import { gt } from "perlica/Predicate/BigInt"
const predicate = gt(4);
expect(predicate(5n)).toBeTrue();
expect(predicate(4n)).toBeFalse();
expect(predicate(3n)).toBeFalse();
le
export const le = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is less than or equal to a pre-defined value
.
import { le } from "perlica/Predicate/BigInt"
const predicate = le(4);
expect(predicate(5n)).toBeFalse();
expect(predicate(4n)).toBeTrue();
expect(predicate(3n)).toBeTrue();
ge
export const ge = <T>(that: T): Predicate<T>
Returns a new Predicate<T>
.
Returns true
if that
is greater than or equal to a pre-defined value
.
import { ge } from "perlica/Predicate/BigInt"
const predicate = ge(4);
expect(predicate(5n)).toBeTrue();
expect(predicate(4n)).toBeTrue();
expect(predicate(3n)).toBeFalse();