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 uniq utility #237

Merged
merged 7 commits into from
Jul 18, 2024
Merged

feat: add uniq utility #237

merged 7 commits into from
Jul 18, 2024

Conversation

ondrejsevcik
Copy link
Contributor

No description provided.

import { uniq } from './uniq'

describe('uniq', () => {
it('returns a new array of mixed primitive value without duplicates', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

would be nice to test with: nulls, undefineds, NaN, objects

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I added a few more values into the inputs. Let me know if there are some others you would test for.

"../../../node_modules/@lokalise/biome-config/configs/biome-base.jsonc",
"../../../node_modules/@lokalise/biome-config/configs/biome-package.jsonc"
]
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ℹ️ The universal-ts-utils package was missing local biome config.

it('returns a new array of mixed primitive value without duplicates', () => {
const objectA = {}
const values = [1, 'a', Number.NaN, true, false, objectA, null, undefined]
const duplicateValues = [...values, ...values]
Copy link
Collaborator

Choose a reason for hiding this comment

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

note that this way of creating duplicates ensure that they are equal not just by value but also by reference.

in order to make behaviour more explicit, it may be helpful to have another test, where duplicates have same value but different reference. I think that will cause some of deduplication fail, which is fine, but something that users of the method should be aware of.

Copy link
Collaborator

@CarlosGamero CarlosGamero Jul 17, 2024

Choose a reason for hiding this comment

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

Is this method intended to be used with objects? As Igor mentioned objects are compared by reference and wondering if this can be confusing (different objects with the same value won't be removed).

If we don't want to use this method with objects maybe we can restrict the accepted types to avoid misunderstanding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added comment to clarify that the uniqueness is compared based on reference check.

@@ -0,0 +1,5 @@
/**
* A method to remove duplicates from an array of primitive values.
* Elements are compared by reference using a Set.
Copy link
Collaborator

Choose a reason for hiding this comment

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

is that true? isn't it done using native equality check, which for objects relies on a reference check, but for primitives does not?

Copy link
Contributor Author

@ondrejsevcik ondrejsevcik Jul 18, 2024

Choose a reason for hiding this comment

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

It's actually using SameValueZero algorithm. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#value_equality

I adjusted the comment there.

@ondrejsevcik ondrejsevcik merged commit 2924b60 into main Jul 18, 2024
4 checks passed
@ondrejsevcik ondrejsevcik deleted the feature/add-utils branch July 18, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants