Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to have complex logic in templates #79

Merged
merged 10 commits into from
Nov 9, 2023
42 changes: 42 additions & 0 deletions src/helpers/compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";

export const compareHelper: HelperConstructorBlock = ctx => {

Check warning on line 3 in src/helpers/compare.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

'ctx' is defined but never used
return new HandlebarsHelper("compare", (v1, operator, v2): boolean => {
switch (operator) {
case "==":
case "eq":
case "equals":
return (v1 == v2);
case "===":
case "seq":
case "strictly-equals":
return (v1 === v2);
case "!=":
case "ne":
case "not-equals":
return (v1 != v2);
case "!==":
case "sne":
case "strictly-not-equals":
return (v1 !== v2);
case "<":
case "lt":
case "less-than":
return (v1 < v2);
case "<=":
case "lte":
case "less-than-equals":
return (v1 <= v2);
case ">":
case "gt":
case "greater-than":
return (v1 > v2);
case ">=":
case "gte":
case "greater-than-equals":
return (v1 >= v2);
default:
throw new Error(`Invalid comparison operator used with compare: ${operator}`);
}
});
};
19 changes: 19 additions & 0 deletions src/helpers/condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";

export const conditionHelper: HelperConstructorBlock = ctx => {

Check warning on line 3 in src/helpers/condition.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

'ctx' is defined but never used
return new HandlebarsHelper("condition", (v1, operator, v2): boolean => {
switch (operator) {
case "!":
case "not":
return !v1;
case "&&":
case "and":
return (v1 && v2);
case "||":
case "or":
return (v1 || v2);
default:
throw new Error(`Invalid comparison operator used with condition: ${operator}`);
nishantwrp marked this conversation as resolved.
Show resolved Hide resolved
}
});
};
9 changes: 9 additions & 0 deletions src/helpers/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DateAndTimeUtils } from "../utils/dateAndTime";

export class HelperContext {
public dateAndTimeUtils: DateAndTimeUtils;

constructor(dateUtils: DateAndTimeUtils) {
this.dateAndTimeUtils = dateUtils;
}
}
7 changes: 7 additions & 0 deletions src/helpers/custom_datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";

export const customDatetimeHelper: HelperConstructorBlock = ctx => {
return new HandlebarsHelper("custom_datetime", (options) => {
return ctx.dateAndTimeUtils.getCurrentTime(options.fn(this));
});
};
21 changes: 21 additions & 0 deletions src/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Handlebars from "handlebars/dist/handlebars";
import { HelperContext } from "./context";

export type HelperConstructorBlock = (ctx: HelperContext) => HandlebarsHelper;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type HelperImpl = (...args: Array<any>) => any;

export class HandlebarsHelper {
private tag: string;
private impl: HelperImpl;

constructor(tag: string, impl: HelperImpl) {
this.tag = tag;
this.impl = impl;
}

public register(): void {
Handlebars.registerHelper(this.tag, this.impl);
}
}
24 changes: 24 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DateAndTimeUtils } from "../utils/dateAndTime";
import { HelperConstructorBlock } from "./helper";
import { HelperContext } from "./context";

import { customDatetimeHelper } from "./custom_datetime";
import { compareHelper } from "./compare";
import { mathHelper } from "./math";
import { conditionHelper } from "./condition";

export class HelperFactory {
private static helpers: HelperConstructorBlock[] = [
customDatetimeHelper,
compareHelper,
mathHelper,
conditionHelper,
];

static registerHelpers(dateAndTimeUtils: DateAndTimeUtils): void {
const context = new HelperContext(dateAndTimeUtils);
for (const helper of this.helpers) {
helper(context).register();
}
}
}
25 changes: 25 additions & 0 deletions src/helpers/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";

export const mathHelper: HelperConstructorBlock = ctx => {

Check warning on line 3 in src/helpers/math.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

'ctx' is defined but never used
return new HandlebarsHelper("math", (rawV1, operator, rawV2): number => {
const v1 = Number.parseFloat(rawV1);
const v2 = Number.parseFloat(rawV2);

if (Number.isNaN(v1) || Number.isNaN(v2)) {
throw new Error(`Can't convert "${rawV1}" and "${rawV2}" to numbers while using math`);
}

switch (operator) {
case "+":
return v1 + v2;
case "-":
return v1 - v2;
case "*":
return v1 * v2;
case "/":
return v1 / v2;
default:
throw new Error(`Invalid operator used with math: ${operator}`);
}
});
};
7 changes: 3 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Note } from "./utils/templates";
import { getVariableFromDefinition } from "./variables/parser";
import { CustomVariable } from "./variables/types/base";
import { setTemplateVariablesView } from "./views/templateVariables";
import { HelperFactory } from "./helpers";

// Can't use import for this library because the types in the library
// are declared incorrectly which result in typescript errors.
Expand Down Expand Up @@ -38,10 +39,6 @@ export class Parser {
}

private getDefaultContext() {
Handlebars.registerHelper("custom_datetime", (options) => {
return this.utils.getCurrentTime(options.fn(this));
});

return {
date: this.utils.getCurrentTime(this.utils.getDateFormat()),
time: this.utils.getCurrentTime(this.utils.getTimeFormat()),
Expand Down Expand Up @@ -231,6 +228,8 @@ export class Parser {
template.body = this.preProcessTemplateBody(template.body);

try {
HelperFactory.registerHelpers(this.utils);

const processedTemplate = frontmatter(template.body);
const templateVariables = processedTemplate.attributes;

Expand Down
Loading