Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created March 10, 2016 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbnv/980e7f23330926c85803 to your computer and use it in GitHub Desktop.
Save jbnv/980e7f23330926c85803 to your computer and use it in GitHub Desktop.
Polyfill for JavaScript.
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