Created
September 18, 2016 17:28
-
-
Save Fabiantjoeaon/df05f5a093b17c7565e317f043b944d8 to your computer and use it in GitHub Desktop.
ES7 array includes polyfill (replaces indexOf === -1) src: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Array.prototype.includes) { | |
Array.prototype.includes = function(searchElement /*, fromIndex*/) { | |
'use strict'; | |
if (this == null) { | |
throw new TypeError('Array.prototype.includes called on null or undefined'); | |
} | |
var O = Object(this); | |
var len = parseInt(O.length, 10) || 0; | |
if (len === 0) { | |
return false; | |
} | |
var n = parseInt(arguments[1], 10) || 0; | |
var k; | |
if (n >= 0) { | |
k = n; | |
} else { | |
k = len + n; | |
if (k < 0) {k = 0;} | |
} | |
var currentElement; | |
while (k < len) { | |
currentElement = O[k]; | |
if (searchElement === currentElement || | |
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN | |
return true; | |
} | |
k++; | |
} | |
return false; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment