Skip to content

Commit

Permalink
Add polyfills for String.prototype.trim{Left,Right}()
Browse files Browse the repository at this point in the history
Most browsers have had these methods since the ES5 era,
but they were never officially part of the language until ES_2019,
so some (e.g. IE11) don't.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=255615448
  • Loading branch information
brad4d authored and blickly committed Jun 28, 2019
1 parent 51271a0 commit dc29495
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/com/google/javascript/jscomp/js/es6/string/trimend.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
*/
'require util/polyfill';

// Most browsers implemented trimRight around ES5-time-frame, but it wasn't
// officially part of the language specification until ES_2019, so we have
// to provide a polyfill for it.
// IE11 doesn't have it, of course...
$jscomp.polyfill('String.prototype.trimRight', function(orig) {
/**
* @this {string}
* @return {string}
*/
function polyfill() {
return this.replace(/[\s\xa0]+$/, '');
}
return orig || polyfill;
}, 'es_2019', 'es3');

$jscomp.polyfill('String.prototype.trimEnd', function(orig) {
return orig || String.prototype.trimRight;
}, 'es_2019', 'es3');
15 changes: 15 additions & 0 deletions src/com/google/javascript/jscomp/js/es6/string/trimstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
*/
'require util/polyfill';

// Most browsers implemented trimLeft around ES5-time-frame, but it wasn't
// officially part of the language specification until ES_2019, so we have
// to provide a polyfill for it.
// IE11 doesn't have it, of course...
$jscomp.polyfill('String.prototype.trimLeft', function(orig) {
/**
* @this {string}
* @return {string}
*/
function polyfill() {
return this.replace(/^[\s\xa0]+/, '');
}
return orig || polyfill;
}, 'es_2019', 'es3');

$jscomp.polyfill('String.prototype.trimStart', function(orig) {
return orig || String.prototype.trimLeft;
}, 'es_2019', 'es3');
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/js/polyfills.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ String.prototype.padStart es8 es3 es6/string/padstart
String.prototype.repeat es6 es3 es6/string/repeat
String.prototype.startsWith es6 es3 es6/string/startswith
String.prototype.trimEnd es_2019 es3 es6/string/trimend
String.prototype.trimLeft es_2019 es3 es6/string/trimstart
String.prototype.trimRight es_2019 es3 es6/string/trimend
String.prototype.trimStart es_2019 es3 es6/string/trimstart
String.raw es6 es6
WeakMap es6 es3 es6/weakmap
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/resources.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ const testSuite = goog.require('goog.testing.testSuite');
testSuite({
testTrimEnd_noChange() {
assertEquals('a', 'a'.trimEnd());
assertEquals('a', 'a'.trimRight());
assertEquals('ab', 'ab'.trimEnd());
assertEquals('ab', 'ab'.trimRight());
assertEquals('a b', 'a b'.trimEnd());
assertEquals('a b', 'a b'.trimRight());
assertEquals(' <-', ' <-'.trimEnd());
assertEquals(' <-', ' <-'.trimRight());
assertEquals('\t<-', '\t<-'.trimEnd());
assertEquals('\t<-', '\t<-'.trimRight());
},

testTrimEnd_change() {
assertTrue('->', '-> '.trimEnd());
assertTrue('->', '-> '.trimEnd());
assertTrue('->', '->\t'.trimEnd());
assertEquals('->', '-> '.trimEnd());
assertEquals('->', '-> '.trimRight());
assertEquals('->', '-> '.trimEnd());
assertEquals('->', '-> '.trimRight());
assertEquals('->', '->\t'.trimEnd());
assertEquals('->', '->\t'.trimRight());
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ const testSuite = goog.require('goog.testing.testSuite');
testSuite({
testTrimStart_noChange() {
assertEquals('a', 'a'.trimStart());
assertEquals('a', 'a'.trimLeft());
assertEquals('ab', 'ab'.trimStart());
assertEquals('ab', 'ab'.trimLeft());
assertEquals('a b', 'a b'.trimStart());
assertEquals('a b', 'a b'.trimLeft());
assertEquals('-> ', '-> '.trimStart());
assertEquals('-> ', '-> '.trimLeft());
assertEquals('->\t', '->\t'.trimStart());
assertEquals('->\t', '->\t'.trimLeft());
},

testTrimStart_change() {
assertEquals('<-', ' <-'.trimStart());
assertEquals('<-', ' <-'.trimLeft());
assertEquals('<-', '\t<-'.trimStart());
assertEquals('<-', '\t<-'.trimLeft());
assertEquals('<-', ' <-'.trimStart());
assertEquals('<-', ' <-'.trimLeft());
},
});

0 comments on commit dc29495

Please sign in to comment.