2.3.1 • Published 2 years ago

@mangar2/decision v2.3.1

Weekly downloads
10
License
LGPL-3.0-or-later
Repository
github
Last release
2 years ago

Abstract

Provides a JSON based term evaluator to calculate decisions based on configuration files The syntax of the JSON-terms is derived from Lisp .

Contents

Meta

Filedecision.js
AbstractProvides a JSON based term evaluator to calculate decisions based on configuration files The syntax of the JSON-terms is derived from Lisp .
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .

Class Decision

new Decision(variables)

Creates a value class providing the ability to evalutate a json based value . Additionally it explains the value in a juman readable string . Supported operations are

Example

const variables = {
   'a/b': 1,
   'b/c': 2,
   'system/presence': 'awake'
}
const value = new Decision(variables)
const result = value.decide(
     [
          'and',
          ['or', ['<', 'a/b', 10], ['<', 'b/c', 2]],
          ['or', ['>=', 'a/b', 10], ['>=', 'b/c', 2]]
      ]
)
// returns result.value = true and result.reason = 'a/b (1) is < 10 and b/c (2) is >= 2'

Decision Parameters

NameTypeAttributeDefaultDescription
variablesObjectoptional{ }list of known variables and values

Decision Methods

_ISOStringToDate

_ISOStringToDate (value) => {Date, any}

Converts any value from an ISO formatted date string to a date object If it is not an ISO formatted date string , the original value will be returned

_ISOStringToDate Parameters
NameTypeDescription
valueanyany value
_ISOStringToDate returns
TypeDescription
Date, anya Date object or the original value

eval

eval (jsonTree) => {EvalResult}

Calculates a value based on a json based json tree

eval Parameters
NameTypeDescription
jsonTreeObjectjson structured boolean logic
eval throws
TypeDescription
Erroron any error in the jsonTree
eval returns
TypeDescription
EvalResultresult of the evaluation

on

on (event, callback)

Sets a callback .

on Parameters
NameTypeDescription
eventstringevent name ( not case sensitive ) for the callback
callbackfunction( parameter )
on throws
TypeDescription
Errorif the callback is not 'function'

Class EvalResult

new EvalResult(value, reason)

Creates a result object with a "value" and a "reason" and a list of used variable : value pairs

EvalResult Parameters

NameTypeDescription
valueDate, string, number, booleanevaluation value
reasonstringvalue reason in human readable form

EvalResult Members

NameTypedescription
reasonstringReason explaining the result
valueDate, string, number, booleanValue of the result
variablesArray.<string>List of missing variables found
2.3.1

2 years ago

2.3.0

2 years ago

2.2.5

3 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago