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

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Mar 10 21:37:37 UTC 2015


also, just for people not afraid of the tilde

`["Maria", "Mariana"].some(e=>~a.indexOf(e));`

yeah, you can use it ^_^


On Tue, Mar 10, 2015 at 9:34 PM, 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 :-/
>
> On Tue, Mar 10, 2015 at 8:42 PM, Garrett Smith <dhtmlkitchen at gmail.com>
> wrote:
>
>> On 3/10/15, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:
>> > I'm still having hard time understanding what's the difference between
>> > contains and the good old indexOf beside the RegExp check ... but I
>> agree
>> > having multiple explicit searches instead of multiple implicit searches
>> > won't make such big difference. Good news is, you probably will still
>> use
>> > RegExp anyway because names can be composed and haveing a \bName\b
>> helper
>> > is probably what you might need anyway :-)
>> >
>> > i.e. Maria !== Marianne
>> >
>>
>> What if Array had a contains or containsAll functions?
>>
>>  var s = "Maria, Mariana";
>>  var a = s.split(/\s*,\s*/);
>>  ["Maria", "Mariana"].every(e=>{return a.indexOf(e)!=-1});
>>  true
>>
>> But a `contains` function might be better than indexOf
>>
>>  ["Maria", "Mariana"].every(e=>{return a.contains(e)});
>>
>> But `containsAll` might be even better.
>>
>>  a.containsAll(["Maria", "Mariana"]) seems even easier to read for me.
>>
>> There is already `filter` for exclusions. What about merging arrays?
>>
>> Array.union(array2, array3, ...);
>>
>>
>> > On Tue, Mar 10, 2015 at 3:31 PM, Bergi <a.d.bergi at web.de> wrote:
>> >
>> >> Edwin Reynoso wrote:
>> >>
>> >> > There are times where I would like to check whether a string has
>> every
>> >> > occurrence of certain strings/numbers:
>> >> >
>> >> > Now to achieve what I would like `String.prototype.includes` to
>> >> accomplish
>> >> > with an array as the first parameter, I currently take the following
>> >> > approach:
>> >> >
>> >> > var str = "John,Mary,Bob,Steve";
>> >> > var names = ["Mary", "Bob"];
>> >> > names.every(name => str.includes(name)); // true;
>> >>
>> >> And that's perfectly fine imho, pretty expressive about what is done
>> >> about
>> >> the array. Just passing an array to `.includes` is rather meaningless
>> >> (not
>> >> denotative).
>> >>
>> >> If we need a method to do this (to allow for native optimisations with
>> >> fancy string search algorithms), I'd suggest to use a different method
>> >> name
>> >> like `String.prototype.includesAll`.
>> >>
>> >>  Bergi
>> >> _______________________________________________
>> >> es-discuss mailing list
>> >> es-discuss at mozilla.org
>> >> https://mail.mozilla.org/listinfo/es-discuss
>> >>
>> >
>>
>>
>> --
>> Garrett
>> @xkit
>> ChordCycles.com
>> garretts.github.io
>> personx.tumblr.com
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150310/9ad8bfae/attachment.html>


More information about the es-discuss mailing list