Skip to content

Commit f337db9

Browse files
committed
Update String#trim spec compliance
Check for two commonly erroneously trimmed characters instead of one, and check for erroneously failing to trim the trimmable whitespace characters. I would also replace `if (typeof this === 'undefined' || this === null)` with `if (this == null)` but I don't know whether that's allowed by this library's style rules.
1 parent 51eb6c8 commit f337db9

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

es6-shim.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,17 @@
644644
overrideNative(String.prototype, 'includes', StringPrototypeShims.includes);
645645
}
646646

647-
var hasStringTrimBug = '\u0085'.trim().length !== 1;
647+
// whitespace from: http://es5.github.io/#x15.5.4.20
648+
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
649+
var wsp = '\t\n\v\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' +
650+
'\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF';
651+
var nxtLine = '\x85';
652+
var zeroWidth = '\u200B';
653+
var hasStringTrimBug = wsp.trim() || !nxtLine.trim() || !zeroWidth.trim();
648654
if (hasStringTrimBug) {
649655
delete String.prototype.trim;
650-
// whitespace from: http://es5.github.io/#x15.5.4.20
651-
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
652-
var ws = [
653-
'\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
654-
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
655-
'\u2029\uFEFF'
656-
].join('');
656+
var ws = '\t-\r \xA0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F' +
657+
'\u3000\uFEFF';
657658
var trimRegexp = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
658659
defineProperties(String.prototype, {
659660
trim: function trim() {

test/string.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,14 @@ var runStringTests = function () {
587587
});
588588

589589
it('should trim the correct characters', function () {
590-
var whitespace = '\u0009' + '\u000b' + '\u000c' + '\u0020' +
591-
'\u00a0' + '\u1680' + '\u2000' + '\u2001' +
592-
'\u2002' + '\u2003' + '\u2004' + '\u2005' +
593-
'\u2006' + '\u2007' + '\u2008' + '\u2009' +
594-
'\u200A' + '\u202f' + '\u205f' + '\u3000';
590+
var whitespace = '\u0009' + '\u000B' + '\u000C' + '\u0020' +
591+
'\u00A0' + '\u1680' + '\u180E' + '\u2000' +
592+
'\u2001' + '\u2002' + '\u2003' + '\u2004' +
593+
'\u2005' + '\u2006' + '\u2007' + '\u2008' +
594+
'\u2009' + '\u200A' + '\u202F' + '\u205F' +
595+
'\u3000' + '\uFEFF';
595596

596-
var lineTerminators = '\u000a' + '\u000d' + '\u2028' + '\u2029';
597+
var lineTerminators = '\u000A' + '\u000D' + '\u2028' + '\u2029';
597598

598599
var trimmed = (whitespace + lineTerminators).trim();
599600
expect(trimmed).to.have.property('length', 0);
@@ -606,6 +607,12 @@ var runStringTests = function () {
606607
expect(trimmed).to.equal('\u0085');
607608
});
608609

610+
it('should not trim U+200B', function () {
611+
var trimmed = '\u200B'.trim();
612+
expect(trimmed).to.have.property('length', 1);
613+
expect(trimmed).to.equal('\u200B');
614+
});
615+
609616
it('should trim on both sides', function () {
610617
var trimmed = ' a '.trim();
611618
expect(trimmed).to.have.property('length', 1);

0 commit comments

Comments
 (0)