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") { if (typeof String.prototype.startsWith === "undefined") {
String.prototype.startsWith = function(searchString, position) { Object.defineProperty(String.prototype, "startsWith", {
position = position || 0; value: function (searchString, position) {
return this.lastIndexOf(searchString, position) === position; position = position || 0;
}; return this.lastIndexOf(searchString, position) === position;
}
});
} }
if (typeof String.prototype.endsWith === "undefined") { if (typeof String.prototype.endsWith === "undefined") {
String.prototype.endsWith = function(searchString, position) { Object.defineProperty(String.prototype, "endsWith", {
var subjectString = this.toString(); value: function (searchString, position) {
if (position === undefined || position > subjectString.length) { var subjectString = this.toString();
position = subjectString.length; 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 // ES6 Math polyfills
if (typeof Math.sign === "undefined") { if (typeof Math.sign === "undefined") {