UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

36 lines (35 loc) 1.93 kB
import type { ImmutableDictionary } from "./dictionary.js"; import type { AnyCaller } from "./function.js"; /** Log an error to the console. */ export declare function logError(reason: unknown): void; /** Is an unknown value an `Error` instance? */ export declare function isError(v: unknown): v is Error & { readonly code?: string | undefined; }; /** Things that can be a message. */ export type PossibleMessage = { message: string; } | string; /** Return the string message from an unknown value, or return `undefined` if it could not be found. */ export declare function getMessage(input: unknown): string | undefined; /** Require a message from an unknown value, or throw `RequiredError` if it could not be found. */ export declare function requireMessage(input: PossibleMessage, caller?: AnyCaller): string; /** * Split a string message into lines, look for prefixes like `name:`, and return a dictionary of those named messages. * - Full messages strings can have multiple lines separated by `\n` newline. * - Named messages are extracted into their own entries in the dictionary. * - Unnamed messages are combined into a single entry with the key `""` (empty string). */ export declare function splitMessage(input: PossibleMessage): ImmutableDictionary<string>; /** * Join a dictionary of named messages back into a single string. * - The `""` (empty string) key is emitted as unnamed lines. * - Named messages are emitted as `name: message`, one line per message line. * - Empty lines are skipped and each emitted line is trimmed to match `splitMessage()` semantics. */ export declare function joinMessage(input: ImmutableDictionary<string>): string; /** * Name a message by applying a `name: ` prefix to it. * - Assumes each line in the message is a separate error, so each line has the same prefix applied. */ export declare function getNamedMessage(name: string | number, message: string): string;