import type { FieldMachineContext, FormMachineContext } from "./form.types" export { default as equal } from "fast-deep-equal" export function isPromise(value: any): value is Promise { return !!value && typeof value.then === "function" } export function getFieldValues(fields: FormMachineContext["fields"]) { const values = {} as Record for (const key in fields) { if (Object.hasOwnProperty.call(fields, key)) { values[key] = fields[key].state.context.value } } return values } export function getFirstErrorField(fields: FormMachineContext["fields"]) { for (const key in fields) { if (Object.hasOwnProperty.call(fields, key)) { const error = fields[key].state.context.error if (error) return key } } } export function getErrors(fields: FormMachineContext["fields"]) { const errors = {} as Record for (const key in fields) { if (Object.hasOwnProperty.call(fields, key)) { const error = fields[key].state.context.error if (error) errors[key] = error } } return errors } export function isValidating(fields: FormMachineContext["fields"]) { return checkFieldContext(fields, "validating") } export function isDirty(fields: FormMachineContext["fields"]) { return checkFieldContext(fields, "dirty") } export function hasError(fields: FormMachineContext["fields"]) { return checkFieldContext(fields, "error") } function checkFieldContext(fields: FormMachineContext["fields"], item: keyof FieldMachineContext) { for (const key in fields) { if (Object.hasOwnProperty.call(fields, key)) { if (fields[key].state.context[item]) return true } } return false }