import { pipe } from "fp-ts/function" import cloneDeep from "lodash/cloneDeep" import isEqual from "lodash/isEqual" import { JSPrimitive, TypeFromPrimitive } from "./primtive" export const objRemoveKey = (key: K) => (obj: T): Omit => pipe(cloneDeep(obj), (e) => { delete e[key] return e }) export const objFieldMatches = ( fieldName: K, matches: ReadonlyArray ) => // eslint-disable-next-line no-unused-vars (obj: T): obj is T & { [_ in K]: V } => matches.findIndex((x) => isEqual(obj[fieldName], x)) !== -1 export const objHasProperty = ( prop: K, type?: P ) => // eslint-disable-next-line (obj: O): obj is O & { [_ in K]: TypeFromPrimitive

} => // eslint-disable-next-line prop in obj && (type === undefined || typeof (obj as any)[prop] === type) type TypeFromPrimitiveArray

= P extends "undefined" ? undefined : P extends "object" ? object[] | null : P extends "boolean" ? boolean[] : P extends "number" ? number[] : P extends "bigint" ? BigInt[] : P extends "string" ? string[] : P extends "symbol" ? Symbol[] : P extends "function" ? Function[] : unknown[] export const objHasArrayProperty = ( prop: K, type: P ) => // eslint-disable-next-line (obj: O): obj is O & { [_ in K]: TypeFromPrimitiveArray

} => prop in obj && Array.isArray((obj as any)[prop]) && (obj as any)[prop].every( (val: unknown) => typeof val === type // eslint-disable-line )