Skip to content

Instantly share code, notes, and snippets.

@jdlich
Created October 10, 2011 23:33
Show Gist options
  • Save jdlich/1276871 to your computer and use it in GitHub Desktop.
Save jdlich/1276871 to your computer and use it in GitHub Desktop.
//
// Number
//
// (num).times(function () {})
Number.prototype.times = function (value) {
for (var i = 0; i < this; i++) {
value();
}
}
//
// Arrays
//
// arr.sum()
Array.prototype.sum = function () {
var sum = 0;
for ( var i = 0; i < this.length; i++ ) {
// TODO: add type coercion
sum += this[i];
}
return sum;
}
//
// Strings
//
// str.capitalize()
String.prototype.capitalize = function () {
// return this.replace(/^\w/, function ($0) { return $0.toUpperCase(); });
return this.charAt(0).toUpperCase() + this.slice(1);
}
// str.toUnderscore()
String.prototype.toUnderscore = function () {
return this.replace(/[A-Z]/g, function ($0) {
return "_" + $0.toLowerCase();
});
}
// str.toCamelCase()
String.prototype.toCamelCase = function () {
return this.replace(/_\w/g, function ($0) {
return $0.replace('_','').toUpperCase();
});
}
//
// Utility
//
// sleep(seconds)
var sleep = function (seconds) {
var start = new Date().getSeconds(),
stop = new Date().setSeconds(start + seconds);
while (new Date() < stop);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment