Skip to content

Instantly share code, notes, and snippets.

@phaberest
Created January 18, 2019 17:36
Show Gist options
  • Save phaberest/85f5a48b731d605f7da2db7b444ae984 to your computer and use it in GitHub Desktop.
Save phaberest/85f5a48b731d605f7da2db7b444ae984 to your computer and use it in GitHub Desktop.
Polyfill for js es2015 includes
// includes() polyfill for strings
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
// includes() polyfill for arrays
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, "includes", {
enumerable: false,
value: function(obj) {
var newArr = this.filter(function(el) {
return el == obj;
});
return newArr.length > 0;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment