THIS PACKAGE IS LEGACY, PLEASE UPDATE TO @duckness/duck
duckness
Duckness helps you to build ducks - redux modules.
Example
// counterDuck.js // Create duck with the name 'counter' for 'counter-app'const counterDuck = // Export action creatorscounterDuckaction'incrementCounter' 'INCREMENT'// counterDuck.act.incrementCounter will build actions with type 'counter-app/counter/INCREMENT'counterDuckaction'decrementCounter' 'DECREMENT'// counterDuck.act.decrementCounter will build actions with type 'counter-app/counter/DECREMENT' // Add selectorcounterDuck // Add reducerscounterDuckcounterDuck // Duck itself is a root reducer
Table of Contents
- duckness
- Example
- Table of Contents
- API
- Additional resources
API
Create Duck
Create a new duck with duckName and poolName (poolName is a namespace for ducks)
const myDuck =
.duckName
const myDuck = myDuckduckName// => 'duck-name'
.poolName
const myDuck = myDuckpoolName// => 'pool-name'
Actions
.mapActionType(actionType)
Maps short action type to long action type
myDuck// => 'pool-name/duck-name/ACTION_TYPE'
.action(actionName, actionType, ?payloadBuilder, ?actionTransformer)
Build action creator and register it under actionName (if actionName present)
const eatFish = myDuckaction'eatAllTheFish' 'EAT_FISH'// => { type: 'pool-name/duck-name/EAT_FISH', payload: { amount: 10 } }myDuckact// => { type: 'pool-name/duck-name/EAT_FISH', payload: { amount: 9000 } }
Optional payloadBuilder
could be specified to customize payloads
const eatFish = myDuckactionnull 'EAT_FISH' { return amount: payload }// => { type: 'pool-name/duck-name/EAT_FISH', payload: { amount: 10 } }
Optional actionTransformer
could be specified to customize action
const eatFish = myDuckactionnull 'EAT_FISH' null { return ...action wellFed: true }// => { type: 'pool-name/duck-name/EAT_FISH', payload: { amount: 10 }, wellFed: true }
If Error
object is passed to action creator payloadBuilder
will be skipped and action.error
will be true
const eatFish = myDuckactionnull 'EAT_FISH'// => { type: 'pool-name/duck-name/EAT_FISH', payload: Error('no more fish'), error: true }
.act[]
Calls registered action creator by its name
const eatFish = myDuckaction'eatAllTheFish' 'EAT_FISH'myDuckact// => { type: 'pool-name/duck-name/EAT_FISH', payload: { amount: 9000 } }
.listActionTypes()
Returns all known short action types. Type is known if .mapActionType
or .action
was called with it.
myDuck// => ['EAT_FISH', 'QUACK']
.types[]
Is an object that maps known short action types to long action types
myDucktypesEAT_FISH// => 'pool-name/duck-name/EAT_FISH'
Selectors
.selector(selectorName, selector)
Registers selector under selectorName
myDuck
.select[]
Calls registered selector
const state = counter: 10 myDuckselect// => 10
Reducers
.reducer(actionType, reducer)
Registers reducer for specific action type
myDuck
.reducer(null, reducer)
Registers wildcard reducer for all duck action types
myDuck
Root reducer
Duck itself is a root reducer for all registered reducers
// => reduced state
duckFace
duckFace is an interface to duck that is added as a last argument to each registered selectors and reducers
myDuck
duckFace.types[]
Is an object that maps known short action types to long action types
duckFacetypesEAT_FISH// => 'pool-name/duck-name/EAT_FISH'
duckFace.act[]
Calls registered action creator by its name
duckFace.select[]
Calls registered selector
const state = counter: 10 duckFaceselect// => 10
duckFace.reduce(state, action)
Calls duck root reducer
const prepareFish = myDuckactionnull 'PREPARE_FISH'myDuck
duck.face
duckFace can also be accessed from the duck itself by duck.face
name.
Clone duck
Duck can be cloned by calling .clone(duckName, moduleName)
.
Cloned duck will contain selectors, reducers and known action types copied from original duck with
all action types adjusted to duckName
and moduleName
. Cloned duck can be expanded further.
const baseDuck = baseDuck const extendedDuck = baseDuckextendedDuck
Hatch
Calling .hatch()
on duck will freeze it preventing duck from modifications like adding reducers or selectors.
Hatched duck can be cloned and clone is unhatched.
Additional resources
- duckness-saga - Saga extension for Duckness
- duckness-pool - boilerplate for React-Redux apps based on Duckness and Duckness-Saga