Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update String#trim #354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,16 +644,17 @@
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 nxtLine = '\x85';
var zeroWidth = '\u200B';
var hasStringTrimBug = wsp.trim() || !nxtLine.trim() || !zeroWidth.trim();
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