/** * Resolver function to evaluate values for a given key from the provided values record. */ type ValueLookup = (key: string, values: Record) => any; /** * Resolver function to evaluate values for a given key. */ type ValueResolver = (key: string) => any; /** * Replacement Options */ interface ReplaceOptions { /** * Defines prefix of the regular expression to lookup key values, the default is `\\$\\{`. */ keyPrefix?: string; /** * Defines postfix of the regular expression to lookup key values, the default is `\\}`. */ keyPostfix?: string; /** * Defines middle part of the regular expression to lookup key values, * the default is `[\\p{Letter}\\p{Mark}\\d_.$-]+`. */ keyPattern?: string; /** * Defines regex flags, like g and u or i. * The default is `gu` */ keyPatternFlags?: string; /** * Defines the value used if the key lookup provides no value, by default this * property is undefined, which means that the input key expression is kept. */ valueNotFound?: string | undefined; /** * custom function to lookup values for keys from the second parameter provided to * the created replacer function. */ valueLookup?: ValueLookup; } /** * A replacement function to replace all `${key}` values in a given `template`. * * @example * ```ts * import replace from "apprt-core/string-replace"; * * const msg = replace("Say ${msg}",{msg: "hello" }); * msg === "Say hello"; // true * ``` * @param template the template string containing `${key}` expressions. * @param values the value lookup object or function * @returns a string with all expressions replaced by matching values in `valueLookup`. */ type StringReplacer = (template: string, values?: Record | ValueResolver) => string; /** * Creates a string template replacer function. * @param options options of replacement function * @returns string replacer function. * @example _Adjust options to change not found values_ * ```ts * import {customReplacer} from "apprt-core/string-replace"; * * const replace = customReplacer({ * valueNotFound: "" * }); * * const msg = replace("${name} says ${msg}",{msg: "hello" }); * msg === " says hello"; // true * ``` * * @example _Custom value lookup function_ * ```ts * import {customReplacer} from "apprt-core/string-replace"; * * const replace = customReplacer({ * // use custom key value lookup function * valueLookup: (key, values) => values[key] * }); * * const msg = replace("Says ${msg}",{msg: "hello" }); * msg === "Says hello"; // true * ``` */ declare function customReplacer(options?: ReplaceOptions): StringReplacer; /** * A default string replace function with the default options. * * @example * _Replace `${key}` using object as key to value mapping_ * ```ts * import {replace} from "apprt-core/string-replace"; * * const msg = replace("${name} says ${msg}", { msg: "hello" }); * msg === "${name} says hello"; // true * ``` * * @example * _Replacing `${key}` using custom value lookup function_ * ```ts * import {replace} from "apprt-core/string-replace"; * * const msg = replace("${name} says ${msg}", (key) => key === "msg" ? "hello" : ""); * msg === " says hello"; // true * ``` * * @param template the string template * @param values record of values or a lookup function * @returns a string with all `${key}` expressions replaced by matching values in `valueLookup`. * */ declare const replace: StringReplacer; export { customReplacer, replace as default, replace }; export type { ReplaceOptions, StringReplacer, ValueLookup, ValueResolver };