Created
January 18, 2019 17:36
-
-
Save phaberest/85f5a48b731d605f7da2db7b444ae984 to your computer and use it in GitHub Desktop.
Polyfill for js es2015 includes
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
// 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