Created
June 5, 2011 18:39
-
-
Save cfleschhut/1009258 to your computer and use it in GitHub Desktop.
JavaScript Snippets: Augmented Native Objects
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
String.augmentedProperties = []; | |
if (!String.prototype.camelize) { | |
String.prototype.camelize = function() { | |
return this.replace(/(\s)([a-zA-Z])/g, function(str, p1, p2) { | |
return p2.toUpperCase(); | |
}); | |
}; | |
String.augmentedProperties.push("camelize"); | |
} | |
if (!String.prototype.reverse) { | |
String.prototype.reverse = function() { | |
return this.split("").reverse().join(""); | |
}; | |
String.augmentedProperties.push("reverse"); | |
} | |
if (!String.prototype.trim) { | |
String.prototype.trim = function() { | |
return this.replace(/^\s+|\s+$/g, ""); | |
}; | |
String.augmentedProperties.push("trim"); | |
} | |
Array.augmentedProperties = []; | |
if (!Array.prototype.inArray) { | |
Array.prototype.inArray = function(needle) { | |
for(var i=0; i<this.length; i++) { | |
if (this[i] == needle) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
Array.augmentedProperties.push("inArray"); | |
} |
Nice addition, thanks! I updated the camelize method.
complete, comprehensive collection => http://sugarjs.com/api
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here a version without regex, just in case. :) Then again, regex will be faster here...
But why do you want to return the empty string, I think that's useless here: