Coder Social home page Coder Social logo

[Proposal]: uniq about toolbox HOT 12 CLOSED

mahdi-sheibak avatar mahdi-sheibak commented on June 2, 2024
[Proposal]: uniq

from toolbox.

Comments (12)

hamed-bavar avatar hamed-bavar commented on June 2, 2024 1

I think JSON.stringify is not the best choice for deeply comparing two objects, It is not going to work as expected for function and symbol types and some others, so it will have different behavior for different types of objects and you should have different strategy based on types of objects. I think using === is better because it will always return false for objects that do not have the same reference.

from toolbox.

hamed-bavar avatar hamed-bavar commented on June 2, 2024 1

in my opinion uniqBy comes very handy, but uniq and uniqWith can be implemented in just one function (for default using shallow comparison otherwise using a provided comparison function).

from toolbox.

hamed-bavar avatar hamed-bavar commented on June 2, 2024

How should elements be compared with each other? through deep comparison or just by reference?

from toolbox.

mahdi-sheibak avatar mahdi-sheibak commented on June 2, 2024

see this example: https://codesandbox.io/p/sandbox/uniq-example-czqq98
@hamed-bavar @ASafaeirad

from toolbox.

mahdi-sheibak avatar mahdi-sheibak commented on June 2, 2024

please share a sandbox

from toolbox.

hamed-bavar avatar hamed-bavar commented on June 2, 2024

check this out. https://playcode.io/1562822

from toolbox.

mahdi-sheibak avatar mahdi-sheibak commented on June 2, 2024

This is good, but does not work on an array of objects. How we can provide this on your implementation?

anotherImplementation([
    {name:"John"},
    {name:"John"},
])

from toolbox.

hamed-bavar avatar hamed-bavar commented on June 2, 2024

I know, as I said this implementation is just a shallow comparison (===) for the sake of keeping everything simple and performant [just like uniq function in lodash]. For deep comparison you need more than that, just a JSON.stringify. the uniq function should be prepared for comparing functions, dates, Symbols, complex nested objects, etc which makes the function very messy and slow.

from toolbox.

hamed-bavar avatar hamed-bavar commented on June 2, 2024

@ASafaeirad, @mahdi-sheibak. what if we shallow compare by default unless a comparison function was provided. but the time complexity would be more than previous implementation since the compare function of sets in JavaScript can not be changed and we need to use array instead.

from toolbox.

mahdi-sheibak avatar mahdi-sheibak commented on June 2, 2024

I know, as I said this implementation is just a shallow comparison (===) for the sake of keeping everything simple and performant [just like uniq function in lodash]. For deep comparison you need more than that, just a JSON.stringify. the uniq function should be prepared for comparing functions, dates, Symbols, complex nested objects, etc which makes the function very messy and slow.

I completely agree with you! How about we take inspiration from lodashjs and create three useful functions: 'uniq' to filter out duplicate values, 'uniqBy' to filter duplicates by a specific key, and 'uniqWith' to filter duplicates based on custom comparison logic?

from toolbox.

ASafaeirad avatar ASafaeirad commented on June 2, 2024

Agree, two objects with different references are not considered the same in the language therefore, to follow the "Least Surprise" principle, the uniq function should not mark them as duplicates. let's keep it simple and compare it with Object.is

from toolbox.

github-actions avatar github-actions commented on June 2, 2024

🎉 This issue has been resolved in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

from toolbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.