Skip to content

Instantly share code, notes, and snippets.

@stwilz
Created July 25, 2019 04:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stwilz/8bcba580cc5b927d7993cddb5dfb4cb1 to your computer and use it in GitHub Desktop.
Save stwilz/8bcba580cc5b927d7993cddb5dfb4cb1 to your computer and use it in GitHub Desktop.
A curry utility for passing additional arguments to `mapGetters`.
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]))
}),
{}
);
@bekliev
Copy link

bekliev commented Dec 10, 2020

great! thank you

@bekliev
Copy link

bekliev commented Dec 10, 2020

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