0.0.5 • Published 4 years ago

@fendy3002/logical-compare v0.0.5

Weekly downloads
8
License
MIT
Repository
-
Last release
4 years ago

Logical Compare is a library that can use comparison logic in either JSON or YAML format, and use it to evaluate whether given data matching the comparison logic.

Installation & Usage

npm:

$ npm install --save @fendy3002/logical-compare

yarn:

$ yarn add @fendy3002/logical-compare

There are 2 ways to use logical compare, either using JSON format with eval, or YAML format with yamlEval. Example for JSON:

import {eval} from '@fendy3002/logical-compare';

let data = { total_price: 10000 };
let logic: {
    $compare: [
        {$prop: "total_price"},
        "gt",
        5000
    ]
};
let evaluation = await eval()(data, logic);

For YAML:

import {yamlEval} from '@fendy3002/logical-compare';

let data = { total_price: 10000 };
let logic: `
  $compare:
    - $prop: "total_price"
    - "gt"
    - 5000
`;
let evaluation = await yamlEval()(data, logic);

Blocks

The logic pieces are consists of blocks. Each block can be a logical operator (or, and), comparison (compare, between), or data (date, prop).

$prop block

JSON:

{
    "$prop": "total_price"
}

YAML:

  $prop: "total_price"

Represent / placeholder for a property. It will be replaced with property value on comparison phase. For nested value, a dot can be used. Ex:

JSON:

{
    "$prop": "item.total_price"
}

$boolean block

JSON:

{
    "$boolean": "true"
}

YAML:

  $boolean: "true"

Will be replaced with boolean value. True if value is boolean true, or a string equals "true". Otherwise false.

$date block

JSON:

{
    "$date": "2000-01-01 00:00:00"
}

YAML:

  $date: "2000-01-01 00:00:00"

Will be replaced with date object. Use today or now as value to get today or now date. Can also use $prop block.

JSON:

{
    "$date": {
        "$prop": "birth"
    }
}

YAML:

  $date: 
    $prop: "birth"

$date block can also have 2 additional formatFrom and formatTo property.

The available values for both formatFrom and formatTo options are timestamp, unix or moment format.

For formatFrom, this block will read timestamp value if it is on timestamp or unix, or try to use it as moment format.

For formatTo, this block will return millisecond timestamp if it is timestamp. Second timestamp if it is unix, or string using moment format. Example:

{
    "$date": {
        "$prop": "birth"
    },
    "formatFrom": "YYYY-MM-DD",
    "formatTo": "timestamp"
}

$and block

JSON:

{
    "$and": [
        { /* first condition */ },
        { /* second condition */ }
    ]
}

YAML:

  $and:
    - /*first_condition*/
    - /*second_condition*/

If any of condition is false, return false. Otherwise true.

$or block

JSON:

{
    "$or": [
        { /* first condition */ },
        { /* second condition */ }
    ]
}

YAML:

  $or:
    - /*first_condition*/
    - /*second_condition*/

If any of condition is true, return true. Otherwise false.

$compare block

JSON:

{
    "$compare": [
        {$prop: "check_in"}, 
        "gt", 
        {$prop: "check_out"}
    ]
}

YAML:

  $compare:
    - $prop: "check_in" 
    - "gt"
    - $prop: "check_out"

Array of 3 items. The first and third parameter are the values that will be compared. The 2nd parameter is the comparison operator. Available comparison operator:

  • eq : equals
  • ne : not equals
  • gt : greater than
  • gte : greater than or equals
  • lt : less than
  • lte : less than or equals
  • starts_with : first props starts with value
  • ends_with : first props ends with value
  • contains : first props contains specific text
  • regex : first props match regex expression
  • in : see in operation

in operation

JSON:

{
    "$compare": [
        {$prop: "customer.type"}, 
        "in", 
        ["PREMIUM", "VIP", "VVIP"]
    ]
}

YAML:

  $compare:
    - $prop: "customer.type" 
    - "in"
    - ["PREMIUM", "VIP", "VVIP"]
    

Same with $compare block, except the third element must be an array. It match (exact match) the first property with third props.

$between and $betweenEx block

JSON:

{
    "$between": [
        {$date: "2000-01-01"},
        {$date: {$prop: "promo_time"} },
        {$date: "2000-03-31"}
    ]
}

YAML:

  $between:
    - $date: "2000-01-01"
    - $date:
        $prop: "promo_time"
    - $date: "2000-03-31"

Array of 3 items. The first and third parameter are the min and max value, while the 2nd parameter is the property to compare. If the property to compare is equal with min or max, it resulted in true.

On the contrary, betweenEx (between exclude), will return false when property is equal to either min or max.

V2 (future plan)

$datepart block

{
    "$datepart": {
        "of": {$prop: "promo_time"},
        "as": "day"
    }
}

From props given in of property, return the date part value defined in as. Supported as property: year, month, day, hour, minute.

$arrpart

{
    "$arrpart": {
        "of": {$prop: "items"},
        "get": "length"
    }
}

Get operation:

  • length
  • {$sum: "prop"}
  • {$max: "prop"}
  • {$min: "prop"}
  • {$avg: "prop"}
0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

5 years ago

0.0.1

5 years ago