dacho
v2.0.1
Published
Utility of generating key/value object with a prefixed value
Downloads
4
Maintainers
Readme
dacho
Utility of generating key/value object with a prefixed value.
For example, it is convenient to define ActionTypes for Flux, Redux and the like. Besides, it prevent from defining same value. It throws Error if you registered already defined value.
dacho is named after Dacho Club that is Japanese comedy group. They perform patterned reaction by the simple rules every time, but it is unique...
Installation
npm install --save dacho
Usage
Basic
It generate key/value object with a prefixed value.
// constants/nettoActionTypes.js
import {reaction} from 'dacho';
export default reaction([
'IN',
'OUT',
'PUSH'
], 'NETTO/')
// OR using Object.
//
// export default reaction({
// IN: null,
// OUT: null,
// PUSH: null
// }, 'NETTO/');
// constants/odenActionTypes.js
import {reaction} from 'dacho';
export default reaction([
'IN',
'OUT',
'PUSH',
'SHOWER',
], 'ODEN/')
// OR using Object.
//
// export default reaction({
// IN: null,
// OUT: null,
// PUSH: null
// SHOWER: null
// }, 'ODEN/');
// actionCreator.js
import nettoActionTypes from './constants/nettoActionTypes';
import odenActionTypes from './constants/odenActionTypes';
function rejectBathing() {
return {
type: nettoActionTypes.PUSH, // 'NETTO/PUSH'
payload: {member: 'UESHIMA'}
};
}
function rejectEating() {
return {
type: odenActionTypes.PUSH, // 'ODEN/PUSH'
payload: {member: 'UESHIMA'}
};
}
// assert.deepEqual(nettoActionTypes, {
// IN: 'NETTO/IN',
// OUT: 'NETTO/OUT',
// PUSH: 'NETTO/PUSH'
// });
// assert.deepEqual(odenActionTypes, {
// IN: 'ODEN/IN',
// OUT: 'ODEN/OUT',
// PUSH: 'ODEN/PUSH'
// SHOWER: 'ODEN/SHOWER'
// });
It throws Error when you create same value object.
// ./constants/nettoActionTypes2.js
import {reaction} from 'dacho';
export default reaction([
'IN'
], 'NETTO/');
// -> throw Error because 'NETTO/IN' is already defined.
With Global Prefix
It generate object with global prefix.
// ./singletons/reaction.js
import {createReaction} from 'dacho';
export default createReaction('DEGAWA/')
// ./constants/degawaNettoActionTypes.js
import reaction from './singletons/reaction';
export default reaction([
'IN',
'OUT',
'PUSH'
], 'NETTO/');
// import degawaNettoActionTypes from './constants/degawaNettoActionTypes';
//
// assert.deepEqual(degawaNettoActionTypes, {
// IN: 'DEGAWA/NETTO/IN',
// OUT: 'DEGAWA/NETTO/OUT',
// PUSH: 'DEGAWA/NETTO/PUSH'
// });
Tips
Testing
Despite collect values, It may raise Errors (key is already registered
) when executing unit tests.
One of solution of this problem is to use clear-require.
Example is here.
Flow
dacho
can use together with Flow annotation.
But, Flow version is 0.19 or higher.