cerebral-addons
Additional utilities for use with cerebral/operators
Usage
import and from 'cerebral-addons/and';
import merge from 'cerebral-addons/merge';
cerebral-operators supports custom getter and setter functions in place of strings. If either of these
functions is detected to by async (indicated by the returning of a promise) then the addon must be marked
as async in the chain and subsequently define success and error paths.
getters
A getter is a function that accepts the args passed to an action method and returns some value.
[promise] getter(args)
Example
an example getter might get some data from the server:
function httpGet(url) {
return function (args) {
return new Promise(resolve => {
getDataFromServer(url, function (err, data) {
resolve(data)
})
})
}
}
[
copy(httpGet('/api/date.json'), 'state:date'), {
success: [],
error: []
}
]
setters
A setter is a function that accepts the args passed to an action method and the value to set.
[promise] setter(args, value)
if the setter is async then the addon will also pass on the resolve value to the success chain
Example
an example setter might post some data to the server:
function httpPost(url) {
return function (args, value) {
return new Promise(resolve => {
postDataToServer(url, value, function (err, data) {
resolve(data)
})
})
}
}
[
copy('state:date', httpPost('/api/date.json')), {
success: [],
error: []
}
]
included getters
cerebral-addons includes the following getters
and
export default [
when(and('state:firstCondition', 'input:otherCondition')), {
true: [],
false: []
}
]
compose
Compose replaces all getters found in object given to the compose factory with the runtime values
Compose doesn't currently support async getters
export default [
copy(compose({
fromInput: get('input:value'),
fromState: get('state:value')
}), 'output:composed')
]
get
see compose
isEqual
export default [
when(isEqual('state:firstValue', 'input:otherValue')), {
true: [],
false: []
}
]
isDeepEqual
export default [
when(isDeepEqual('state:firstValue', 'input:otherValue')), {
true: [],
false: []
}
]
literal
export default [
copy(literal('literal'), 'output:value')
]
not
export default [
when(and('state:firstCondition', not('input:otherCondition'))), {
true: [],
false: []
}
]
or
export default [
when(or('state:firstCondition', 'input:otherCondition')), {
true: [],
false: []
}
]
findWhere
copy(findWhere('state:users', { name: 'John' }), 'output:john')
pop
pop also modifies the array in the state
copy(pop('state:users'), 'output:lastUser')
shift
shift also modifies the array in the state
copy(shift('state:users'), 'output:firstUser')
Included setters
cerebral-addons includes the following setters
merge
copy('input:newData', merge('state:allData'))
push
copy('input:newUser', push('state:users'))
unshift
copy('input:newUser', unshift('state:users'))
Note on Adblockers
Some adblockers such as uBlock Origin may block access to pop.js
during development. This can
be resolved by turning off ad blocking for localhost or using webpack (or similar) for
development. This shouldn't be an issue for production deployments if you are packaging your
production dependencies in a combined .min.js
.
Contribute
Fork repo
npm install
npm start
runs dev mode which watches for changes and auto lints, tests and buildsnpm test
runs the testsnpm run lint
lints the codenpm run build
compiles es2015 to es5