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

How to deal with empty items #6

Open
hax opened this issue Jul 11, 2020 · 5 comments
Open

How to deal with empty items #6

hax opened this issue Jul 11, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@hax
Copy link
Member

hax commented Jul 11, 2020

What [1, , 2, , , 2, 1].unique() should return?

At least there are three options:

  1. Treat empty items as undefined, so returns [1, undefined, 2].
    This matches what [...new Set(arr)] do as README, but I suppose it's not intentional. Note if unique(f), f would be called on every empty items as undefined, (or called with no param?) (To avoid runtime error, if f is a key, I guess it should be treat as x => x?.[key])
  2. Skip all empty items and keep them, so returns [1, , 2, , , ,]
  3. Skip all empty items and drop them, so returns [1, 2]

Personally I prefer the last option.

@ljharb
Copy link
Member

ljharb commented Jul 11, 2020

It’s intentional from array spread, as in most new array methods, to pretend sparse arrays don’t exist, and to treat them as undefined as well.

@jridgewell
Copy link
Member

jridgewell commented Jul 11, 2020

It’s intentional from array spread, as in most new array methods, to pretend sparse arrays don’t exist, and to treat them as undefined as well.

Can you specify which array methods? As far as I know, for every array method, the predicate is skipped when the item is empty. That would be option 3 here.

@hax
Copy link
Member Author

hax commented Jul 11, 2020

as in most new array methods

Very interesting... So I checked the "new array methods"

  • spread/from/entries/keys/values (2015): iterable related and all treat empty items as undefined
  • of/fill (2015): not related to empty items
  • copyWithIn(2015): move empty items as is
  • find/findIndex(2015): treat empty items as undefined
  • includes (2016): treat empty items as undefined
  • flat/flatMap (2019): skip empty items

So at least copyWithin and flat/flatMap still respect empty items.

I feel unique is much closer to filter, flat cases.

And if someone really want treat empty items as undefined, they could use [...arr].unique() 🤓

@TechQuery TechQuery added the enhancement New feature or request label Jul 14, 2020
@TechQuery TechQuery self-assigned this Jul 14, 2020
@TechQuery
Copy link
Collaborator

@hax As the semantic of unique, [1, undefined, 2] maybe make sense.
If someone needs the 3rd option, array.filter(Boolean).unique() is easy for him/her.

TechQuery added a commit that referenced this issue Jul 14, 2020
[optimize] add Mutation notice in ReadMe
@hax
Copy link
Member Author

hax commented Jul 18, 2020

@TechQuery Ideally no one should use arrays with empty slots anymore. It's unclear whether empty slots have different meaning with intentional undefined item in specific usage, so I think it's very hard to say which one make much sense. The point here is which behavior should be used in unique by default. In either choice, developers need a extra operation if they want another semantic.

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

No branches or pull requests

4 participants