From 58f8ecf4a0b01e20250dcccaa5006e61924f79e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dziuba?= Date: Mon, 15 Apr 2024 14:30:28 +0200 Subject: [PATCH] feat: add comparison operators helpers (#5334) Co-authored-by: Dima Grossman --- .../handlebar-helpers/handlebarHelpers.ts | 12 ++ .../compile-template/compile-template.spec.ts | 144 ++++++++++++++++++ .../compile-template.usecase.ts | 54 +++++++ 3 files changed, 210 insertions(+) diff --git a/libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts b/libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts index 5595ffb44bf..99e855a48df 100644 --- a/libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts +++ b/libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts @@ -10,6 +10,12 @@ export enum HandlebarHelpersEnum { SORT_BY = 'sortBy', NUMBERFORMAT = 'numberFormat', I18N = 'i18n', + GT = 'gt', + GTE = 'gte', + LT = 'lt', + LTE = 'lte', + EQ = 'eq', + NE = 'ne', } // eslint-disable-next-line @typescript-eslint/naming-convention @@ -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' }, }; diff --git a/packages/application-generic/src/usecases/compile-template/compile-template.spec.ts b/packages/application-generic/src/usecases/compile-template/compile-template.spec.ts index 4003ac48f29..9b2393168b2 100644 --- a/packages/application-generic/src/usecases/compile-template/compile-template.spec.ts +++ b/packages/application-generic/src/usecases/compile-template/compile-template.spec.ts @@ -209,4 +209,148 @@ describe('Compile Template', function () { expect(result).toEqual('
Not a number
'); }); }); + + describe('gt helper', () => { + const template = `{{#gt steps 5 }}gt block{{else}}else block{{/gt}}`; + it('shoud render gt block', async () => { + const result = await useCase.execute({ + data: { steps: 6 }, + template, + }); + + expect(result).toEqual('gt block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); + + describe('gte helper', () => { + const template = `{{#gte steps 5 }}gte block{{else}}else block{{/gte}}`; + it('shoud render gte block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('gte block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 4 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); + + describe('lt helper', () => { + const template = `{{#lt steps 5 }}lt block{{else}}else block{{/lt}}`; + it('shoud render lt block', async () => { + const result = await useCase.execute({ + data: { steps: 4 }, + template, + }); + + expect(result).toEqual('lt block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); + + describe('lte helper', () => { + const template = `{{#lte steps 5 }}lte block{{else}}else block{{/lte}}`; + it('shoud render lte block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('lte block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 6 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); + + describe('eq helper', () => { + const template = `{{#eq steps 5 }}eq block{{else}}else block{{/eq}}`; + it('shoud render eq block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('eq block'); + }); + + it('shoud use strict check and render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: '5' }, + template, + }); + + expect(result).toEqual('else block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 6 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); + + describe('ne helper', () => { + const template = `{{#ne steps 5 }}ne block{{else}}else block{{/ne}}`; + it('shoud render ne block', async () => { + const result = await useCase.execute({ + data: { steps: 6 }, + template, + }); + + expect(result).toEqual('ne block'); + }); + + it('shoud use strict check and render ne block', async () => { + const result = await useCase.execute({ + data: { steps: '5' }, + template, + }); + + expect(result).toEqual('ne block'); + }); + + it('shoud render alternative block', async () => { + const result = await useCase.execute({ + data: { steps: 5 }, + template, + }); + + expect(result).toEqual('else block'); + }); + }); }); diff --git a/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts b/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts index 1669e6c0d13..bd78462aa2c 100644 --- a/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts +++ b/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts @@ -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 {