@tadashi/condition

Create conditional to validate data.

Usage no npm install needed!

<script type="module">
  import tadashiCondition from 'https://cdn.skypack.dev/@tadashi/condition';
</script>

README

Condition

NPM version Node.js CI Coverage Status

XO code style

Create conditional to validate data.

Install

$ npm i -S @tadashi/condition

Usage

See example below.

import 'condition' from '@tadashi/condition'

const data = {
  age: 65,
  gender: 'F',
  city: 'São Paulo',
  hasCar: true,
  colors: ['red', 'blue']
}

const conditions = [
  {
    join_operator: 'and',
    args: [
      {
        field: 'gender',
        operator: 'eq',
        value: 'F'
      }, {
        field: 'age',
        operator: 'gt',
        value: 21
      }, {
        join_operator: 'or',
        args: [
          {
            field: 'city',
            operator: 'assigned',
            value: false
          }, {
            field: 'colors',
            operator: 'intersection',
            value: ['blue', 'green']
          }
        ]
      }, {
        field: 'hasCar',
        operator: 'eq',
        value: true
      }
    ]
  }
]

const isValid = condition(conditions)
isValid(data) // => true

License

MIT © Thiago Lagden