UNPKG

object-replace-mustache

Version:

replace placeholders of an object with a view like you would use mustache.render for strings

49 lines (44 loc) 1.2 kB
import { isPlainObject } from './utils' import { replaceString } from './replace-string' export interface ObjectReplaceOptions { /** * The delimiters to use for replacing. * * @default ["{{", "}}"] */ delimiters?: [string, string] /** * Whether to throw or ignore errors. * * @default "ignore" */ handleError?: 'throw' | 'ignore' } export const replace = <T extends Record<string, any>>( item: T, view: Record<string, any>, options?: ObjectReplaceOptions, ): Record<string, any> => { return recursiveReplace(item, view, options) } const recursiveReplace = <T>( item: T, view: Record<string, any>, options?: ObjectReplaceOptions, ): any => { if (typeof item === 'string') { return replaceString(item, view, { delimiters: options?.delimiters, handleError: options?.handleError ?? 'ignore', }) } else if (isPlainObject(item)) { const result: Record<string, any> = {} for (const key in item) { result[key] = recursiveReplace(item[key], view, options) } return result } else if (Array.isArray(item)) { return [...item.map((subItem) => recursiveReplace(subItem, view, options))] } return item }