import type { Eq } from "@principia/prelude/Eq"; import type { Predicate } from "../Function"; import type { ReadonlyRecord } from "./model"; const _hasOwnProperty = Object.prototype.hasOwnProperty; /** * Test whether a given record contains the given key * * @category Guards * @since 1.0.0 */ export const has_ = (r: ReadonlyRecord, k: string): k is N => _hasOwnProperty.call(r, k); /** * Test whether a given record contains the given key * * @category Guards * @since 1.0.0 */ export function has(k: string, r: ReadonlyRecord): k is N; export function has(this: any, k: string, r?: ReadonlyRecord): k is N { return _hasOwnProperty.call(r === undefined ? this : r, k); } /** * Test whether one record contains all of the keys and values contained in another record * * @category Guards * @since 1.0.0 */ export const isSubrecord_ = (E: Eq) => ( me: ReadonlyRecord, that: ReadonlyRecord ): boolean => { for (const k in me) { if (!_hasOwnProperty.call(that, k) || !E.equals(me[k])(that[k])) { return false; } } return true; }; /** * Test whether one record contains all of the keys and values contained in another record * * @category Guards * @since 1.0.0 */ export const isSubrecord = (E: Eq) => (that: ReadonlyRecord) => ( me: ReadonlyRecord ): boolean => isSubrecord_(E)(me, that); /** * @category Guards * @since 1.0.0 */ export const every_ = (r: ReadonlyRecord, predicate: Predicate): boolean => { for (const k in r) { if (!predicate(r[k])) { return false; } } return true; }; /** * @category Guards * @since 1.0.0 */ export const every = (predicate: Predicate) => (r: ReadonlyRecord): boolean => every_(r, predicate); /** * @category Guards * @since 1.0.0 */ export const some_ = (r: ReadonlyRecord, predicate: (a: A) => boolean): boolean => { for (const k in r) { if (predicate(r[k])) { return true; } } return false; }; /** * @category Guards * @since 1.0.0 */ export const some = (predicate: (a: A) => boolean) => (r: ReadonlyRecord): boolean => some_(r, predicate); /** * @category Guards * @since 1.0.0 */ export const elem_ = (E: Eq) => (r: ReadonlyRecord, a: A): boolean => { for (const k in r) { if (E.equals(r[k])(a)) { return true; } } return false; }; /** * @category Guards * @since 1.0.0 */ export const elem = (E: Eq) => (a: A) => (r: ReadonlyRecord) => elem_(E)(r, a);