String trim methods polyfill for 140byt.es
-
-
Save eliperelman/1036520 to your computer and use it in GitHub Desktop.
String trim methods polyfill for 140byt.es
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(a,b){for(b in a)String.prototype[b]=b[b]||Function('return this.replace('+a[b]+',"")')}({trimLeft:/^\s+/,trimRight:/\s+$/}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "StringPolyfillTrimmingMethods", | |
"description": "String polyfill trimming methods for non-ECMAScript5 environments", | |
"keywords": [ | |
"string", | |
"prototype", | |
"trim", | |
"polyfill", | |
"ecmascript5" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
D’oh.
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
@mathiasbynens another gotcha is the escaping:
will turn into
'/^s+/'