object-replace-mustache
Version:
replace placeholders of an object with a view like you would use mustache.render for strings
68 lines • 2.16 kB
text/typescript
//#region src/is-template-string.d.ts
type IsTemplateStringOptions = {
delimiters: [string, string];
};
declare const isTemplateString: (str: any, options?: IsTemplateStringOptions) => str is string;
//#endregion
//#region src/replace-object.d.ts
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>;
//#endregion
//#region src/replace-string.d.ts
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;
//#endregion
//#region src/render.d.ts
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;
//#endregion
export { IsTemplateStringOptions, ObjectReplaceOptions, RenderOptions, ReplaceTemplateStringOptions, delimiters, isTemplateString, render, replace, replaceString, replaceStringEjs, replaceStringMustache };