Last active
December 19, 2015 08:39
-
-
Save FxIII/5927226 to your computer and use it in GitHub Desktop.
underscore mixin
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
// 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]; | |
} | |
}); |
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
// 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