@legalplace/lplogic 2.0.1 → 2.0.3
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 +60 -8
- package/package.json +1 -1
- package/tests/LpLogic.test.ts +41 -0
- package/tests/Parser.test.ts +0 -40
- package/tests/References.test.ts +0 -26
package/dist/LpLogic.js
CHANGED
|
@@ -98,17 +98,69 @@ json_logic_js_1.default.add_operation("has-many", function (value) {
|
|
|
98
98
|
return false;
|
|
99
99
|
return value.length > 1;
|
|
100
100
|
});
|
|
101
|
-
|
|
102
|
-
if (!Array.isArray(value))
|
|
103
|
-
|
|
104
|
-
|
|
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); });
|
|
108
|
+
return value.length === filter.length;
|
|
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); });
|
|
119
|
+
return value.length === filter.length;
|
|
120
|
+
};
|
|
121
|
+
json_logic_js_1.default.add_operation("not", not);
|
|
122
|
+
json_logic_js_1.default.add_operation("does-not-contain", not);
|
|
123
|
+
json_logic_js_1.default.add_operation("empty", function (value) {
|
|
124
|
+
if (!Array.isArray(value)) {
|
|
125
|
+
var stringValue = typeof value === "number" ? value.toString() : value;
|
|
126
|
+
return stringValue.trim().length === 0;
|
|
127
|
+
}
|
|
128
|
+
var filter = value.filter(function (_value) {
|
|
129
|
+
var stringValue = typeof _value === "number" ? _value.toString() : _value;
|
|
130
|
+
return stringValue.trim().length === 0;
|
|
131
|
+
});
|
|
105
132
|
return value.length === filter.length;
|
|
106
133
|
});
|
|
107
|
-
json_logic_js_1.default.add_operation("
|
|
108
|
-
if (!Array.isArray(value))
|
|
109
|
-
|
|
110
|
-
|
|
134
|
+
json_logic_js_1.default.add_operation("not-empty", function (value) {
|
|
135
|
+
if (!Array.isArray(value)) {
|
|
136
|
+
var stringValue = typeof value === "number" ? value.toString() : value;
|
|
137
|
+
return stringValue.trim().length > 0;
|
|
138
|
+
}
|
|
139
|
+
var filter = value.filter(function (_value) {
|
|
140
|
+
var stringValue = typeof _value === "number" ? _value.toString() : _value;
|
|
141
|
+
return stringValue.trim().length > 0;
|
|
142
|
+
});
|
|
111
143
|
return value.length === filter.length;
|
|
112
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).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).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);
|
|
113
165
|
var LpLogic = json_logic_js_1.default.apply;
|
|
114
166
|
exports.default = LpLogic;
|
package/package.json
CHANGED
package/tests/LpLogic.test.ts
CHANGED
|
@@ -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
|
})
|
package/tests/Parser.test.ts
DELETED
|
@@ -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
|
-
})
|
package/tests/References.test.ts
DELETED
|
@@ -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
|
-
})
|