import type { Nullish } from "../../types/common.mjs"; import { isUnionThat } from "./array.mjs"; import type { Assertion, Validator } from "./common.mjs"; import { isObjectLike } from "./object.mjs"; import { isNull, isNullish, isUndefined } from "./primitive.mjs"; export const noCheck = (_: T): _ is never => true; export const optional = ( validator: Validator ): Validator => isUnionThat(validator, isUndefined); export const nullable = ( validator: Validator ): Validator => isUnionThat(validator, isNull); export const nullishOr = ( validator: Validator ): Validator => isUnionThat(validator, isNullish); export const stringRecord = ( validator: Validator, includeNotEnumerable = false ): Validator> => (value): value is Record => isObjectLike(value) && (includeNotEnumerable ? Object.getOwnPropertyNames(value) : Object.keys(value) ).every((key) => validator(Reflect.get(value, key))); export const record = ( validator: Validator ): Validator> => (value): value is Record => isObjectLike(value) && Reflect.ownKeys(value).every((key) => validator(Reflect.get(value, key))); export const is = (reference: T): Validator => (value: unknown): value is T => Object.is(value, reference); export const givenValue = is; export const defineValidator = ( validator: Validator ): Validator => validator; export const assertThat = ( validator: Validator, message?: string ): Assertion => (value) => { if (!validator(value)) throw new TypeError(message ?? "Assertion Failed"); };