Created
July 25, 2019 04:27
-
-
Save stwilz/8bcba580cc5b927d7993cddb5dfb4cb1 to your computer and use it in GitHub Desktop.
A curry utility for passing additional arguments to `mapGetters`.
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
import { mapGetters } from "vuex"; | |
const curryMapGetters = args => (namespace, getters) => | |
Object.entries(mapGetters(namespace, getters)).reduce( | |
(acc, [getter, fn]) => ({ | |
...acc, | |
[getter]: state => | |
fn.call(state)(...(Array.isArray(args) ? args : [args])) | |
}), | |
{} | |
); |
Rewrote a bit to make it more readable (https://gist.github.com/bekliev/5fbd348054d2f57bc0b0af0720b9cfff):
import { mapGetters } from 'vuex';
const curryMapGetters = (...args) => (namespace, getters) => {
const getters = mapGetters(namespace, getters);
const entries = Object.entries(getters);
const initialAccumulator = {};
return entries.reduce((acc, [getterName, getterHandler]) => {
acc[getterName] = (state) => {
return getterHandler.call(state)(...args);
};
return acc;
}, initialAccumulator);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great! thank you