Skip to content

Instantly share code, notes, and snippets.

@FxIII
Last active December 19, 2015 08:39
Show Gist options
  • Save FxIII/5927226 to your computer and use it in GitHub Desktop.
Save FxIII/5927226 to your computer and use it in GitHub Desktop.
underscore mixin
// as python setdefault: put defaultValue in object if object[key] is undefined
_.mixin({
setdefault:function(object,key,defaultValue){
var d ={};
d[key]=defaultValue;
return _(object).defaults(d)[key];
}
});
// returns the list of indexes matching a filterFunction
_.mixin({
indexes:function(list,filterFunction){
return _(list).chain().map(function(i,k){
return [k,i];
}).filter(function(i){
return filterFunction(i[1]);
}).pluck(0).value();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment