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

“index” vs. function parameter: what’s the difference? #3

Open
mathiasbynens opened this issue Jun 12, 2020 · 15 comments
Open

“index” vs. function parameter: what’s the difference? #3

mathiasbynens opened this issue Jun 12, 2020 · 15 comments
Assignees
Labels
question Further information is requested

Comments

@mathiasbynens
Copy link
Member

Question about https://github.com/TechQuery/array-unique-proposal#core-features:

While Array.prototype.unique() invoked with:

  1. no parameter, it'll work as [...new Set(array)];

  2. one index-key parameter (Number, String or Symbol), it'll get values from each array element with the key, and then deduplicates the origin array based on these values;

  3. one function parameter, it'll call this function for each array element, and then deduplicates the origin array based on these returned values.

How is 2 different from 3?

@Jack-Works
Copy link
Member

2 is an abbreviated version of 3.

"key" equals to (x) => x.key

Same for number and symbols.

@ljharb
Copy link
Member

ljharb commented Jun 12, 2020

I think I'd be pretty confused if I learned that shortcut pattern on unique and found it wasn't available on all the other predicate-taking array methods.

@Jack-Works
Copy link
Member

I think I'd be pretty confused if I learned that shortcut pattern on unique and found it wasn't available on all the other predicate-taking array methods.

maybe we can add that

@ljharb
Copy link
Member

ljharb commented Jun 12, 2020

That seems like it'd be better as a separate cross-cutting proposal, if so

@jridgewell
Copy link
Member

Agreed with @ljharb here. I think we should drop 2 for now, and possibly add it as its own proposal with all of the other array methods (particularly map).

@hax
Copy link
Member

hax commented Jul 10, 2020

I think unique key is very common use case and I hope it could be included in this proposal.

@ljharb
Copy link
Member

ljharb commented Jul 10, 2020

@hax I agree it's common; but it's already common for every array method that takes a function, not just this new one.

@hax
Copy link
Member

hax commented Jul 11, 2020

@ljharb I think it's possible to add key param for other array methods, but consider unique() potentially could support multiple keys ( #7 ) which unlikely fit for other methods, I suppose the key usage in unique is different than other array methods.

@ljharb
Copy link
Member

ljharb commented Jul 11, 2020

Anything with a predicate seems like it’d apply the same to single or multiple keys.

@hax
Copy link
Member

hax commented Jul 11, 2020

Maybe, but I feel arr.filter(["a", "b"]), arr.find("a"), arr.some(['a', 'b']) are confusing.

@ljharb
Copy link
Member

ljharb commented Jul 11, 2020

I do too, but I’d think the same about unique with an array.

@jridgewell jridgewell mentioned this issue Jul 13, 2020
8 tasks
@Jack-Works
Copy link
Member

in the last meeting, the committee requires to remove the key: string | symbol signature. it should be a separate proposal to add this kind of feature to all Array methods. @TechQuery

@TechQuery TechQuery self-assigned this Jul 28, 2020
@TechQuery TechQuery added the question Further information is requested label Jul 28, 2020
@TechQuery
Copy link
Collaborator

@ljharb @jridgewell
Only map() may be possible to add a String-parameter signature, for example objects.map('name'), adding a string parameter to other Array methods with a callback parameter may confuse developers:

  • filter(): keep the elements which has an Equal string value or Truthy key-value?
  • findIndex(): find an element index which has an Equal string value or Truthy key-value?
  • some(): detect an element which has an Equal string value or Truthy key-value?
  • every(): detect every element has an Equal string value or Truthy key-value?
  • sort(): sort elements by a string key, but Ascending or Descending?

@ljharb
Copy link
Member

ljharb commented Sep 10, 2020

given the similarity between .map(() => 'name') and .map('name') i'd say the same potential confusion applies anywhere - or, if not, doesn't apply anywhere.

@hax
Copy link
Member

hax commented Sep 10, 2020

I suggest we remove index parameter, and make non-function param throw TypeError so we could reconsider it in the future.

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

No branches or pull requests

6 participants