Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Snippets: Augmented Native Objects
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");
}
@FabianBeiner

This comment has been minimized.

Copy link

FabianBeiner commented Jun 5, 2011

Here a version without regex, just in case. :) Then again, regex will be faster here...

String.prototype.camelize = function() {
  return this.split(" ").map( function(w) { return w.substr(0, 1).toUpperCase() + w.substr(1); } ).join("");
}

But why do you want to return the empty string, I think that's useless here:

String.prototype.camelize = function() {
  return this.replace(/(\s)([a-zA-Z])/g, function(str, p1, p2) {
    return p2.toUpperCase();
  });
};
@cfleschhut

This comment has been minimized.

Copy link
Owner Author

cfleschhut commented Jul 31, 2011

Nice addition, thanks! I updated the camelize method.

@cfleschhut

This comment has been minimized.

Copy link
Owner Author

cfleschhut commented Aug 14, 2011

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
You can’t perform that action at this time.