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

feat: add comparison operators helpers #5334

Merged
merged 4 commits into from Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts
Expand Up @@ -10,6 +10,12 @@ export enum HandlebarHelpersEnum {
SORT_BY = 'sortBy',
NUMBERFORMAT = 'numberFormat',
I18N = 'i18n',
GT = 'gt',
GTE = 'gte',
LT = 'lt',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is existing handlebar helper EQUALS (==) and it might be confused with EQ (===).

Let me know if you have better ideas for naming.

LTE = 'lte',
EQ = 'eq',
NE = 'ne',
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -25,4 +31,10 @@ export const HandlebarHelpers = {
[HandlebarHelpersEnum.SORT_BY]: { description: 'sort an array of objects by a property' },
[HandlebarHelpersEnum.NUMBERFORMAT]: { description: 'format number' },
[HandlebarHelpersEnum.I18N]: { description: 'translate' },
[HandlebarHelpersEnum.GT]: { description: 'greater than' },
[HandlebarHelpersEnum.GTE]: { description: 'greater than or equal to' },
[HandlebarHelpersEnum.LT]: { description: 'lesser than' },
[HandlebarHelpersEnum.LTE]: { description: 'lesser than or equal to' },
[HandlebarHelpersEnum.EQ]: { description: 'strict equal' },
[HandlebarHelpersEnum.NE]: { description: 'strict not equal to' },
};
Expand Up @@ -209,4 +209,148 @@ describe('Compile Template', function () {
expect(result).toEqual('<div>Not a number</div>');
});
});

describe('gt helper', () => {
const template = `{{#gt steps 5 }}<span>gt block</span>{{else}}<span>else block</span>{{/gt}}`;
it('shoud render gt block', async () => {
const result = await useCase.execute({
data: { steps: 6 },
template,
});

expect(result).toEqual('<span>gt block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});

describe('gte helper', () => {
const template = `{{#gte steps 5 }}<span>gte block</span>{{else}}<span>else block</span>{{/gte}}`;
it('shoud render gte block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>gte block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 4 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});

describe('lt helper', () => {
const template = `{{#lt steps 5 }}<span>lt block</span>{{else}}<span>else block</span>{{/lt}}`;
it('shoud render lt block', async () => {
const result = await useCase.execute({
data: { steps: 4 },
template,
});

expect(result).toEqual('<span>lt block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});

describe('lte helper', () => {
const template = `{{#lte steps 5 }}<span>lte block</span>{{else}}<span>else block</span>{{/lte}}`;
it('shoud render lte block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>lte block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 6 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});

describe('eq helper', () => {
const template = `{{#eq steps 5 }}<span>eq block</span>{{else}}<span>else block</span>{{/eq}}`;
it('shoud render eq block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>eq block</span>');
});

it('shoud use strict check and render alternative block', async () => {
const result = await useCase.execute({
data: { steps: '5' },
template,
});

expect(result).toEqual('<span>else block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 6 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});

describe('ne helper', () => {
const template = `{{#ne steps 5 }}<span>ne block</span>{{else}}<span>else block</span>{{/ne}}`;
it('shoud render ne block', async () => {
const result = await useCase.execute({
data: { steps: 6 },
template,
});

expect(result).toEqual('<span>ne block</span>');
});

it('shoud use strict check and render ne block', async () => {
const result = await useCase.execute({
data: { steps: '5' },
template,
});

expect(result).toEqual('<span>ne block</span>');
});

it('shoud render alternative block', async () => {
const result = await useCase.execute({
data: { steps: 5 },
template,
});

expect(result).toEqual('<span>else block</span>');
});
});
});
Expand Up @@ -158,6 +158,60 @@ Handlebars.registerHelper(
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.GT,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 > arg2 ? options.fn(this) : options.inverse(this);
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.GTE,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 >= arg2 ? options.fn(this) : options.inverse(this);
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.LT,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 < arg2 ? options.fn(this) : options.inverse(this);
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.LTE,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 <= arg2 ? options.fn(this) : options.inverse(this);
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.EQ,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 === arg2 ? options.fn(this) : options.inverse(this);
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.NE,
function (arg1, arg2, options) {
// eslint-disable-next-line
// @ts-expect-error
return arg1 !== arg2 ? options.fn(this) : options.inverse(this);
}
);

@Injectable()
export class CompileTemplate {
async execute(command: CompileTemplateCommand): Promise<string> {
Expand Down