Array.prototype.includesAll

# Shahar Or (8 years ago)

Hey, following up from: esdiscuss.org/topic/array-prototype-includes-with-multiple-arguments

How about something like this Array.prototype.includesAll? codepen.io/mightyiam/pen/PzNLKr/?editors=0012

# Jordan Harband (8 years ago)

It'd be nicer if it took an array, rather than being variadic. That also preserves the ability to add extra arguments in the future.

# Oriol Bugzilla (8 years ago)

What's the point of using reduce instead of every?

Array.prototype.includesAll = function (...args) {
  return args.every(item => this.includes(item));
}

I disagree with this test

expect([2, 3].includesAll()).toBe(false)

The array [2,3] includes all items in []. So it should return true.

# Shahar Or (8 years ago)

What's the point of using reduce instead of every?

Of course. Updated to use .every.

I disagree with this test

expect([2, 3].includesAll()).toBe(false)

The array [2,3] includes all items in []. So it should return true.

There are no items in [] so that doesn't seem like a true statement to me. However, one could argue both ways. So I look at .includes:

[].includes() // false
[1]includes() // false
// and so on...

So, at least consistency pulls towards false.

It'd be nicer if it took an array, rather than being variadic. That also

preserves the ability to add extra arguments in the future.

I see the point. Updated to use a single array argument.


Here it is: codepen.io/mightyiam/pen/PzNLKr/?editors=0012

# Renki Ivanko (8 years ago)

There are no items in [] that aren't included in [2,3]. A separate question is whether undefined should mean []; I'd say it should throw a TypeError instead.

# Oriol Bugzilla (8 years ago)

There are no items in [] so that doesn't seem like a true statement to me.

It's true by Vacuous_truth.

So, at least consistency pulls towards false.

You are misunderstanding what includes does when there is no argument.

[1].includes(); // false
[void 0].includes(); // true

Consistency with every pulls towards true.

# Bob Myers (8 years ago)

This proposal is so far from something that should go into the base language that it makes me choke.

Are you also going to propose that we add includesNone and includesSome? Do you want to include an option to sort the items for efficiency? Why do you provide no fromIndex parameter? Shall we include an option to lowercase before checking for equality? How does this relate to the notion of array difference, which is one way to accomplish what you want?

Just write a library that does what you want and get on with your life.

-- Bob

# Tab Atkins Jr. (8 years ago)

On Tue, Jun 14, 2016 at 12:58 PM, Bob Myers <rtm at gol.com> wrote:

This proposal is so far from something that should go into the base language that it makes me choke.

Are you also going to propose that we add includesNone and includesSome? Do you want to include an option to sort the items for efficiency? Why do you provide no fromIndex parameter? Shall we include an option to lowercase before checking for equality? How does this relate to the notion of array difference, which is one way to accomplish what you want?

Just write a library that does what you want and get on with your life.

This is a grossly inappropriate tone and does not belong on the list.

# Alexander Jones (8 years ago)

There is a precedent here from set theory.

A.includesAll(B)

is really the equivalent of:

A is a superset of B

or otherwise put:

B - A = empty set

Hence, if B is the empty set, then it is indeed true for all sets A.

# Isiah Meadows (8 years ago)

I agree that the tone was a bit iffy, but I do support this being in a third party library. I have only a couple times ever had a legitimate need for this, one of which was to write an assertion that I later used. Not sure it's common enough it needs to be in the language. I just don't see the need for it, and I don't feel it would be worth adding to the already huge and complex implementations.