String prototypes polyfills on Object.defineProperty

This commit is contained in:
Ilya Goncharov
2020-06-01 18:47:31 +03:00
committed by Ilya Goncharov
parent 9431fc4693
commit a476d1dbc4
+16 -12
View File
@@ -4,21 +4,25 @@
*/
if (typeof String.prototype.startsWith === "undefined") {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
};
Object.defineProperty(String.prototype, "startsWith", {
value: function (searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
}
});
}
if (typeof String.prototype.endsWith === "undefined") {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
Object.defineProperty(String.prototype, "endsWith", {
value: function (searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
});
}
// ES6 Math polyfills
if (typeof Math.sign === "undefined") {