Skip to content

Instantly share code, notes, and snippets.

@Erutan409
Created April 4, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erutan409/89d08f605a9a3f79dd54739abd0382dc to your computer and use it in GitHub Desktop.
Save Erutan409/89d08f605a9a3f79dd54739abd0382dc to your computer and use it in GitHub Desktop.
Lodash matchOne mixin
(function (lodash) {
if (!lodash.hasOwnProperty('matchOne')) {
lodash.mixin(lodash, {
matchOne: function () {
var values = lodash(arguments).values(),
test = values.head(),
odd = values.drop(1).filter(function (_void, index) {
return index % 2 === 0;
}).reverse(),
even = values.drop(1).filter(function (_void, index) {
return index % 2 !== 0;
}).reverse();
if (odd.size() > even.size()) {
even.unshift(undefined);
}
var found = undefined;
odd.each(function (value, index) {
if (value === test) {
found = even.get(index);
return false;
}
});
if (lodash.isFunction(found)) {
found = found();
}
return found;
}
}, {chain: false});
}
})(_);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment