Last active
November 11, 2018 04:09
-
-
Save VitorLuizC/c86b6c1ba6dc675e35def8d7c3c81c9a to your computer and use it in GitHub Desktop.
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
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; |
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
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