matcher: function (item) {
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
}
Why there is "~" bitwise operator used in this method? matcher: function (item) {
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
}
Why there is "~" bitwise operator used in this method?Basically, it's a smug version of:
return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1;
Writing it like this is a lot clearer. Furthermore it even returns `true` and `false` instead of some truthy/falsy integer.I do agree that it's bad style, because it doesn't clearly communicate the intention. If you want something that returns true or false, you should return true or false.
Bitwise NOT on -1 will give you 0 -- for any other number, it'll return a truthy value.
It's the same as:
`return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1`
If you're really dead set on doing things this way, it's safer to prefix with !! to coerce a real boolean:
`return !!~item.toLowerCase().indexOf(this.query.toLowerCase())`