String trim methods polyfill for 140byt.es
-
-
Save eliperelman/1036520 to your computer and use it in GitHub Desktop.
!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> |
one second: doesn't your trim function need a return
in it?
Oh man, it used to, it must have gotten lost in the refactoring. Dang, I don't think we're going to make it :(
yeah, this seems terminal. you may be able to still keep trimLeft and trimRight together though, right?
Yeah, definitely. I'll update.
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+$'})
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]+$
D’oh.
The whitespace included in the character class \s
is inconsistent across browsers. Instead a manual check of the required whitespace - is - best.
tough nut to crack. at least we can length-- like this: