Checks
Emery considers "checks" predicates that cannot be expressed as type guards, without enforcing opaque types. While we recommend opaque types where appropriate, we can't make assumptions about your program's requirements.
Utils
While the available checks are useful for specific cases, we expose a handful of convenient utility functions to extend their behaviour or create your own purpose-built checks.
checkAll
Returns a new function for checking all cases against a value, a bit like pipe
for predicates.
function checkAll<T>(...predicates: UnaryPredicate<T>[]): UnaryPredicate<T>;
Useful for creating a new predicate that combines all those provided, which can be called elsewhere in your program.
import { checkAll, isNonNegative, isInteger } from 'emery';
export const isNonNegativeInteger = checkAll(isNonNegative, isInteger);
When combined with assertions, checks become incredibly powerful for simplifying logic.
import { assert } from 'emery';import { isNonNegativeInteger } from './path-to/check';
function getThingByIndex(index: number) { assert(isNonNegativeInteger(index));
// safely use `index` return things[index];}
checkAllWith
Apply all checks against a value.
function checkAllWith<T>(value: T, ...predicates: UnaryPredicate<T>[]): boolean;
Useful for calling predicates immediately:
function getThingByIndex(index: number) { assert(checkAllWith(index, isNonNegative, isInteger));
// safely use `index` return things[index];}
negate
Returns a new negated version of the stated predicate function.
function negate<T>(predicate: UnaryPredicate<T>): UnaryPredicate<T>;
Useful for inverting an existing predicate:
const hasSpaces = (value: string) => /\s/g.test(value);const hasNoSpaces = negate(hasSpaces);
Number
Common checks for number
types.
isFinite
Checks whether a number is finite.
isFinite(1); // → trueisFinite(Number.POSITIVE_INFINITY); // → falseisFinite(Number.NEGATIVE_INFINITY); // → false
isInfinite
Checks whether a number is infinite.
isInfinite(Number.POSITIVE_INFINITY); // → trueisInfinite(Number.NEGATIVE_INFINITY); // → trueisInfinite(1); // → false
isInteger
Checks whether a number is an integer.
isInteger(1); // → trueisInteger(Number.MAX_SAFE_INTEGER); // → trueisInteger(1.2); // → false
isFloat
Checks whether a number is a float.
isFloat(1.2); // → trueisFloat(-1.2); // → trueisFloat(1); // → false
isEven
Checks whether a number is even.
isEven(2); // → trueisEven(-2); // → trueisEven(1); // → false
isOdd
Checks whether a number is odd.
isOdd(1); // → trueisOdd(-1); // → trueisOdd(2); // → false
isNegativeZero
Checks whether a number is negative zero.
isNegativeZero(-0); // → trueisNegativeZero(0); // → falseisNegativeZero(1); // → false
isNegative
Checks whether a number is negative.
isNegative(-1); // → trueisNegative(0); // → falseisNegative(1); // → false
isPositive
Checks whether a number is positive.
isPositive(1); // → trueisPositive(0); // → falseisPositive(-1); // → false
isNonNegative
Checks whether a number is not negative.
isNonNegative(1); // → trueisNonNegative(0); // → trueisNonNegative(-1); // → false
isNonPositive
Checks whether a number is not positive.
isNonPositive(-1); // → trueisNonPositive(0); // → trueisNonPositive(1); // → false