🚨 Latest Research:Tanstack npm Packages Compromised in Ongoing Mini Shai-Hulud Supply-Chain Attack.Learn More →
Socket
Book a DemoSign in
Socket

marina-tree

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

marina-tree

An extensible logical tree evaluator, written on node.js.

latest
Source
npmnpm
Version
4.0.1
Version published
Maintainers
1
Created
Source

marina-tree

Build Status Coverage Status

An extensible logical tree evaluator.

npm i marina-tree

Usage

Single Fact

The same fact will be evaluated in all functions in the tree.

const Marina = require('marina-tree');

// Functions definition
const myFuncs = {
  isAnArray: (param) => {
    return param instanceof Array;
  },
  isANumber: (param) => {
    return typeof param === 'number';
  },
  isOdd: (param) => {
    return param % 2 === 1;
  },
};

// Initialize with my functions and a string (wildcard) that will indicate nesting.
const marina = new Marina(myFuncs, '*');

// Build my conditions tree
const myTree = {
  funcs: ['isAnArray', '*'],
  facts: [
    null, // Padding
    {
      funcs: ['isANumber', 'isOdd'],
      operator: 'and',
    }
  ],
  operator: 'or',
};

console.log(marina.eval(myTree, 2));  // false
console.log(marina.eval(myTree, 3));  // true
console.log(marina.eval(myTree, {})); // false
console.log(marina.eval(myTree, [])); // true

const Marina = require('marina-tree');

// Functions definition
const myFuncs = {
  isAnArray: (param) => {
    return param instanceof Array;
  },
  isANumber: (param) => {
    return typeof param === 'number';
  },
  isGte10: (param) => {
    return param >= 10;
  },
  isLte100: (param) => {
    return param <= 100;
  },
  isAFunction: (param) => {
    return typeof param === 'function';
  },
};

// Initialize with my functions and a string (wildcard) that will indicate nesting.
const marina = new Marina(myFuncs, '*');

// Build my conditions tree
const myTree = {
  funcs: ['*', '*'],
  facts: [
    {
      funcs: ['isANumber', 'isGte10', 'isLte100'],
      operator: 'and',
    },
    {
      funcs: ['isANumber', '*'],
      facts: [
        null, // Padding
        {
          funcs: ['isANumber'],
          operator: 'not',
        }
      ],
      operator: 'or',
    },
  ],
  operator: 'or',
};

console.log(marina.eval(myTree, 2));    // false
console.log(marina.eval(myTree, 3));    // false
console.log(marina.eval(myTree, {}));   // true
console.log(marina.eval(myTree, []));   // true
console.log(marina.eval(myTree, 9));    // false
console.log(marina.eval(myTree, 10));   // true
console.log(marina.eval(myTree, 100));  // true
console.log(marina.eval(myTree, 101));  // false
console.log(marina.eval(myTree, (() => console.log('is a function'))));  // true

Single Fact + Nested Args

Each function will receive the same fact + a specific argument.

const Marina = require('marina-tree');

// Definition of more generic and reusable functions
const myFuncs = {
  gte: (singleFact, specificArg) => {
    return singleFact[specificArg.field] >= specificArg.value;
  },
  lte: (singleFact, specificArg) => {
    return singleFact[specificArg.field] <= specificArg.value;
  },
  in: (singleFact, specificArg) => {
    return specificArg.value.indexOf(singleFact[specificArg.field]) > -1;
  },
};

// Initialize with my functions and a string (wildcard) that will indicate nesting.
const marina = new Marina(myFuncs, '*');

// Build my conditions tree
const myTree = {
  funcs: ['*', '*', '*'],
  facts: [
    {
      funcs: ['gte', 'lte'],
      operator: 'and',
    },
    {
      funcs: ['*', 'in'],
      facts: [
        {
          funcs: ['in', 'gte'],
          operator: 'and',
        },
        null, // Padding
      ],
      operator: 'or',
    },
    {
      funcs: ['in'],
      operator: 'not',
    },
  ],
  operator: 'and',
};

const nestedArgs = [
  [
    {
      field: 'age',
      value: 20,
    },
    {
      field: 'age',
      value: 80,
    },
  ],
  [
    [
      {
        field: 'lang',
        value: ['java', 'c++'],
      },
      {
        field: 'exp',
        value: 5,
      },
    ],
    {
      field: 'lang',
      value: ['cobol'],
    },
  ],
  [
    {
      field: 'lang',
      value: ['php']
    },
  ],
];

const persons = [
  {
    age: 20,
    lang: 'c++',
    exp: 2,
  },
  {
    age: 25,
    lang: 'java',
    exp: 5,
  },
  {
    age: 60,
    lang: 'cobol',
    exp: 30,
  },
  {
    age: 30,
    lang: 'php',
    exp: 10,
  },
];

console.log(marina.eval(myTree, persons[0], nestedArgs)); // false
console.log(marina.eval(myTree, persons[1], nestedArgs)); // true
console.log(marina.eval(myTree, persons[2], nestedArgs)); // true
console.log(marina.eval(myTree, persons[3], nestedArgs)); // false

Operators

  • and
  • nand
  • or
  • xor
  • not (unary)
  • none (unary)
  • ... (alias for none) (unary)

Marina

Marina

License

MIT

Keywords

logical-tree

FAQs

Package last updated on 03 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts