| 1 |
import type { Formatter } from "./formatters"; |
| 2 |
import type { TemplateOpts } from "./options"; |
| 3 |
import type { Metadata } from "./parse"; |
| 4 |
import { normalizeReplacements } from "./options"; |
| 5 |
import parseAndBuildMetadata from "./parse"; |
| 6 |
import populatePlaceholders from "./populate"; |
| 7 |
|
| 8 |
export default function stringTemplate<T>( |
| 9 |
formatter: Formatter<T>, |
| 10 |
code: string, |
| 11 |
opts: TemplateOpts, |
| 12 |
): (arg?: unknown) => T { |
| 13 |
code = formatter.code(code); |
| 14 |
|
| 15 |
let metadata: Metadata; |
| 16 |
|
| 17 |
return (arg?: unknown) => { |
| 18 |
const replacements = normalizeReplacements(arg); |
| 19 |
|
| 20 |
if (!metadata) metadata = parseAndBuildMetadata(formatter, code, opts); |
| 21 |
|
| 22 |
return formatter.unwrap(populatePlaceholders(metadata, replacements)); |
| 23 |
}; |
| 24 |
} |