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); // → falseisInfinite
Checks whether a number is infinite.
isInfinite(Number.POSITIVE_INFINITY); // → trueisInfinite(Number.NEGATIVE_INFINITY); // → trueisInfinite(1); // → falseisInteger
Checks whether a number is an integer.
isInteger(1); // → trueisInteger(Number.MAX_SAFE_INTEGER); // → trueisInteger(1.2); // → falseisFloat
Checks whether a number is a float.
isFloat(1.2); // → trueisFloat(-1.2); // → trueisFloat(1); // → falseisEven
Checks whether a number is even.
isEven(2); // → trueisEven(-2); // → trueisEven(1); // → falseisOdd
Checks whether a number is odd.
isOdd(1); // → trueisOdd(-1); // → trueisOdd(2); // → falseisNegativeZero
Checks whether a number is negative zero.
isNegativeZero(-0); // → trueisNegativeZero(0); // → falseisNegativeZero(1); // → falseisNegative
Checks whether a number is negative.
isNegative(-1); // → trueisNegative(0); // → falseisNegative(1); // → falseisPositive
Checks whether a number is positive.
isPositive(1); // → trueisPositive(0); // → falseisPositive(-1); // → falseisNonNegative
Checks whether a number is not negative.
isNonNegative(1); // → trueisNonNegative(0); // → trueisNonNegative(-1); // → falseisNonPositive
Checks whether a number is not positive.
isNonPositive(-1); // → trueisNonPositive(0); // → trueisNonPositive(1); // → false