@legalplace/lplogic 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/LpLogic.js CHANGED
@@ -98,18 +98,28 @@ json_logic_js_1.default.add_operation("has-many", function (value) {
98
98
  return false;
99
99
  return value.length > 1;
100
100
  });
101
- json_logic_js_1.default.add_operation("contains", function (value, comparator) {
102
- if (!Array.isArray(value))
103
- return value === comparator;
104
- var filter = value.filter(function (_value) { return _value === comparator; });
101
+ var is = function (value, comparator) {
102
+ if (!Array.isArray(value)) {
103
+ var cleanValue = (typeof value === "number" ? value.toString() : value).toLowerCase();
104
+ var cleanComparator = (typeof comparator === "number" ? comparator.toString() : comparator).toLowerCase();
105
+ return cleanValue === cleanComparator;
106
+ }
107
+ var filter = value.filter(function (_value) { return is(_value, comparator); });
105
108
  return value.length === filter.length;
106
- });
107
- json_logic_js_1.default.add_operation("does-not-contain", function (value, comparator) {
108
- if (!Array.isArray(value))
109
- return value !== comparator;
110
- var filter = value.filter(function (_value) { return _value !== comparator; });
109
+ };
110
+ json_logic_js_1.default.add_operation("is", is);
111
+ json_logic_js_1.default.add_operation("contains", is);
112
+ var not = function (value, comparator) {
113
+ if (!Array.isArray(value)) {
114
+ var cleanValue = (typeof value === "number" ? value.toString() : value).toLowerCase();
115
+ var cleanComparator = (typeof comparator === "number" ? comparator.toString() : comparator).toLowerCase();
116
+ return cleanValue !== cleanComparator;
117
+ }
118
+ var filter = value.filter(function (_value) { return not(_value, comparator); });
111
119
  return value.length === filter.length;
112
- });
120
+ };
121
+ json_logic_js_1.default.add_operation("not", not);
122
+ json_logic_js_1.default.add_operation("does-not-contain", not);
113
123
  json_logic_js_1.default.add_operation("empty", function (value) {
114
124
  if (!Array.isArray(value)) {
115
125
  var stringValue = typeof value === "number" ? value.toString() : value;
@@ -132,5 +142,25 @@ json_logic_js_1.default.add_operation("not-empty", function (value) {
132
142
  });
133
143
  return value.length === filter.length;
134
144
  });
145
+ var like = function (value, comparator) {
146
+ if (!Array.isArray(value)) {
147
+ var cleanValue = (typeof value === "number" ? value.toString() : value).toLowerCase();
148
+ var cleanComparator = (typeof comparator === "number" ? comparator.toString() : comparator).toLowerCase();
149
+ return new RegExp("( |^)" + cleanComparator + "( |$)", 'i').test(cleanValue);
150
+ }
151
+ var filter = value.filter(function (_value) { return like(_value, comparator); });
152
+ return value.length === filter.length;
153
+ };
154
+ json_logic_js_1.default.add_operation("like", like);
155
+ var notLike = function (value, comparator) {
156
+ if (!Array.isArray(value)) {
157
+ var cleanValue = (typeof value === "number" ? value.toString() : value).toLowerCase();
158
+ var cleanComparator = (typeof comparator === "number" ? comparator.toString() : comparator).toLowerCase();
159
+ return !new RegExp("( |^)" + cleanComparator + "( |$)", 'i').test(cleanValue);
160
+ }
161
+ var filter = value.filter(function (_value) { return notLike(_value, comparator); });
162
+ return value.length === filter.length;
163
+ };
164
+ json_logic_js_1.default.add_operation("not-like", notLike);
135
165
  var LpLogic = json_logic_js_1.default.apply;
136
166
  exports.default = LpLogic;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/lplogic",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "LegalPlace LpLogic",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -79,6 +79,17 @@ test('Testing "contains" Hello on a value `Bye`', async () => {
79
79
  expect( LpLogic({"contains":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}}) ).toBe(false)
80
80
  })
81
81
 
82
+ // Returns true
83
+ test('Testing "is" Hello on a value `Hello`', async () => {
84
+ expect( LpLogic({"is":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Hello"}}) ).toBe(true)
85
+ })
86
+
87
+
88
+ // Returns false
89
+ test('Testing "is" Hello on a value `Bye`', async () => {
90
+ expect( LpLogic({"is":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}}) ).toBe(false)
91
+ })
92
+
82
93
  // Returns true
83
94
  test('Testing "does-not-contain" Hello on a value `Bye`', async () => {
84
95
  expect( LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}}) ).toBe(true)
@@ -87,4 +98,34 @@ test('Testing "does-not-contain" Hello on a value `Bye`', async () => {
87
98
  // Returns false
88
99
  test('Testing "does-not-contain" Hello on a value `Hello`', async () => {
89
100
  expect( LpLogic({"does-not-contain":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Hello"}}) ).toBe(false)
101
+ })
102
+
103
+ // Returns true
104
+ test('Testing "not" Hello on a value `Bye`', async () => {
105
+ expect( LpLogic({"not":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Bye"}}) ).toBe(true)
106
+ })
107
+
108
+ // Returns false
109
+ test('Testing "not" Hello on a value `Hello`', async () => {
110
+ expect( LpLogic({"not":[{"var":"v.1"}, "Hello"]}, {"v":{"1":"Hello"}}) ).toBe(false)
111
+ })
112
+
113
+ // Returns true
114
+ test('Testing "like" World on a value `Hello world`', async () => {
115
+ expect( LpLogic({"like":[{"var":"v.1"}, "World"]}, {"v":{"1":"Hello world"}}) ).toBe(true)
116
+ })
117
+
118
+ // Returns false
119
+ test('Testing "like" Bye on a value `Hello world`', async () => {
120
+ expect( LpLogic({"like":[{"var":"v.1"}, "Bye"]}, {"v":{"1":"Hello world"}}) ).toBe(false)
121
+ })
122
+
123
+ // Returns true
124
+ test('Testing "not-like" Bye on a value `Hello world`', async () => {
125
+ expect( LpLogic({"not-like":[{"var":"v.1"}, "Bye"]}, {"v":{"1":"Hello world"}}) ).toBe(true)
126
+ })
127
+
128
+ // Returns false
129
+ test('Testing "not-like" World on a value `Hello world`', async () => {
130
+ expect( LpLogic({"not-like":[{"var":"v.1"}, "World"]}, {"v":{"1":"Hello world"}}) ).toBe(false)
90
131
  })
@@ -1,40 +0,0 @@
1
- import { Parser, References } from "../lib/index"
2
- import fs from "fs"
3
-
4
- // Getting & Parsing models
5
- let ExpectedModelV3 = JSON.parse(fs.readFileSync("./tests/misc/expected-model-v3.json", "UTF-8"))
6
- let UnparsedModelV3 = JSON.parse(fs.readFileSync("./tests/misc/unparsed-model-v3.json", "UTF-8"))
7
-
8
- // Getting Conditions v1 & v3 by option ID
9
- let ConditionsV1:Record<string, any> = {}
10
- let ConditionsV3:Record<string, any> = {}
11
-
12
- // Unparsed Conditions
13
- Object.keys(UnparsedModelV3.options).forEach((id) => {
14
-
15
- let meta = UnparsedModelV3.options[ id ].meta
16
- if(Array.isArray(meta.compiledConditions) && meta.compiledConditions.length > 0)
17
- ConditionsV1[ id ] = meta.compiledConditions
18
- })
19
-
20
- // Expected Conditions
21
- Object.keys(ExpectedModelV3.options).forEach((id) => {
22
- let meta = ExpectedModelV3.options[ id ].meta
23
- if(typeof meta.compiledConditions === 'object' && Object.keys(meta.compiledConditions).length > 0)
24
- ConditionsV3[ id ] = meta.compiledConditions
25
- })
26
-
27
- // Setting Refs
28
- beforeAll(() => {
29
- References.setRefs(UnparsedModelV3.documents.main.sections, UnparsedModelV3.options, UnparsedModelV3.variables);
30
- })
31
-
32
- // Starting tests
33
- describe(`Parser: Testing Conditions Parsing from V1 to V3 (LpLogic)`, () => {
34
- for(let id in ConditionsV1){
35
- it(`Parsing conditions for option id ${id}`, async () => {
36
- let parsedCondition = new Parser(ConditionsV1[id]).getLogic();
37
- expect(parsedCondition).toStrictEqual( ConditionsV3[id] )
38
- })
39
- }
40
- })
@@ -1,26 +0,0 @@
1
- import { References } from "../lib/index"
2
- import fs from "fs"
3
-
4
- // Getting & Parsing model & expected refs
5
- let ExpectedModelV3 = JSON.parse(fs.readFileSync("./tests/misc/expected-model-v3.json", "UTF-8"))
6
- let RefsModelV3 = JSON.parse(fs.readFileSync("./tests/misc/refs-model-v3.json", "UTF-8"))
7
-
8
- // Initiating refs
9
- beforeAll(() => {
10
- References.setRefs(ExpectedModelV3.documents.main.sections, ExpectedModelV3.options, ExpectedModelV3.variables);
11
- })
12
-
13
- // Running tests on parentsTree
14
- test('Testing parentsTree', async () => {
15
- expect(References.getParentsTree()).toStrictEqual(RefsModelV3.parentsTree)
16
- })
17
-
18
- // Running tests on conditionnedOptions
19
- test('Testing conditionnedOptions', async () => {
20
- expect(References.getConditionnedOptions()).toStrictEqual(RefsModelV3.conditionnedOptions)
21
- })
22
-
23
- // Running tests on multiplesDependencies
24
- test('Testing multiplesDependencies', async () => {
25
- expect(References.getMultiplesDependencies()).toStrictEqual(RefsModelV3.multiplesDependencies)
26
- })