/** * Interface for a formatter. */ export interface Formatter { /** * Formats a value. * @param {unknown} value - The value to format. * @returns {TResult} The formatted value. */ format(value: unknown): TResult; } /** * Interface for a reversible formatter. */ export interface ReversibleFormatter extends Formatter { /** * Unformats a value. * @param {TResult} value - The value to unformat. * @returns {TOrigin} The unformatted value. */ unformat(value: TResult): TOrigin; } /** * Type for a formatter factory. */ export type FormatterFactory = new (...args: TSettings) => Formatter /** * Type for a reversible formatter factory. */ export type ReversibleFormatterFactory = new (...args: TSettings) => ReversibleFormatter /** * Checks if a formatter is reversible. * @param {Formatter | (new (...args: TArgs) => Formatter)} formatter - The formatter to check. * @returns {boolean} True if the formatter is reversible, false otherwise. */ export function isReversible(formatter: (new (...args: TArgs) => Formatter)): formatter is (new (...args: TArgs) => Formatter & ReversibleFormatter) export function isReversible(formatter: Formatter): formatter is ReversibleFormatter export function isReversible(formatter: Formatter | (new (...args: TArgs) => Formatter)): formatter is ReversibleFormatter | (new (...args: TArgs) => Formatter & ReversibleFormatter) { switch (typeof formatter) { case 'function': return 'unformat' in formatter.prototype && typeof formatter.prototype.unformat == 'function'; case 'object': return formatter && 'unformat' in formatter && typeof formatter.unformat == 'function'; default: return false; } }