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

Stage 1 Entrance Criteria #2

Closed
8 tasks done
Jack-Works opened this issue Jun 12, 2020 · 7 comments
Closed
8 tasks done

Stage 1 Entrance Criteria #2

Jack-Works opened this issue Jun 12, 2020 · 7 comments

Comments

@Jack-Works
Copy link
Member

Jack-Works commented Jun 12, 2020

https://tc39.es/process-document/

  • Identified “champion” who will advance the addition (Jack Works)
  • Prose outlining the problem or need and the general shape of a solution
  • Illustrative examples of usage
  • High-level API
  • Discussion of key algorithms, abstractions and semantics
  • Identification of potential “cross-cutting” concerns and implementation challenges/complexity
  • A publicly available repository for the proposal that captures the above requirements
  • Polyfills / demos
@mathiasbynens
Copy link
Member

IMHO “prose outlining the problem or need” could be clarified: what are some example use cases where deduplication in this manner is needed? The current README simply says this is “common” without backing up that claim.

I’m particularly interested to hear use cases for .unique(hashFunction) that don’t also require a (Weak)Map<hash, value>.

TechQuery added a commit that referenced this issue Jun 13, 2020
[fix] 2 detail bugs  (closes #1)
@TechQuery
Copy link
Collaborator

TechQuery commented Jun 13, 2020

use cases for .unique(hashFunction) that don’t also require a (Weak)Map<hash, value>

I don’t quite understand, can you explain? @mathiasbynens I add Typical cases in ReadMe.

Sometimes, we may need 2 or more key-value pairs as a hash to deduplicate an array.

@jridgewell
Copy link
Member

@TechQuery I also think the problem statement should be made more clear. I think from the example cases that you're interested in using an object's arbitrary property value as the unique key, instead of the object's identity?

@TechQuery
Copy link
Collaborator

TechQuery commented Jul 13, 2020

@jridgewell Sometimes, id from remote database may be enough for duplication, so we can use array.unique('id').
But sometimes, we may need a fingerprint (such as more key-value or a hash-value) to duplicate objects with complex structure.

@ljharb

This comment has been minimized.

@jridgewell
Copy link
Member

Let's please not discuss unique(prop: string) call signature here, there's already #3.

My point is that we already have [...new Set(array)] to create unique arrays, and the readme doesn't clearly explain why this isn't satisfactory. If the desire is to use the id prop of the object as the hashing key (which I think is a valid reason for this proposal), then you should explain why [...new Set(array)] doesn't work. Eg,

Imagine you have model instances from a database. But, there are duplicate entries in our array (for whatever reason):

    const models = [
      new Model({ id: 1, value: 'foo' }),
      new Model({ id: 2, value: 'bar' }),
      new Model({ id: 1, value: 'foo' }),
    ];

You might be tempted to use `[...new Set(models)]` to create an array containing only unique instances. But if we try this, we'll see that we still have two `foo` models.

    const notReallyUnique = [...new Set(models)];
    assert.equals(notReallyUnique.length, 3); // We still have 3 models!

This is because `Set` uses object identity, and each `Model` instance has it's own unique identity. What we really want to do is use the `id` property to compare values, not object identity.

    const unique = models.unique(model => model.id);
    assert.equals(unique.length, 2); // We dropped the last `foo` model!

@Jack-Works
Copy link
Member Author

we have reached stage 1 so I'm closing this! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants