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 unique handlebar helper #3717

Merged
merged 1 commit into from
Jul 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,6 +5,7 @@ export enum HandlebarHelpersEnum {
LOWERCASE = 'lowercase',
PLURALIZE = 'pluralize',
DATEFORMAT = 'dateFormat',
UNIQUE = 'unique',
}

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -15,4 +16,5 @@ export const HandlebarHelpers = {
[HandlebarHelpersEnum.LOWERCASE]: { description: 'transform to lowercase' },
[HandlebarHelpersEnum.PLURALIZE]: { description: 'pluralize if needed' },
[HandlebarHelpersEnum.DATEFORMAT]: { description: 'format date' },
[HandlebarHelpersEnum.UNIQUE]: { description: 'filter unique values in an array' },
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ describe('Compile Template', function () {
expect(result).toEqual('<div>1 dog and 2 sausages for him</div>');
});

it('should render unique values of array', async function () {
const result = await useCase.execute(
CompileTemplateCommand.create({
data: {
names: [{ name: 'dog' }, { name: 'cat' }, { name: 'dog' }],
},
template:
'<div>{{#each (unique names "name")}}{{this}}-{{/each}}</div>',
})
);

expect(result).toEqual('<div>dog-cat-</div>');
});

it('should allow the user to specify handlebars helpers', async function () {
const result = await useCase.execute(
CompileTemplateCommand.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ Handlebars.registerHelper(
}
);

Handlebars.registerHelper(
HandlebarHelpersEnum.UNIQUE,
function (array, property) {
if (!Array.isArray(array)) return '';

return array
.map((item) => {
if (item[property]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@jainpawan21 wdyt of also making it work for unique values of an array of strings?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the object doesn't have that property we should handle it somehow

return item[property];
}
})
.filter((value, index, self) => self.indexOf(value) === index);
Comment on lines +58 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

If I am getting it right, we will return an array of only the property we filtered the distinct of. I feel we should return the whole object.
For example:
array: [{name:1,var:1},{name:1,var:1},{name:2,var:1}]
should return [{name:1,var:1},{name:2,var:1}]`
instead of ['1','2']

what do you think?

}
);

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