GithubHelp home page GithubHelp logo

jkso / conducer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from choko-org/conducer

0.0 2.0 0.0 8 KB

Minimal tools for creating rules with conditions using simple reducers.

License: MIT License

JavaScript 100.00%

conducer's Introduction

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

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

conducer's People

Contributors

sebas5384 avatar

Watchers

James Cloos avatar pylonkin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.