From b3249b5f3fe933c206d5a5f78d1d123b058d6e2d Mon Sep 17 00:00:00 2001 From: Martin Valigursky Date: Tue, 31 May 2022 10:31:30 +0100 Subject: [PATCH] Polyfill for String.trimEnd --- src/polyfill/string.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/polyfill/string.js b/src/polyfill/string.js index 3c6b9b016cd..d82d0284944 100644 --- a/src/polyfill/string.js +++ b/src/polyfill/string.js @@ -1,7 +1,7 @@ import { defineProtoFunc } from "./defineProtoFunc.js"; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith#Polyfill -defineProtoFunc(String, 'endsWith', function(search, this_len) { +defineProtoFunc(String, 'endsWith', function (search, this_len) { if (this_len === undefined || this_len > this.length) { this_len = this.length; } @@ -9,7 +9,7 @@ defineProtoFunc(String, 'endsWith', function(search, this_len) { }); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill -defineProtoFunc(String, 'includes', function(search, start) { +defineProtoFunc(String, 'includes', function (search, start) { 'use strict'; if (typeof start !== 'number') { start = 0; @@ -23,6 +23,11 @@ defineProtoFunc(String, 'includes', function(search, start) { }); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#Polyfill -defineProtoFunc(String, 'startsWith', function(search, pos) { +defineProtoFunc(String, 'startsWith', function (search, pos) { return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; }); + +// https://vanillajstoolkit.com/polyfills/stringtrimend/ +defineProtoFunc(String, 'trimEnd', function () { + return this.replace(new RegExp(/[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]+/.source + '$', 'g'), ''); +});