Skip to content

Commit 75edfc5

Browse files
lanzhihengktsn
authored andcommitted
Add some js Doc for helper (#1100)
* Add some js Doc for helper * Add some js Doc for mapxxxx function * Fix some typo and parameters in comments
1 parent e3ae423 commit 75edfc5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/helpers.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Reduce the code which written in Vue.js for getting the state.
3+
* @param {String} [namespace] - Module's namespace
4+
* @param {Object|Array} states # Object's item can be a function which accept state and getters for param, you can do something for state and getters in it.
5+
* @param {Object}
6+
*/
17
export const mapState = normalizeNamespace((namespace, states) => {
28
const res = {}
39
normalizeMap(states).forEach(({ key, val }) => {
@@ -22,10 +28,17 @@ export const mapState = normalizeNamespace((namespace, states) => {
2228
return res
2329
})
2430

31+
/**
32+
* Reduce the code which written in Vue.js for committing the mutation
33+
* @param {String} [namespace] - Module's namespace
34+
* @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept anthor params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
35+
* @return {Object}
36+
*/
2537
export const mapMutations = normalizeNamespace((namespace, mutations) => {
2638
const res = {}
2739
normalizeMap(mutations).forEach(({ key, val }) => {
2840
res[key] = function mappedMutation (...args) {
41+
// Get the commit method from store
2942
let commit = this.$store.commit
3043
if (namespace) {
3144
const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
@@ -42,9 +55,16 @@ export const mapMutations = normalizeNamespace((namespace, mutations) => {
4255
return res
4356
})
4457

58+
/**
59+
* Reduce the code which written in Vue.js for getting the getters
60+
* @param {String} [namespace] - Module's namespace
61+
* @param {Object|Array} getters
62+
* @return {Object}
63+
*/
4564
export const mapGetters = normalizeNamespace((namespace, getters) => {
4665
const res = {}
4766
normalizeMap(getters).forEach(({ key, val }) => {
67+
// thie namespace has been mutate by normalizeNamespace
4868
val = namespace + val
4969
res[key] = function mappedGetter () {
5070
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
@@ -62,10 +82,17 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
6282
return res
6383
})
6484

85+
/**
86+
* Reduce the code which written in Vue.js for dispatch the action
87+
* @param {String} [namespace] - Module's namespace
88+
* @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
89+
* @return {Object}
90+
*/
6591
export const mapActions = normalizeNamespace((namespace, actions) => {
6692
const res = {}
6793
normalizeMap(actions).forEach(({ key, val }) => {
6894
res[key] = function mappedAction (...args) {
95+
// get dispatch function from store
6996
let dispatch = this.$store.dispatch
7097
if (namespace) {
7198
const module = getModuleByNamespace(this.$store, 'mapActions', namespace)
@@ -82,19 +109,36 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
82109
return res
83110
})
84111

112+
/**
113+
* Rebinding namespace param for mapXXX function in special scoped, and return them by simple object
114+
* @param {String} namespace
115+
* @return {Object}
116+
*/
85117
export const createNamespacedHelpers = (namespace) => ({
86118
mapState: mapState.bind(null, namespace),
87119
mapGetters: mapGetters.bind(null, namespace),
88120
mapMutations: mapMutations.bind(null, namespace),
89121
mapActions: mapActions.bind(null, namespace)
90122
})
91123

124+
/**
125+
* Normalize the map
126+
* normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
127+
* normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
128+
* @param {Array|Object} map
129+
* @return {Object}
130+
*/
92131
function normalizeMap (map) {
93132
return Array.isArray(map)
94133
? map.map(key => ({ key, val: key }))
95134
: Object.keys(map).map(key => ({ key, val: map[key] }))
96135
}
97136

137+
/**
138+
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
139+
* @param {Function} fn
140+
* @return {Function}
141+
*/
98142
function normalizeNamespace (fn) {
99143
return (namespace, map) => {
100144
if (typeof namespace !== 'string') {
@@ -107,6 +151,13 @@ function normalizeNamespace (fn) {
107151
}
108152
}
109153

154+
/**
155+
* Search a special module from store by namespace. if module not exist, print error message.
156+
* @param {Object} store
157+
* @param {String} helper
158+
* @param {String} namespace
159+
* @return {Object}
160+
*/
110161
function getModuleByNamespace (store, helper, namespace) {
111162
const module = store._modulesNamespaceMap[namespace]
112163
if (process.env.NODE_ENV !== 'production' && !module) {

0 commit comments

Comments
 (0)