export type ParseCasesResult = { /** * A list of prepended arguments. */ args: string[]; /** * A map of all cases. */ cases: Record; }; export type FormatValues = Record; export type ProcessFunction = (message: string, values?: FormatValues) => any[]; export type TypeHandler = (value: TValue | null | undefined, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => TReturn; /** * @typedef {Record} FormatValues */ /** * @callback ProcessFunction * @param {string} message * @param {FormatValues} [values={}] * @return {any[]} */ /** * @typedef {(value: TValue | null | undefined, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => TReturn} TypeHandler * @template [TValue=any] * @template [TReturn=any] * @param {TValue} value * The object which matched the key of the block being processed. * @param {string} matches * Any format options associated with the block being processed. * @param {string} locale * The locale to use for formatting. * @param {FormatValues} values * The object of placeholder data given to the original `format`/`process` * call. * @param {ProcessFunction} process * The `process` function itself so that sub-messages can be processed by type * handlers. * @return {TReturn} */ /** * The main class for formatting messages. * * @author Emanuel Rabina */ export class MessageFormatter { /** * Creates a new formatter that can work using any of the custom type handlers * you register. * * @param {string} locale * @param {Record} [typeHandlers] * Optional object where the keys are the names of the types to register, * their values being the functions that will return a nicely formatted * string for the data and locale they are given. */ constructor(locale: string, typeHandlers?: Record); locale: string; typeHandlers: Record>; /** * Formats an ICU message syntax string using `values` for placeholder data * and any currently-registered type handlers. * * @type {(message: string, values?: FormatValues) => string} */ format: (message: string, values?: FormatValues) => string; /** * Process an ICU message syntax string using `values` for placeholder data * and any currently-registered type handlers. The result of this method is * an array of the component parts after they have been processed in turn by * their own type handlers. This raw output is useful for other renderers, * eg: React where components can be used instead of being forced to return * raw strings. * * This method is used by {@link MessageFormatter#format} where it acts as a * string renderer. * * @param {string} message * @param {FormatValues} [values] * @return {any[]} */ process(message: string, values?: FormatValues): any[]; } /** * Finds the index of the matching closing curly bracket, including through * strings that could have nested brackets. * * @param {string} string * @param {number} fromIndex * @return {number} * The index of the matching closing bracket, or -1 if no closing bracket * could be found. */ export function findClosingBracket(string: string, fromIndex: number): number; /** * @typedef ParseCasesResult * @property {string[]} args * A list of prepended arguments. * @property {Record} cases * A map of all cases. */ /** * Most branch-based type handlers are based around "cases". For example, * `select` and `plural` compare compare a value to "case keys" to choose a * subtranslation. * * This util splits "matches" portions provided to the aforementioned handlers * into case strings, and extracts any prepended arguments (for example, * `plural` supports an `offset:n` argument used for populating the magic `#` * variable). * * @param {string} string * @return {ParseCasesResult} */ export function parseCases(string?: string): ParseCasesResult; /** * Handler for `plural` statements within ICU message syntax strings. Returns * a formatted string for the branch that closely matches the current value. * * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more * details on how the `plural` statement works. * * @param {string} value * @param {string} matches * @param {string} locale * @param {import('./MessageFormatter.js').FormatValues} values * @param {import('./MessageFormatter.js').ProcessFunction} process * @return {any | any[]} */ export function pluralTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[]; /** * Handler for `select` statements within ICU message syntax strings. Returns * a formatted string for the branch that closely matches the current value. * * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more * details on how the `select` statement works. * * @param {string} value * @param {string} matches * @param {string} locale * @param {import('./MessageFormatter.js').FormatValues} values * @param {import('./MessageFormatter.js').ProcessFunction} process * @return {any | any[]} */ export function selectTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[]; /** * Split a `{key, type, format}` block into those 3 parts, taking into account * nested message syntax that can exist in the `format` part. * * @param {string} block * @return {string[]} * An array with `key`, `type`, and `format` items in that order, if present * in the formatted argument block. */ export function splitFormattedArgument(block: string): string[];