From 2dff46d74da2043abf4cc5d839a839f7b6f75073 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 29 Jun 2019 17:38:24 +0100 Subject: [PATCH] Add leading 0s to URLs are more human-friendly --- index.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index ecd52db..69a5d6d 100644 --- a/index.ts +++ b/index.ts @@ -9,7 +9,8 @@ import ProgressBar from 'progress'; const BUILD_DIR = join(__dirname, 'build'); const SRC_DIR = join(__dirname, 'src'); -const PROGRESS_BAR_FORMAT = "[:bar] :rate/ps :percent"; +const PROGRESS_BAR_FORMAT = "[:bar] :rate/ps :percent :current/:total"; +const MAX_VALUE = process.env.MAX_VALUE ? parseInt(process.env.MAX_VALUE) : 10; const BUNDLER_OPTIONS = { outDir: BUILD_DIR, @@ -21,6 +22,10 @@ function statusOutput(message: string) { console.log("> " + message + "..."); } +function isPrecision(value: number, precision: number) { + return parseFloat(value.toFixed(precision)) === value; +} + function writeTemplate(content: string, value: string) { const outputFile = join(BUILD_DIR, value, 'index.html'); mkdirp.sync(dirname(outputFile)); @@ -30,6 +35,13 @@ function writeTemplate(content: string, value: string) { 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"); + } } (async function() { @@ -42,7 +54,7 @@ function renderTemplate(template: HandlebarsTemplateDelegate, value: number) { statusOutput("Compiling HTML template"); const template = Handlebars.compile(readFileSync(join(BUILD_DIR, 'template.html')).toString()); - const possibleValues = range(0, 10, 0.5); + const possibleValues = range(0, MAX_VALUE, 0.01); const bar = new ProgressBar(PROGRESS_BAR_FORMAT, { total: possibleValues.length, width: 40 @@ -50,7 +62,9 @@ function renderTemplate(template: HandlebarsTemplateDelegate, value: number) { statusOutput("Generating pages"); possibleValues.forEach((i) => { - renderTemplate(template, i); + if (i) { + renderTemplate(template, parseFloat(i.toFixed(2))); + } bar.tick(); }); })();