From 98404445883edd142bfcc08805e385f6e4810cd3 Mon Sep 17 00:00:00 2001 From: Ben Jaeger Date: Sun, 2 Jul 2023 07:44:47 +0100 Subject: [PATCH 1/3] factor: move custom-datetime helper to separate registerHelpers method --- src/parser.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 5081209..e5074b3 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -38,9 +38,7 @@ export class Parser { } private getDefaultContext() { - Handlebars.registerHelper("custom_datetime", (options) => { - return this.utils.getCurrentTime(options.fn(this)); - }); + this.registerHelpers() return { date: this.utils.getCurrentTime(this.utils.getDateFormat()), @@ -277,4 +275,10 @@ export class Parser { return null; } } + + private registerHelpers() { + Handlebars.registerHelper("custom_datetime", (options) => { + return this.utils.getCurrentTime(options.fn(this)); + }); + } } From 158c36fce25b43d171bb50b4a61e7d8e3e6f5efa Mon Sep 17 00:00:00 2001 From: Ben Jaeger Date: Sun, 2 Jul 2023 07:45:28 +0100 Subject: [PATCH 2/3] feat: add compare helper --- src/parser.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/parser.ts b/src/parser.ts index e5074b3..daa6937 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -280,5 +280,32 @@ export class Parser { Handlebars.registerHelper("custom_datetime", (options) => { return this.utils.getCurrentTime(options.fn(this)); }); + + Handlebars.registerHelper("compare", function (v1, operator, v2, options) { + switch (operator) { + case "==": + return (v1 == v2) ? options.fn(this) : options.inverse(this); + case "===": + return (v1 === v2) ? options.fn(this) : options.inverse(this); + case "!=": + return (v1 != v2) ? options.fn(this) : options.inverse(this); + case "!==": + return (v1 !== v2) ? options.fn(this) : options.inverse(this); + case "<": + return (v1 < v2) ? options.fn(this) : options.inverse(this); + case "<=": + return (v1 <= v2) ? options.fn(this) : options.inverse(this); + case ">": + return (v1 > v2) ? options.fn(this) : options.inverse(this); + case ">=": + return (v1 >= v2) ? options.fn(this) : options.inverse(this); + case "&&": + return (v1 && v2) ? options.fn(this) : options.inverse(this); + case "||": + return (v1 || v2) ? options.fn(this) : options.inverse(this); + default: + return options.inverse(this); + } + }); } } From c1c1af002f95e80a049a67b21157b4280b8cd22c Mon Sep 17 00:00:00 2001 From: Ben Jaeger Date: Sun, 2 Jul 2023 08:11:04 +0100 Subject: [PATCH 3/3] feat: add alternative names for comparisons --- src/parser.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/parser.ts b/src/parser.ts index daa6937..2cd40b1 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -284,24 +284,42 @@ export class Parser { Handlebars.registerHelper("compare", function (v1, operator, v2, options) { switch (operator) { case "==": + case "eq": + case "equals": return (v1 == v2) ? options.fn(this) : options.inverse(this); case "===": + case "seq": + case "strictly-equals": return (v1 === v2) ? options.fn(this) : options.inverse(this); case "!=": + case "ne": + case "not-equals": return (v1 != v2) ? options.fn(this) : options.inverse(this); case "!==": + case "sne": + case "strictly-not-equals": return (v1 !== v2) ? options.fn(this) : options.inverse(this); case "<": + case "lt": + case "less-than": return (v1 < v2) ? options.fn(this) : options.inverse(this); case "<=": + case "lte": + case "less-than-equals": return (v1 <= v2) ? options.fn(this) : options.inverse(this); case ">": + case "gt": + case "greater-than": return (v1 > v2) ? options.fn(this) : options.inverse(this); case ">=": + case "gte": + case "greater-than-equals": return (v1 >= v2) ? options.fn(this) : options.inverse(this); case "&&": + case "and": return (v1 && v2) ? options.fn(this) : options.inverse(this); case "||": + case "or": return (v1 || v2) ? options.fn(this) : options.inverse(this); default: return options.inverse(this);