2.0.0 • Published 4 years ago
lplogic v2.0.0
LpLogic
LpLogic
Setup
npm install --save git+ssh://git@git.legalplace.eu:legalplace/lplogic.git
Libraries
LpLogic
An extension of JsonLogic supporting LegalPlace's conditions operators:
- selected : Returns true only if the tested input is
true
or an array containing onlytrue
value - not-selected : Returns true only if the tested input is
false
or an array containing onlyfalse
value - has-one : Returns true only if the tested input is an array with a length === 1
- at-least-one : Returns true only if the tested input is an array containing at least one value as
true
- has-many : Returns true only if the tested input is an array with a length > 1
- contains : Returns true only if the input is equal to the conditon's value input or the input is an array with all its values equal to the conditon's value
- does-not-contain : Returns true only if the input is not equal to the conditon's value or the input is an array with all its values not equal to the conditon's value
Usage
import LpLogic from "lplogic"
// Returns true
LpLogic({"selected":[{"var":"o.1"}]}, {"o":{"1":true}})
LpLogic({"selected":[{"var":"o.1"}]}, {"o":{"1":[true, true]}})
// Returns false
LpLogic({"selected":[{"var":"o.1"}]}, {"o":{"1":false}})
LpLogic({"selected":[{"var":"o.1"}]}, {"o":{"1":[true, false]}})
// Returns true
LpLogic({"not-selected":[{"var":"o.1"}]}, {"o":{"1":false}})
LpLogic({"not-selected":[{"var":"o.1"}]}, {"o":{"1":[false, false]}})
// Returns false
LpLogic({"not-selected":[{"var":"o.1"}]}, {"o":{"1":true}})
LpLogic({"not-selected":[{"var":"o.1"}]}, {"o":{"1":[true, false]}})
// Returns true
LpLogic({"has-one":[{"var":"o.1"}]}, {"o":{"1":[true]}})
// Returns false
LpLogic({"has-one":[{"var":"o.1"}]}, {"o":{"1":[true, true]}})
// Returns true
LpLogic({"has-many":[{"var":"o.1"}]}, {"o":{"1":[true, true]}})
// Returns false
LpLogic({"has-many":[{"var":"o.1"}]}, {"o":{"1":[true]}})
// Returns true
LpLogic({"at-least-one":[{"var":"o.1"}]}, {"o":{"1":[false, false, true]}})
// Returns false
LpLogic({"at-least-one":[{"var":"o.1"}]}, {"o":{"1":[false, false, false]}})
// Returns true
LpLogic({"contains":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Hello"}})
LpLogic({"contains":[{"var":"v.1"}, "Hello"]}, {"v":{"1":["Hello","Hello"]}})
// Returns false
LpLogic({"contains":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}})
LpLogic({"contains":[{"var":"v.1"}, "Hello"]}, {"v":{"1":["Hello","Bye"]}})
// Returns true
LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}})
LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":["Bye","Bye"]}})
// Returns false
LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Hello"}})
LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":["Hello","Bye"]}})
Parser
Parsing ConditionV1 objects to LpLogic object
Usage
import { Parser, References } from "lplogic"
let model = {
documents: {
"main": {
sections: [{
"id": 1,
"label": "It's 3 am",
"options": [1,2]
}]
}
},
options: {
"1": {
meta: {
id: 1,
type: "checkbox",
label: "Check me to see Option 2",
conditional: false
},
children:[],
variables: []
},
"2": {
meta: {
id: 2,
type: "checkbox",
label: "Hey ! it's option 2",
conditional: true,
compiledConditions: [{
c: "selected",
o: 1
}]
},
children:[],
variables: []
}
},
variables: {}
}
// Setting References before starting to parse
let sections = model.documents.main.sections
let options = model.options
let variables = model.variables
References.setRefs(sections, options, variables);
// Parsing conditions on Option 2
let conditions = new Parser( options["2"].meta.compiledConditions ).getLogic()
In this example, the variable conditions
should be equal to
{"selected":[{"var":"o.1"}]}
MISC
Running tests
npm test
Building
npm run build
2.0.0
4 years ago