@kubb/core
Version:
Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.
26 lines (22 loc) • 686 B
text/typescript
type Options = {
text: string
replaceBy: string
prefix?: string
key: string
searchValues?: (prefix: string, key: string) => Array<RegExp | string>
}
export function searchAndReplace(options: Options): string {
const { text, replaceBy, prefix = '', key } = options
const searchValues = options.searchValues?.(prefix, key) || [
`${prefix}["${key}"]`,
`${prefix}['${key}']`,
`${prefix}[\`${key}\`]`,
`${prefix}"${key}"`,
`${prefix}'${key}'`,
`${prefix}\`${key}\``,
new RegExp(`${prefix}${key}`, 'g'),
]
return searchValues.reduce((prev, searchValue) => {
return prev.toString().replaceAll(searchValue, replaceBy)
}, text) as string
}