Minimal tools for creating rules with conditions using simple reducers. Conditions are pure functions, so there's no side-effects. Facts must have everything needed by the conditions.
They return a single condition function
which expects the facts
as argument in order to return a boolean.
Every condition (all conditions) must be true.
Every condition can't be true.
Some condition must be true.
Some condition can't be true.
Simple:
import { every, some } from 'conducer'
export const hasRole = role => ({ user }) => user.roles.some(role)
export const isActiveUser = ({ user: { status } }) => status
const canEditComments = every([
isActiveUser,
some([
hasRole('admin'),
hasRole('moderator'),
])
])
const facts = { user: { status: true, roles: ['moderator', 'admin'] } }
if (canEditComments(facts)) {
// Give access to edit comments.
}
else {
// Denied access to editing comments.
}
Using the when()
chaining function to avoid if
conditions:
import { every, some, when } from 'conducer'
// ... same as above.
when(canEditComments(facts))
.do(() => {
// Give access to edit comments.
})
.else(() => {
// Denied access to editing comments.
})