object-replace-mustache
Version:
replace placeholders of an object with a view like you would use mustache.render for strings
66 lines (61 loc) • 2.09 kB
text/typescript
type IsTemplateStringOptions = {
delimiters: [string, string];
};
declare const isTemplateString: (str: any, options?: IsTemplateStringOptions) => str is string;
interface ObjectReplaceOptions {
/**
* The delimiters to use for replacing.
*
* @default ["{{", "}}"]
*/
delimiters?: [string, string];
/**
* Whether to throw or ignore errors.
*
* @default "ignore"
*/
handleError?: "throw" | "ignore";
}
declare const replace: <T extends Record<string, any>>(item: T, view: Record<string, any>, options?: ObjectReplaceOptions) => Record<string, any>;
type ReplaceTemplateStringOptions = {
/**
* Specify the delimiters
*
* @default ['{{', '}}']
*/
delimiters?: [string, string];
/**
* Whether to throw an error when an error occurs or return the original string
*
* @default "throw"
*/
handleError?: "throw" | "ignore";
};
declare const replaceString: (template: string, view?: {}, options?: ReplaceTemplateStringOptions) => unknown;
declare const delimiters: {
readonly mustache: readonly ["{{", "}}"];
readonly ejs: readonly ["<%=", "%>"];
};
declare const replaceStringMustache: (template: string, view?: {}) => unknown;
declare const replaceStringEjs: (template: string, view?: {}) => unknown;
type RenderOptions = {
/**
* Specify the delimiters
*
* @default ['{{', '}}']
*/
delimiters?: [string, string];
/**
* Whether to throw an error when an error occurs or return the original string
*
* @default "ignore"
*/
handleError?: "throw" | "ignore";
/**
* Format function to format the value before replacing it in the string.
*/
format?: (value: any) => string;
};
declare const render: (str: string, view: Record<string, any>, options?: RenderOptions) => string;
export { delimiters, isTemplateString, render, replace, replaceString, replaceStringEjs, replaceStringMustache };
export type { IsTemplateStringOptions, ObjectReplaceOptions, RenderOptions, ReplaceTemplateStringOptions };