Skip to content

Commit

Permalink
Combine String#trim regexes into one. Fixes #284
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 3, 2014
1 parent ff2874d commit 31f20f4
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,14 @@
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
'\u2029\uFEFF'
].join('');
var trimBeginRegexp = new RegExp('^[' + ws + '][' + ws + ']*');
var trimEndRegexp = new RegExp('[' + ws + '][' + ws + ']*$');
var trimRegexp = new RegExp('(^[' + ws + ']+|[' + ws + ']+)$');
defineProperties(String.prototype, {
trim: function() {
if (this === undefined || this === null) {
throw new TypeError("can't convert " + this + " to object");
}
return String(this)
.replace(trimBeginRegexp, "")
.replace(trimEndRegexp, "");
}
return String(this).replace(trimRegexp, "");
}
});
}

Expand Down

3 comments on commit 31f20f4

@cscott
Copy link
Collaborator

@cscott cscott commented on 31f20f4 Sep 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops -- you need the g flag here, or else we only trim on one side! :(

@cscott
Copy link
Collaborator

@cscott cscott commented on 31f20f4 Sep 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it in eb6ba2e (and added a test case)

@ljharb
Copy link
Collaborator Author

@ljharb ljharb commented on 31f20f4 Sep 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks and good catch

Please sign in to comment.