Skip to content

Commit

Permalink
update String#trim spec compliance
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lewisje committed Aug 23, 2015
1 parent 51eb6c8 commit 53beb7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
15 changes: 7 additions & 8 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,16 +644,15 @@
overrideNative(String.prototype, 'includes', StringPrototypeShims.includes);
}

var hasStringTrimBug = '\u0085'.trim().length !== 1;
// whitespace from: http://es5.github.io/#x15.5.4.20
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
var wsp = '\t\n\v\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' +
'\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF';
var hasStringTrimBug = wsp.trim() || '\x85\u002B'.trim().length !== 2;
if (hasStringTrimBug) {
delete String.prototype.trim;
// whitespace from: http://es5.github.io/#x15.5.4.20
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
var ws = [
'\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
'\u2029\uFEFF'
].join('');
var ws = '\t-\r \xA0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F' +
'\u3000\uFEFF';
var trimRegexp = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
defineProperties(String.prototype, {
trim: function trim() {
Expand Down
19 changes: 13 additions & 6 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,14 @@ var runStringTests = function () {
});

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

var lineTerminators = '\u000a' + '\u000d' + '\u2028' + '\u2029';
var lineTerminators = '\u000A' + '\u000D' + '\u2028' + '\u2029';

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

it('should not trim U+200B', function () {
var trimmed = '\u200B'.trim();
expect(trimmed).to.have.property('length', 1);
expect(trimmed).to.equal('\u200B');
});

it('should trim on both sides', function () {
var trimmed = ' a '.trim();
expect(trimmed).to.have.property('length', 1);
Expand Down

0 comments on commit 53beb7c

Please sign in to comment.