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

feat: add unique handlebar helper #3717

merged 1 commit into from
Jul 4, 2023

Conversation

jainpawan21
Copy link
Member

What change does this PR introduce?

  1. Add unique handlebar helper and its test.

Why was this change needed?

To remove duplicates in array iteration

Other information (Screenshots)

Usage Example:-

Custom code

{{#each (unique names "name")}}
--<b>{{this}}</b>--- 
{{/each}}

Input:-

{
	"names": [{
		"name": "Name 1",
		"age": "30"
	}, {
		"name": "Name 2"
	}, {
		"name": "Name 1"
	}]
}

HTML output:-

--Name 1----Name 2--

@linear
Copy link

linear bot commented Jul 4, 2023

NV-2482 unique items in array helper in handlebars

If I send an array with duplicate items in payload and in email (or any other channel) editor, loop through this payload variable, all duplicate items will be considered.

There should be a helper to consider only unique values (take duplicate values only once)

related:- https://stackoverflow.com/a/23733207

David suggested to create process of adding new handlebar helpers easy for community.

@github-actions
Copy link

github-actions bot commented Jul 4, 2023

Pull reviewers stats

Stats of the last 30 days for novu:

User Total reviews Time to review Total comments
p-fernandez 48 1h 38m 147
LetItRock 17 17h 33m 65
djabarovgeorge 16 1d 22h 45m 55
scopsy 15 24m 3
ainouzgali 11 53m 50
unicodeveloper 9 7d 8h 29m 96
davidsoderberg 5 15h 55m 3
BiswaViraj 3 1d 22h 30m 2
jainpawan21 3 4h 46m 2
peoray 1 27m 1
michaldziuba03 1 10h 53m 1
Cliftonz 1 5d 54m 0

Copy link
Contributor

@LetItRock LetItRock left a comment

Choose a reason for hiding this comment

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

@jainpawan21 jainpawan21 added this pull request to the merge queue Jul 4, 2023
Merged via the queue into next with commit d24d4cb Jul 4, 2023
9 checks passed
@jainpawan21 jainpawan21 deleted the NV-2482 branch July 4, 2023 16:11

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?


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.

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

Comment on lines +58 to +64
return array
.map((item) => {
if (item[property]) {
return item[property];
}
})
.filter((value, index, self) => self.indexOf(value) === index);
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?

LetItRock pushed a commit that referenced this pull request Jul 4, 2023
feat: add unique handlebar helper
@LetItRock LetItRock mentioned this pull request Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants