Create an object where values are identical to its key names. Inspired by keymirror and Facebook’s react/lib/keyMirror, keyconst reduces the amount of code to create new key constants in objects.
Instead of
const Actions = keymirror({ BUY_ICECREAM: null })
you can now simplify this to
const Actions = keyconst([ 'BUY_ICECREAM' ])
$ yarn add @bitchcraft/keyconst
$ npm install -P @bitchcraft/keyconst
import keyconst from '@bitchcraft/keyconst';
const Events = keyconst([
'AUTH_SUCCESS',
'AUTH_FAILURE',
'AUTH_LOADING',
]);
function reducer(state = Map(), event) {
const { payload, type } = event;
switch (type){
case Events.AUTH_LOADING:
return state.set('loading', true);
case Events.AUTH_SUCCESS:
return state.set('loading', false)
.set('user', payload.user);
case Events.AUTH_FAILURE:
return state.set('loading', false)
.set('error', payload.error)
.delete('user');
}
return state;
}
var keyconst = require('@bitchcraft/keyconst');
var Events = keyconst([
'AUTH_SUCCESS',
'AUTH_FAILURE',
'AUTH_LOADING',
]);
function reducer(state, event) {
if (typeof state !== 'object') state = {};
var payload = event.payload;
var type = event.type;
switch (type){
case Events.AUTH_LOADING:
state.loading = true;
break;
case Events.AUTH_SUCCESS:
state.loading = false;
state.user = payload.user;
break;
case Events.AUTH_FAILURE:
state.loading = false;
state.error = payload.error;
delete state.loading;
break;
}
return state;
}
// babel: env, stage-0, flow
import keyconst from '@bitchcraft/keyconst/src/keyconst';
Gzipped size is around 1KB. You can check out the bundle analytics for the non-minified bundle.
Please file issues in Github
We are open for PRs. Please respect to the linting rules.
Keyconst is free software und the BSD-3-Clause (see LICENSE.md).
- Josh Li (Maintainer)