archive
/
givemoneyto.me
Archived
1
Fork 0

Add leading 0s to URLs are more human-friendly

This commit is contained in:
Jake Howard 2019-06-29 17:38:24 +01:00
parent f9f4ae3460
commit 2dff46d74d
Signed by: jake
GPG Key ID: 57AFB45680EDD477
1 changed files with 17 additions and 3 deletions

View File

@ -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();
});
})();