Coder Social home page Coder Social logo

json-conditions's Introduction

Simple conditional logic testing of JSON objects

Simple json takes an array of conditions that can be compared against a JSON object to test if that object passes or fails the conditions.

Quickstart

npm install json-conditions
const checkConditions = require('json-conditions');

const reference = {
	user: {
		preferredName: 'Alex',
		age: 4,
	},
	toy: {
		name: 'Model Train',
		tracks: 18,
		engines: 1
		battery: true,
	}
};

const simpleRules = [
	{ property: 'toy.engines', op: 'gt', value: 2 },
	{ property: 'batteries', op: 'eq', value: true },
];

// Returns true
checkConditions({
	rules: simpleRules,
	satisfy: 'ANY',
	log: console.log,
}, reference);

// Returns false
checkConditions({
	rules: simpleRules,
	satisfy: 'ALL',
	log: console.log,
}, reference);

// Returns true
checkConditions({
	rules: [
		// A required condition must always be satisfied regardless of the value
		{ property: 'toy.tracks', op: 'gt', value: 2, required: true },
		{ property: 'batteries', op: 'eq', value: true },
		{ property: 'solarPanels', op: 'gte', value: 0 },
	],
	satisfy: 'ANY',
	log: console.log,
}, reference);

Parameters

Param Type Default Description
settings.log function Optional function to log debug output from the evaluation
settings.rules object[] Rules, see below
settings.satisfy string ANY How many rules must be satisfied to pass, 'ALL' or 'ANY'
reference object The javascript object to evaluate the rules against

Rules

Each rule is described by an object with the following properties

property Type Default Description
op string The logical operator to use for comparison (see below)
property string The property in the reference object to check, evaluated by
required boolean false If true, this rule must always evaluate to true for the object to pass the conditions
value * Value to compare the property to

Property is passed to lodash.get to lookup the value in the object. So effectively the rules are evaluated to get(reference, rule.property) ${rule.op} ${rule.value}

Operators

The following operators can be used in rules. Operators use javascript coersion (ie == not ===) Additionally, we assume that rule values may have come from a form, and so try to be forgiving when dealing with booleans. If the value of the property is a boolean, then the strings 'true' and 'false' (case insensitive) will be converted to booleans.

Operator Javascript operation Notes
eq ==
neq !=
ne != (Alias for neq)
gt >
gte >=
lt <
lte <=
absent !
empty ! (Alias for absent)
present !!
startsWith _.toString(x).startsWith()
endsWith _.toString(x).endsWith()
contains _.toString(x).includes()

License

Licensed under the NoHarm license

json-conditions's People

Contributors

chrisjensen 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.