From 63389c7daa2cce435ca91363c33958dedd871ab6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 29 Jun 2019 17:54:41 +0100 Subject: [PATCH] Ensure templates have the correct values in --- index.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/index.ts b/index.ts index 1b38cbf..3b4ca4b 100644 --- a/index.ts +++ b/index.ts @@ -26,22 +26,14 @@ function isPrecision(value: number, precision: number) { return parseFloat(value.toFixed(precision)) === value; } -function writeTemplate(content: string, value: string) { +function writeTemplate( + template: HandlebarsTemplateDelegate, + value: string, + context: any +) { const outputFile = join(BUILD_DIR, value, 'index.html'); mkdirp.sync(dirname(outputFile)); - writeFileSync(outputFile, content); -} - -function renderTemplate(template: HandlebarsTemplateDelegate, value: number) { - const html = template({ value }); - writeTemplate(html, value.toString()); - if (isPrecision(value, 1)) { - writeTemplate(html, value.toString() + '0'); - } - if (isPrecision(value, 0)) { - writeTemplate(html, value.toString() + '.0'); - writeTemplate(html, value.toString() + '.00'); - } + writeFileSync(outputFile, template(context)); } (async function() { @@ -65,7 +57,18 @@ function renderTemplate(template: HandlebarsTemplateDelegate, value: number) { statusOutput('Generating pages'); possibleValues.forEach(i => { if (i) { - renderTemplate(template, parseFloat(i.toFixed(2))); + const value = parseFloat(i.toFixed(2)); + const context = { + value: value.toFixed(2), + }; + writeTemplate(template, value.toString(), context); + if (isPrecision(value, 1)) { + writeTemplate(template, value.toString() + '0', context); + } + if (isPrecision(value, 0)) { + writeTemplate(template, value.toString() + '.0', context); + writeTemplate(template, value.toString() + '.00', context); + } } bar.tick(); });