Last active
December 18, 2015 16:29
-
-
Save jhirn/5811820 to your computer and use it in GitHub Desktop.
This file contains 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() { | |
// Underscore methods that we want to implement on Array. | |
var methods = [ | |
'all', | |
'any', | |
'compact', | |
'contains', | |
'countBy', | |
'detect', | |
'difference', | |
'each', | |
'every', | |
'filter', | |
'find', | |
'first', | |
'flatten', | |
'groupBy', | |
'include', | |
'indexOf', | |
'initial', | |
'intersection', | |
'invoke', | |
'isEmpty', | |
'last', | |
'lastIndexOf', | |
'map', | |
'max', | |
'min', | |
'object', | |
'pluck', | |
'range', | |
'reduce', | |
'reduceRight', | |
'reject', | |
'rest', | |
'select', | |
'shuffle', | |
'size', | |
'some', | |
'sortBy', | |
'sortedIndex', | |
'toArray', | |
'union', | |
'uniq', | |
'where', | |
'without', | |
'zip' | |
]; | |
// Array extensions | |
// Mix in each method as a proxy. | |
_.each(methods, function(method) { | |
Array.prototype[method] = function() { | |
return _[method].apply(this, [this].concat(_.toArray(arguments))); | |
}; | |
}); | |
Array.prototype.isEmpty = function() { | |
return this.length < 1; | |
}; | |
Array.prototype.isNotEmpty = function() { | |
return this.length > 0; | |
}; | |
Array.prototype.sum = function() { | |
return _.reduce(this, function(memo, num) { | |
return memo + num; | |
}, 0); | |
}; | |
// String extensions | |
String.prototype.capitalize = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1); | |
}; | |
String.prototype.toNumber = function() { | |
return parseInt(this, 10); | |
}; | |
String.prototype.humanize = function() { | |
return this.capitalize().replace(/-|_/, ' '); | |
}; | |
String.prototype.hyphenate = function() { | |
return this.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s|_/g, '-').toLowerCase(); | |
}; | |
String.prototype.truncate = function(length) { | |
return (this.length > length) ? this.substring(0, length) + '...' : this; | |
}; | |
String.prototype.to_i = function() { | |
return parseInt(this, 10); | |
}; | |
String.prototype.to_cssClassName = function() { | |
return this.split(' ').join('_').toLowerCase(); | |
}; | |
this.isTypeOf = function(type, suspect) { | |
return suspect.constructor === type; | |
}; | |
this.isNotTypeOf = function(type, suspect) { | |
if (typeof suspect === "undefined") { | |
throw new Error("isNotTypeOf(type, suspect): suspect is undefined"); | |
} | |
return suspect.constructor !== type; | |
}; | |
// Create a hash of query params | |
location.params = (function() { | |
var data = {}; | |
var query = window.location.href.toString().split("?"); | |
if (query.length === 1) { return data; } | |
var pairs = query[1].split("&"); | |
for (var i = 0; i < pairs.length; i++) { | |
var vals = pairs[i].split("="); | |
data[vals[0]] = vals[1]; | |
} | |
return data; | |
})(); | |
if (!String.concat) { | |
String.concat = function() { | |
return String.prototype.concat.apply('', arguments); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment