Skip to content

Instantly share code, notes, and snippets.

@jdltechworks
Last active May 31, 2018 09:47
Show Gist options
  • Save jdltechworks/c5d875c739dd33425a051dbb4c4f3856 to your computer and use it in GitHub Desktop.
Save jdltechworks/c5d875c739dd33425a051dbb4c4f3856 to your computer and use it in GitHub Desktop.
mutation types with prefix
const createMutationTypes = (opts) => {
opts = opts || {};
let separator = opts.separator || '_'
if (typeof opts === 'string') {
opts = {prefix: opts}
}
const defineType = (obj, prefix, n, v) => {
v = v || [].concat.apply([], [prefix, n])
.join(separator)
.toUpperCase()
Object.defineProperty(obj, n, {
value: v,
enumerable: true,
writable: false,
configurable: false
})
return obj
}
const definer = (obj, prefix) => {
return (...definitions) => {
definitions.forEach((def) => {
defineType(obj, prefix, def);
})
return obj
}
}
let prefix = opts.prefix || '';
return definer({}, prefix);
}
export default createMutationTypes
import createMutationTypes from './mutation-types-generator'
export default createMutationTypes('auth')(
'LOGGED_IN',
'ERRORS',
'PENDING'
)
/*
* expected output
* { LOGGED_IN: 'AUTH_LOGGED_IN', ERRORS: 'AUTH_ERRORS', PENDING: 'AUTH_PENDING' }
*
*/
import types from './mutation-types.js'
const mutation = {
[types.LOGGED_IN](state, { credentials }) {
state.credentials = credentials
},
[types.ERRORS](state, { errors}) {
state.errors = errors
},
[types.PENGING](state, { message }) {
state.message = message
}
}
export default mutation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment