Created
March 10, 2016 15:27
-
-
Save jbnv/980e7f23330926c85803 to your computer and use it in GitHub Desktop.
Polyfill for JavaScript.
This file contains 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'; | |
var O = Object(this); | |
var len = parseInt(O.length) || 0; | |
if (len === 0) { | |
return false; | |
} | |
var n = parseInt(arguments[1]) || 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; | |
}; | |
} | |
if (!String.prototype.startsWith) { | |
String.prototype.startsWith = function(searchString, position){ | |
position = position || 0; | |
return this.substr(position, searchString.length) === searchString; | |
}; | |
} | |
if (!String.prototype.contains) { | |
String.prototype.contains = function(it) { return this.indexOf(it) != -1; }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment