Accepting an array as the first parameter to String.prototype.includes

Edwin Reynoso eorroe at gmail.com
Wed Mar 11 02:06:14 UTC 2015


Well the current ES6 `.includes()` was before named `.contains()`:
[String.prototype.includes] (
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#String.prototype.contains
)

But if Garrett Smith was trying to point out that `.contains()` would be
better naming for the specific purpose of using an Array:
`str.contains(["Mary", "Bob"])` then ok? maybe.

This is what I originally had in mind:

```
String.prototype.includesAll = function(arr, index, onlyOne){
  return arr[onlyOne ? "some" : "every"](el => this.includes(el, index),
this);
}
```

But like Bergi said "denotative" the above with the `onlyOne` argument
would also make this function denotative as well, and if you wanted to use
that argument you would have to specify the 0 as the index all the time.

So perhaps two functions

```
String.prototype.containsAll = function(arr, index) {
   return arr.every(el => return this.includes(el, index), this);
}

String.prototype.contains= function(arr, index) {
  return arr.some(el => return this.includes(el, index), this);
}

```

Origianlly I had the names `includesAll` and `includesOneOf`, but
`contains` and `containsAll` is more human readable.

Andrea Giammarchi said:

> I'm still having hard time understanding what's the difference between
contains and the good old indexOf

Well like Garrett Smith said it is easier to read, and it gets rid of
`.indexOf() > -1` which I consider a hack.

Why I consider a hack simply because `.indexOf()` is used to return the
index, then adding `> -1` or using a tilda `~` to make it a Boolean. Why?

Also mixing that with a `some/every` function? and passing a callback, yea
I'd choose:

`str.contains(["Mary", "Bob"])` over:

```
["Mary", "Bob"].every(el => el.indexOf(el) > -1);

//OR

["Mary", "Bob"].every(el => el.includes(el));

```

Also using `indexOf()` if you want to start from a certain index would add
more complexity.

On Tue, Mar 10, 2015 at 7:48 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> not sure I understand what you say but `["maria", "marianne"].some(str.contains,
> str)` means `["maria", "marianne"].some(function (value) {return
> this.contains(value)}, str)` ... no bind nor arrow function needed, the
> function `str.contains` is executed with `str` as context per each
> iteration. Is that confusing? 'cause that's how
> some/every/forEach/map/filter work
>
> On Tue, Mar 10, 2015 at 10:18 PM, Garrett Smith <dhtmlkitchen at gmail.com>
> wrote:
>
>> On 3/10/15, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:
>> > contains better than indexOf ? I'd agree only if contains wasn't
>> accepting
>> > any extra argument, which makes it even more (pointless?) similar to
>> > indexOf.
>> >
>> > If it had only one incoming parameter, you could have `["maria",
>> > "marianne"].some(str.contains, str)` and win over all other examples.
>> but
>> > just the `!=-1` or `>-1` as reason to prefer contains? ... dunno, I
>> think I
>> > have 99 problems in JS related development, `>-1` ain't one :-/
>> >
>>
>> Evidently.
>>
>> I can only guess that str is a string. Strings don't have a contains
>> method, nor was one proposed the call str.contains(str) inside of a
>> call to some looks like a confused mistake.
>> --
>> Garrett
>> @xkit
>> ChordCycles.com
>> garretts.github.io
>> personx.tumblr.com
>>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150310/4260a4c6/attachment.html>


More information about the es-discuss mailing list