Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 20, 2011 20:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliperelman/1036520 to your computer and use it in GitHub Desktop.
Save eliperelman/1036520 to your computer and use it in GitHub Desktop.
String trim methods polyfill for 140byt.es

String trim methods polyfill for 140byt.es

!function(
a, // trimming object that defined String.prototype extensions and their related Regular Expressions
b // placeholder variable for iterating over 'a'
){
for(b in a) // iterate over each of the trimming items in 'a'
String.prototype[b]=b[b] || // use the native string trim/trimLeft/trimRight method if available, if not:
Function('return this.replace('+a[b]+',"")') // generate a function that will return a new string that replaces the relevant whitespace
}({
trimLeft: /^\s+/, // regex to trim the left side of a string (along with prototype name)
trimRight: /\s+$/ // regex to trim the right side of a string (along with prototype name)
})
!function(a,b){for(b in a)String.prototype[b]=b[b]||Function('return this.replace('+a[b]+',"")')}({trimLeft:/^\s+/,trimRight:/\s+$/})
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Eli Perelman <http://eliperelman.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "StringPolyfillTrimmingMethods",
"description": "String polyfill trimming methods for non-ECMAScript5 environments",
"keywords": [
"string",
"prototype",
"trim",
"polyfill",
"ecmascript5"
]
}
<!DOCTYPE html>
<title>String polyfill trimming methods</title>
<script>
!function(a,b){for(b in a)String.prototype[b]=[b]||Function('return this.replace('+a[b]+',"")')}({trimLeft:/^\s+/,trimRight:/\s+$/,trim:/^\s+|\s+$/g})
var str = ' my awesome string ';
// outputs: 'my awesome string '
console.log(str.trimLeft());
// outputs: ' my awesome string'
console.log(str.trimRight());
</script>
@jed
Copy link

jed commented Jun 20, 2011

i'm pretty sure you can get this one there. first up:

!function(a,b){for(b in a)String.prototype[b]=''[b]||Function('this.replace('+a[b]+',"")')}({trimLeft:/^\s+/,trimRight:/\s+$/,trim:/^\s+|\s+$/g})

@eliperelman
Copy link
Author

Awesome start! 5 more characters to go. I was thinking of how I can recycle the word "trim", but I don't think I can without making this entry longer...

@Kambfhase
Copy link

just tried recycling "trim" wont help. Btw, this looks pretty sexy!

@eliperelman
Copy link
Author

Thanks! Yeah, I tried recycle but ended up adding 3 characters. Darn, I feel like it's so close.

@jed
Copy link

jed commented Jun 20, 2011

i think recycling trim might work, but not as an object. working...

@jed
Copy link

jed commented Jun 20, 2011

tough nut to crack. at least we can length-- like this:

!function(a,b){for(b in a)String.prototype[b]=b[b]||Function('this.replace('+a[b]+',"")')}({trimLeft:/^\s+/,trimRight:/\s+$/,trim:/^\s+|\s+$/g})

@jed
Copy link

jed commented Jun 20, 2011

one second: doesn't your trim function need a return in it?

@eliperelman
Copy link
Author

Oh man, it used to, it must have gotten lost in the refactoring. Dang, I don't think we're going to make it :(

@jed
Copy link

jed commented Jun 20, 2011

yeah, this seems terminal. you may be able to still keep trimLeft and trimRight together though, right?

@eliperelman
Copy link
Author

Yeah, definitely. I'll update.

@mathiasbynens
Copy link

I think you could save some bytes by hardcoding the opening / of the regex inside the Function()… Oh wait, no, you’d have to make them strings then, which adds 2 bytes in total, and which breaks escaping.

Not using trim in the property keys doesn’t help either. Damn.

// 135 bytes
!function(a,b){for(b in a)String.prototype[b]=b[b]||Function('return this.replace(/'+a[b]+'/,"")')}({trimLeft:'^\s+',trimRight:'\s+$'})
// 136 bytes
!function(a,b){for(b in a)String.prototype[b='trim'+b]=b[b]||Function('return this.replace(/'+a[b]+'/,"")')}({Left:'^\s+',Right:'\s+$'})

@subzey
Copy link

subzey commented Jun 21, 2011

Please note that WhiteSpace definintion differs in ECMA3 and 5: ECMA5 adds BOM (\xFEFF) character.
So, in order to make, for example, ECMA5-compatible trim in ECMA3 we should (or at least I think so) use regexp ^[\s\uFEFF]+|[\s\uFEFF]+$

@jed
Copy link

jed commented Jun 21, 2011

@mathiasbynens another gotcha is the escaping:

'/'+'^\s+'+'/'

will turn into

'/^s+/'

@mathiasbynens
Copy link

D’oh.

@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.

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