Skip to content

Commit

Permalink
feat(worker): add handlebar helper for number formatting (#4827)
Browse files Browse the repository at this point in the history
* feat: add handlebar helper for number formating

* refactor: improve number format helper readability
  • Loading branch information
michaldziuba03 authored Nov 15, 2023
1 parent d9f8a6e commit 8d8216e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
2 changes: 2 additions & 0 deletions libs/shared/src/consts/handlebar-helpers/handlebarHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum HandlebarHelpersEnum {
UNIQUE = 'unique',
GROUP_BY = 'groupBy',
SORT_BY = 'sortBy',
NUMBERFORMAT = 'numberFormat',
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -21,4 +22,5 @@ export const HandlebarHelpers = {
[HandlebarHelpersEnum.UNIQUE]: { description: 'filter unique values in an array' },
[HandlebarHelpersEnum.GROUP_BY]: { description: 'group by a property' },
[HandlebarHelpersEnum.SORT_BY]: { description: 'sort an array of objects by a property' },
[HandlebarHelpersEnum.NUMBERFORMAT]: { description: 'format number' },
};
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { Test } from '@nestjs/testing';
import { UserSession } from '@novu/testing';

import { CompileTemplate } from './compile-template.usecase';
import { CompileTemplateCommand } from './compile-template.command';

describe('Compile Template', function () {
let useCase: CompileTemplate;
let session: UserSession;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [CompileTemplate],
providers: [],
imports: [],
providers: [CompileTemplate],
}).compile();

session = new UserSession();
await session.initialize();

useCase = moduleRef.get<CompileTemplate>(CompileTemplate);
});

Expand Down Expand Up @@ -88,11 +83,11 @@ describe('Compile Template', function () {
],
},
template:
'{{#each (groupby names "name")}}<h1>{{key}}</h1>{{#each items}}{{age}}-{{/each}}{{/each}}>',
'{{#each (groupBy names "name")}}<h1>{{key}}</h1>{{#each items}}{{age}}-{{/each}}{{/each}}',
})
);

expect(result).toEqual('<h1>Name1</h1>30-32-<h1>Name2</h1>31-');
expect(result).toEqual('<h1>Name 1</h1>30-32-<h1>Name 2</h1>31-');
});

it('should render sortBy values of array', async function () {
Expand Down Expand Up @@ -188,4 +183,30 @@ describe('Compile Template', function () {
expect(result).toEqual('<div>ABCD</div>');
});
});

describe('Number formating', () => {
it('should format number', async () => {
const result = await useCase.execute(
CompileTemplateCommand.create({
data: { number: 1000000000 },
template:
'<div>{{numberFormat number decimalSep="," decimalLength="2" thousandsSep="|"}}</div>',
})
);

expect(result).toEqual('<div>1|000|000|000,00</div>');
});

it('should not fail and return passed value', async () => {
const result = await useCase.execute(
CompileTemplateCommand.create({
data: { number: 'Not a number' },
template:
'<div>{{numberFormat number decimalSep="," decimalLength="2" thousandsSep="|"}}</div>',
})
);

expect(result).toEqual('<div>Not a number</div>');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ Handlebars.registerHelper(
}
);

// based on: https://gist.github.com/DennyLoko/61882bc72176ca74a0f2
Handlebars.registerHelper(
HandlebarHelpersEnum.NUMBERFORMAT,
function (number, options) {
if (isNaN(number)) {
return number;
}

const decimalLength = options.hash.decimalLength || 2;
const thousandsSep = options.hash.thousandsSep || ',';
const decimalSep = options.hash.decimalSep || '.';

const value = parseFloat(number);

const re = '\\d(?=(\\d{3})+' + (decimalLength > 0 ? '\\D' : '$') + ')';

const num = value.toFixed(Math.max(0, ~~decimalLength));

return (decimalSep ? num.replace('.', decimalSep) : num).replace(
new RegExp(re, 'g'),
'$&' + thousandsSep
);
}
);

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

1 comment on commit 8d8216e

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.