Skip to content

Minimal tools for creating rules with conditions using simple reducers.

License

Notifications You must be signed in to change notification settings

choko-org/conducer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Conducer

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.

Build Status sponsored by Taller

Tools:

They return a single condition function which expects the facts as argument in order to return a boolean.

every([ condition, conditionOne, conditionTwo ])

Every condition (all conditions) must be true.

notEvery([ condition, conditionOne, conditionTwo ])

Every condition can't be true.

some([ condition, conditionOne, conditionTwo ])

Some condition must be true.

notSome([ condition, conditionOne, conditionTwo ])

Some condition can't be true.

Examples:

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.
  })

Usage:

LICENSE

MIT

About

Minimal tools for creating rules with conditions using simple reducers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published