Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active November 11, 2018 04:09
Show Gist options
  • Save VitorLuizC/c86b6c1ba6dc675e35def8d7c3c81c9a to your computer and use it in GitHub Desktop.
Save VitorLuizC/c86b6c1ba6dc675e35def8d7c3c81c9a to your computer and use it in GitHub Desktop.
type Dictionary = {
[name: string]: string;
};
type Getter = {
get: () => any;
set: (value: any) => void;
};
type Getters <T extends Dictionary> = {
[name in keyof T]:
};
const createGetter = (name: string, type: string): Getter => ({
get () {
return this.$store.getters[type];
},
set (value) {
console.warn(`Can't assign a value to "${name}", "${name}" is a binder for Vuex "${type}" getter.`);
}
});
const mapGetters = <T extends Dictionary> (dictionary: T): Getters<T> => {
return Object.keys(dictionary).reduce((getters, name) => {
getters[name] = createGetter(name, dictionary[name]);
return getters;
}, Object.create(null));
};
export default mapGetters;
const createGetter = (name, type) => ({
[name]: {
get () {
const value = this.$store.getters[type];
return value;
},
set (value) {
console.warn(`Can't assign a value to "${name}", "${name}" is a binder for Vuex "${type}" getter.`);
}
}
});
const mapGetters = (object) => {
const entries = Object.entries(object);
const getters = entries.reduce(
(getters, [ name, type ]) => ({
...getters,
...createGetter(name, type)
}),
Object.create(null)
);
return getters;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment