Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 20, 2011 16:53
Show Gist options
  • Save eliperelman/1035982 to your computer and use it in GitHub Desktop.
Save eliperelman/1035982 to your computer and use it in GitHub Desktop.
String.prototype.trim polyfill for 140byt.es
function(){
/* Rules: // make sure
(1) anonymous function // to annotate
(2) optionally self-executing // your code
(3) <140 bytes // so everyone
(4) no global vars // can learn
(5) MIT license // from it!
(6) good luck!
*/}
function(){/*Rules: (1) anonymous function (2) optionally self-executing (3) <140 bytes (4) no global vars (5) MIT license (6) good luck!*/}
{
"name": "sample",
"keywords": [ "sample", "original" ]
}
@jdalton
Copy link

jdalton commented Aug 21, 2011

The whitespace included in the character class \s is inconsistent across browsers. Instead a manual check of the required whitespace - is - best.
Also browsers like Chrome have a bug where String.prototype.trim = ''.trim will cause String#trim to become enumerable.

@eliperelman
Copy link
Author

Fixing the enumerable issue. I'm not sure yet if I can tackle making it any more robust as the character class alone to resolve the whitespace issue looks to be over 140 characters. I will research.

@eliperelman
Copy link
Author

subzey commented in another gist of mine that the following regex might work: ^[\s\uFEFF]+|[\s\uFEFF]+$. I will use it unless you think there is something non-compliant with it.

@chrisjacob
Copy link

Did you happen to find any issues with the regex you implemented?

@ivomarsan
Copy link

return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment