Skip to content

Instantly share code, notes, and snippets.

@DeProdigy
Last active December 27, 2015 09:58
Show Gist options
  • Save DeProdigy/7307336 to your computer and use it in GitHub Desktop.
Save DeProdigy/7307336 to your computer and use it in GitHub Desktop.
WDI JS Belt
var WDIBelt = WDIBelt || {};
WDIBelt.version = 0.21;
WDIBelt.each = function(array, func) {
for (var index = 0; index < array.length; index++) {
var element = array[index];
func.call(element, index);
}
}
WDIBelt.map = function(array, func) {
var results = [];
for (var index = 0; index < array.length; index++) {
var element = array[index];
results.push(func(element));
}
return results;
}
WDIBelt.select = function(array, truthTest) {
var results = [];
this.each(array, function(){
if (truthTest(this) === true) {
results.push(this);
}
});
return results;
}
WDIBelt.random = function(array) {
var randIndex = Math.floor(array.length * Math.random())
return array[randIndex];
}
WDIBelt.contains = function(array, item) {
var result = false;
this.each(array, function(){
if (this.toString() === item) {
result = true;
}
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment