@legalplace/prerenderx 2.1.2 → 2.2.0
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/libs/ConditionsRunner.d.ts +2 -5
- package/dist/libs/ConditionsRunner.js +4 -53
- package/dist/libs/DataPopulator.d.ts +49 -0
- package/dist/libs/DataPopulator.js +153 -0
- package/dist/libs/OutputParser.js +1 -0
- package/package.json +1 -1
- package/src/libs/ConditionsRunner.ts +26 -97
- package/src/libs/DataPopulator.ts +291 -0
- package/src/libs/OutputParser.ts +3 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Types } from "@legalplace/referencesparser";
|
|
2
2
|
import { ConditionV3 } from "@legalplace/models-v3-types";
|
|
3
|
+
import { ConditionDataMap } from "./DataPopulator";
|
|
3
4
|
declare type OVC_TYPE = {
|
|
4
5
|
options: {
|
|
5
6
|
[key: string]: boolean[];
|
|
@@ -12,15 +13,11 @@ declare class ConditionsRunner {
|
|
|
12
13
|
private references;
|
|
13
14
|
private ovc;
|
|
14
15
|
constructor(references: Types.ReferencesType, ovc: OVC_TYPE);
|
|
15
|
-
private populateData;
|
|
16
16
|
isOptionRelatedToOption(optionA: number, optionB: number): boolean;
|
|
17
17
|
isVariableRelatedToOption(variableId: number, optionId: number): boolean;
|
|
18
18
|
isVariableRelatedToVariable(variableA: number, variableB: number): boolean;
|
|
19
19
|
executeCondition(condition: {
|
|
20
|
-
dataMap:
|
|
21
|
-
o: number[];
|
|
22
|
-
v: number[];
|
|
23
|
-
};
|
|
20
|
+
dataMap: ConditionDataMap;
|
|
24
21
|
conditions: ConditionV3;
|
|
25
22
|
}, id: number, index: number, conditionType: "options" | "optionValidator" | "variables" | "prefillers" | "variableValidator" | "sections" | "documents"): any;
|
|
26
23
|
}
|
|
@@ -4,61 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
var lplogic_1 = __importDefault(require("@legalplace/lplogic"));
|
|
7
|
+
var DataPopulator_1 = __importDefault(require("./DataPopulator"));
|
|
7
8
|
var ConditionsRunner = (function () {
|
|
8
9
|
function ConditionsRunner(references, ovc) {
|
|
9
10
|
this.references = references;
|
|
10
11
|
this.ovc = ovc;
|
|
11
12
|
}
|
|
12
|
-
ConditionsRunner.prototype.populateData = function (id, index, dataMap, withRelations) {
|
|
13
|
-
var _this = this;
|
|
14
|
-
if (withRelations === void 0) { withRelations = true; }
|
|
15
|
-
var data = {
|
|
16
|
-
o: {},
|
|
17
|
-
v: {}
|
|
18
|
-
};
|
|
19
|
-
dataMap.o.forEach(function (currentOptionId) {
|
|
20
|
-
var value = _this.ovc.options[currentOptionId];
|
|
21
|
-
var currentOptionParents = _this.references.relations.options[currentOptionId].parents;
|
|
22
|
-
var currentOptionRoot = currentOptionParents[currentOptionParents.length - 1];
|
|
23
|
-
var currentOptionRootRelations = currentOptionRoot
|
|
24
|
-
? _this.references.relations.options[currentOptionId]
|
|
25
|
-
: undefined;
|
|
26
|
-
if (withRelations === true && currentOptionRootRelations) {
|
|
27
|
-
if (id === currentOptionId ||
|
|
28
|
-
currentOptionRootRelations.children.options.includes(id) ||
|
|
29
|
-
currentOptionRootRelations.dependants.includes(id))
|
|
30
|
-
value = [value[index]];
|
|
31
|
-
}
|
|
32
|
-
data.o[currentOptionId] = value;
|
|
33
|
-
});
|
|
34
|
-
dataMap.v.forEach(function (currentVariableId) {
|
|
35
|
-
var variableReference = _this.references.variables[currentVariableId];
|
|
36
|
-
var value = _this.ovc.variables[currentVariableId];
|
|
37
|
-
var variableParents = _this.references.relations.variables[currentVariableId].parents;
|
|
38
|
-
var currentOptionId = variableParents[0];
|
|
39
|
-
var currentOptionParents = _this.references.relations.options[currentOptionId].parents;
|
|
40
|
-
var currentOptionRoot = currentOptionParents[currentOptionParents.length - 1];
|
|
41
|
-
var currentOptionRootRelations = currentOptionRoot
|
|
42
|
-
? _this.references.relations.options[currentOptionId]
|
|
43
|
-
: undefined;
|
|
44
|
-
if (withRelations === true && currentOptionRootRelations) {
|
|
45
|
-
if (currentOptionRootRelations.children.options.includes(currentOptionId) ||
|
|
46
|
-
currentOptionRootRelations.dependants.includes(currentOptionId)) {
|
|
47
|
-
value = [value[index]];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
var dataValue = value.map(function (v) {
|
|
51
|
-
return v === null ? "" : v;
|
|
52
|
-
});
|
|
53
|
-
if (variableReference.type === "number") {
|
|
54
|
-
dataValue = dataValue.map(function (v) {
|
|
55
|
-
return typeof v === "number" ? v : parseFloat(v);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
data.v[currentVariableId] = dataValue;
|
|
59
|
-
});
|
|
60
|
-
return data;
|
|
61
|
-
};
|
|
62
13
|
ConditionsRunner.prototype.isOptionRelatedToOption = function (optionA, optionB) {
|
|
63
14
|
var OptionBParents = this.references.relations.options[optionB].parents;
|
|
64
15
|
var OptionBRoot = OptionBParents[OptionBParents.length - 1];
|
|
@@ -116,16 +67,16 @@ var ConditionsRunner = (function () {
|
|
|
116
67
|
});
|
|
117
68
|
var currentData;
|
|
118
69
|
if (conditionType === "options" || conditionType === "optionValidator") {
|
|
119
|
-
currentData =
|
|
70
|
+
currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, id, index).getData();
|
|
120
71
|
}
|
|
121
72
|
else if (conditionType === "variables" ||
|
|
122
73
|
conditionType === "prefillers" ||
|
|
123
74
|
conditionType === "variableValidator") {
|
|
124
75
|
var dataId = this.references.relations.variables[id].parents[0];
|
|
125
|
-
currentData =
|
|
76
|
+
currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, dataId, index).getData();
|
|
126
77
|
}
|
|
127
78
|
else if (conditionType === "sections" || conditionType === "documents") {
|
|
128
|
-
currentData =
|
|
79
|
+
currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, id, index).getData();
|
|
129
80
|
}
|
|
130
81
|
return lplogic_1.default(condition.conditions, currentData);
|
|
131
82
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Types } from "@legalplace/referencesparser";
|
|
2
|
+
import ConditionsRunner from "./ConditionsRunner";
|
|
3
|
+
declare type OVC_TYPE = {
|
|
4
|
+
options: {
|
|
5
|
+
[key: string]: boolean[];
|
|
6
|
+
};
|
|
7
|
+
variables: {
|
|
8
|
+
[key: string]: (number | string)[];
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
declare class DataPopulator {
|
|
12
|
+
private references;
|
|
13
|
+
private ovc;
|
|
14
|
+
private conditionsRunner;
|
|
15
|
+
private id?;
|
|
16
|
+
private index?;
|
|
17
|
+
private dataMap;
|
|
18
|
+
private data;
|
|
19
|
+
constructor(conditionsRunner: ConditionsRunner, references: Types.ReferencesType, ovc: OVC_TYPE, dataMap: ConditionDataMap, id?: number, index?: number);
|
|
20
|
+
getData(): Readonly<ConditionData>;
|
|
21
|
+
private populateConditions;
|
|
22
|
+
private populateVariables;
|
|
23
|
+
private getVariableValues;
|
|
24
|
+
private getVariableRootParent;
|
|
25
|
+
private cleanArrayFromNullValues;
|
|
26
|
+
populateOptions(): void;
|
|
27
|
+
private getOptionInputs;
|
|
28
|
+
private getOptionRootParent;
|
|
29
|
+
private optionConditionValue;
|
|
30
|
+
private variableConditionValue;
|
|
31
|
+
private sectionConditionValue;
|
|
32
|
+
}
|
|
33
|
+
export interface ConditionDataMap {
|
|
34
|
+
o: number[];
|
|
35
|
+
v: number[];
|
|
36
|
+
c: {
|
|
37
|
+
o: number[];
|
|
38
|
+
s: number[];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface ConditionData {
|
|
42
|
+
o: Record<string, boolean[]>;
|
|
43
|
+
v: Record<string, (string | number)[]>;
|
|
44
|
+
c: {
|
|
45
|
+
o: Record<string, boolean[]>;
|
|
46
|
+
s: Record<string, boolean[]>;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export default DataPopulator;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
+
r[k] = a[j];
|
|
7
|
+
return r;
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
var DataPopulator = (function () {
|
|
11
|
+
function DataPopulator(conditionsRunner, references, ovc, dataMap, id, index) {
|
|
12
|
+
this.data = {
|
|
13
|
+
o: {},
|
|
14
|
+
v: {},
|
|
15
|
+
c: {
|
|
16
|
+
o: {},
|
|
17
|
+
s: {}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
this.id = id;
|
|
21
|
+
this.ovc = ovc;
|
|
22
|
+
this.index = index;
|
|
23
|
+
this.dataMap = dataMap;
|
|
24
|
+
this.references = references;
|
|
25
|
+
this.conditionsRunner = conditionsRunner;
|
|
26
|
+
this.populateVariables();
|
|
27
|
+
this.populateOptions();
|
|
28
|
+
this.populateConditions();
|
|
29
|
+
}
|
|
30
|
+
DataPopulator.prototype.getData = function () {
|
|
31
|
+
return this.data;
|
|
32
|
+
};
|
|
33
|
+
DataPopulator.prototype.populateConditions = function () {
|
|
34
|
+
for (var i = 0; i < this.dataMap.c.s.length; i += 1) {
|
|
35
|
+
var sectionId = this.dataMap.c.s[i];
|
|
36
|
+
var sectionCondition = this.sectionConditionValue(sectionId);
|
|
37
|
+
this.data.c.s[sectionId] = [sectionCondition === true];
|
|
38
|
+
}
|
|
39
|
+
for (var i = 0; i < this.dataMap.c.o.length; i += 1) {
|
|
40
|
+
var optionId = this.dataMap.c.o[i];
|
|
41
|
+
var optionCondition = this.optionConditionValue(optionId);
|
|
42
|
+
this.data.c.o[optionId] =
|
|
43
|
+
optionCondition === undefined ? [false] : optionCondition;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
DataPopulator.prototype.populateVariables = function () {
|
|
47
|
+
for (var i = 0; i < this.dataMap.v.length; i += 1) {
|
|
48
|
+
var variableId = this.dataMap.v[i];
|
|
49
|
+
var variableReference = this.references.variables[variableId];
|
|
50
|
+
var values = this.getVariableValues(variableId);
|
|
51
|
+
if (variableReference.type === "number") {
|
|
52
|
+
values = values.map(function (v) { return (typeof v === "number" ? v : parseFloat(v)); });
|
|
53
|
+
}
|
|
54
|
+
this.data.v[variableId] = __spreadArrays(values);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
DataPopulator.prototype.getVariableValues = function (variableId) {
|
|
58
|
+
var conditions = this.variableConditionValue(variableId);
|
|
59
|
+
var value = this.ovc.variables[variableId];
|
|
60
|
+
if (this.id === undefined || this.index === undefined) {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
var variableRootParent = this.getVariableRootParent(variableId);
|
|
64
|
+
var variableRootRelations = this.references.relations.options[variableRootParent];
|
|
65
|
+
if (variableRootRelations.children.options.includes(this.id) ||
|
|
66
|
+
variableRootRelations.dependants.includes(this.id) ||
|
|
67
|
+
this.id === variableRootParent) {
|
|
68
|
+
if (conditions !== undefined && conditions[this.index] === false)
|
|
69
|
+
return ["fAl$e"];
|
|
70
|
+
return [value[this.index]];
|
|
71
|
+
}
|
|
72
|
+
var cleanValues = this.cleanArrayFromNullValues(__spreadArrays(value));
|
|
73
|
+
if (conditions === undefined)
|
|
74
|
+
return cleanValues;
|
|
75
|
+
return cleanValues.map(function (currentValue, index) {
|
|
76
|
+
if (conditions[index] === false)
|
|
77
|
+
return "fAl$e";
|
|
78
|
+
return currentValue;
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
DataPopulator.prototype.getVariableRootParent = function (variableId) {
|
|
82
|
+
var variableParents = this.references.relations.variables[variableId]
|
|
83
|
+
.parents;
|
|
84
|
+
return variableParents[variableParents.length - 1];
|
|
85
|
+
};
|
|
86
|
+
DataPopulator.prototype.cleanArrayFromNullValues = function (arr) {
|
|
87
|
+
return arr.map(function (v) { return (v === null ? "" : v); });
|
|
88
|
+
};
|
|
89
|
+
DataPopulator.prototype.populateOptions = function () {
|
|
90
|
+
for (var i = 0; i < this.dataMap.o.length; i += 1) {
|
|
91
|
+
var optionId = this.dataMap.o[i];
|
|
92
|
+
var inputs = this.getOptionInputs(optionId);
|
|
93
|
+
this.data.o[optionId] = __spreadArrays(inputs);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
DataPopulator.prototype.getOptionInputs = function (optionId) {
|
|
97
|
+
var conditions = this.optionConditionValue(optionId);
|
|
98
|
+
var inputs = this.ovc.options[optionId];
|
|
99
|
+
if (this.id === undefined || this.index === undefined) {
|
|
100
|
+
return inputs;
|
|
101
|
+
}
|
|
102
|
+
var optionRootParent = this.getOptionRootParent(optionId);
|
|
103
|
+
var optionRootRelations = this.references.relations.options[optionRootParent];
|
|
104
|
+
if (optionRootRelations.children.options.includes(this.id) ||
|
|
105
|
+
optionRootRelations.dependants.includes(this.id) ||
|
|
106
|
+
this.id === optionRootParent) {
|
|
107
|
+
if (conditions !== undefined && conditions[this.index] === false)
|
|
108
|
+
return [false];
|
|
109
|
+
return [inputs[this.index]];
|
|
110
|
+
}
|
|
111
|
+
if (conditions === undefined)
|
|
112
|
+
return inputs;
|
|
113
|
+
return inputs.map(function (value, index) {
|
|
114
|
+
if (conditions[index] === false)
|
|
115
|
+
return false;
|
|
116
|
+
return value;
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
DataPopulator.prototype.getOptionRootParent = function (optionId) {
|
|
120
|
+
var optionParents = this.references.relations.options[optionId].parents;
|
|
121
|
+
return optionParents.length > 0
|
|
122
|
+
? optionParents[optionParents.length - 1]
|
|
123
|
+
: optionId;
|
|
124
|
+
};
|
|
125
|
+
DataPopulator.prototype.optionConditionValue = function (optionId) {
|
|
126
|
+
var _this = this;
|
|
127
|
+
var conditions = this.references.conditions.options[optionId];
|
|
128
|
+
if (conditions === undefined)
|
|
129
|
+
return undefined;
|
|
130
|
+
var inputs = this.ovc.options[optionId];
|
|
131
|
+
return inputs.map(function (input, index) {
|
|
132
|
+
return _this.conditionsRunner.executeCondition(conditions, optionId, index, "options");
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
DataPopulator.prototype.variableConditionValue = function (variableId) {
|
|
136
|
+
var _this = this;
|
|
137
|
+
var conditions = this.references.conditions.variables[variableId];
|
|
138
|
+
if (conditions === undefined)
|
|
139
|
+
return undefined;
|
|
140
|
+
var inputs = this.ovc.variables[variableId];
|
|
141
|
+
return inputs.map(function (input, index) {
|
|
142
|
+
return _this.conditionsRunner.executeCondition(conditions, variableId, index, "variables");
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
DataPopulator.prototype.sectionConditionValue = function (sectionId) {
|
|
146
|
+
var conditions = this.references.conditions.sections.main[sectionId];
|
|
147
|
+
if (conditions === undefined)
|
|
148
|
+
return undefined;
|
|
149
|
+
return this.conditionsRunner.executeCondition(conditions, 0, 0, "sections");
|
|
150
|
+
};
|
|
151
|
+
return DataPopulator;
|
|
152
|
+
}());
|
|
153
|
+
exports.default = DataPopulator;
|
|
@@ -39,6 +39,7 @@ exports.parseOutputWithVariables = function (output, variables, autonums) {
|
|
|
39
39
|
parsedOutput = parsedOutput.replace(new RegExp("\\[var:" + variableId + "\\]", "g"), value);
|
|
40
40
|
});
|
|
41
41
|
parsedOutput = parsedOutput.replace(/\[\[PAGE_BREAK\]\]/gi, '<p class="pagebreak"><!-- pagebreak --></p>');
|
|
42
|
+
parsedOutput = parsedOutput.replace(/\[(E|B)_BLUR\]/gi, "");
|
|
42
43
|
autonums.forEach(function (currentAn) {
|
|
43
44
|
if (currentAn[3] === null)
|
|
44
45
|
parsedOutput = parsedOutput.replace("[" + currentAn[0] + ":" + currentAn[1] + "]", "<span class=\"" + currentAn[0] + "\">" + currentAn[2] + "</span>");
|
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import LpLogic from "@legalplace/lplogic";
|
|
3
3
|
import { Types } from "@legalplace/referencesparser";
|
|
4
4
|
import { ConditionV3 } from "@legalplace/models-v3-types";
|
|
5
|
+
import DataPopulator, { ConditionDataMap } from "./DataPopulator";
|
|
5
6
|
|
|
6
7
|
type OVC_TYPE = {
|
|
7
8
|
options: {
|
|
@@ -12,11 +13,6 @@ type OVC_TYPE = {
|
|
|
12
13
|
};
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
interface ConditionDataMap {
|
|
16
|
-
o: number[];
|
|
17
|
-
v: number[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
16
|
interface ConditionData {
|
|
21
17
|
o: Record<string, boolean[]>;
|
|
22
18
|
v: Record<string, (string | number)[]>;
|
|
@@ -32,94 +28,6 @@ class ConditionsRunner {
|
|
|
32
28
|
this.ovc = ovc;
|
|
33
29
|
}
|
|
34
30
|
|
|
35
|
-
private populateData(
|
|
36
|
-
id: number,
|
|
37
|
-
index: number,
|
|
38
|
-
dataMap: ConditionDataMap,
|
|
39
|
-
withRelations = true
|
|
40
|
-
) {
|
|
41
|
-
const data: ConditionData = {
|
|
42
|
-
o: {},
|
|
43
|
-
v: {}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// Populating options
|
|
47
|
-
dataMap.o.forEach(currentOptionId => {
|
|
48
|
-
// Getting option value
|
|
49
|
-
let value = this.ovc.options[currentOptionId];
|
|
50
|
-
|
|
51
|
-
// Getting current relations
|
|
52
|
-
const currentOptionParents = this.references.relations.options[
|
|
53
|
-
currentOptionId
|
|
54
|
-
].parents;
|
|
55
|
-
const currentOptionRoot =
|
|
56
|
-
currentOptionParents[currentOptionParents.length - 1];
|
|
57
|
-
const currentOptionRootRelations = currentOptionRoot
|
|
58
|
-
? this.references.relations.options[currentOptionId]
|
|
59
|
-
: undefined;
|
|
60
|
-
|
|
61
|
-
// Checking if current option is related to the option that changed
|
|
62
|
-
if (withRelations === true && currentOptionRootRelations) {
|
|
63
|
-
if (
|
|
64
|
-
id === currentOptionId ||
|
|
65
|
-
currentOptionRootRelations.children.options.includes(id) ||
|
|
66
|
-
currentOptionRootRelations.dependants.includes(id)
|
|
67
|
-
)
|
|
68
|
-
value = [value[index]];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
data.o[currentOptionId] = value;
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Populating variables
|
|
75
|
-
dataMap.v.forEach(currentVariableId => {
|
|
76
|
-
// Getting variable data
|
|
77
|
-
const variableReference = this.references.variables[currentVariableId];
|
|
78
|
-
let value = this.ovc.variables[currentVariableId];
|
|
79
|
-
const variableParents = this.references.relations.variables[
|
|
80
|
-
currentVariableId
|
|
81
|
-
].parents;
|
|
82
|
-
const currentOptionId = variableParents[0];
|
|
83
|
-
|
|
84
|
-
// Getting current relations
|
|
85
|
-
const currentOptionParents = this.references.relations.options[
|
|
86
|
-
currentOptionId
|
|
87
|
-
].parents;
|
|
88
|
-
const currentOptionRoot =
|
|
89
|
-
currentOptionParents[currentOptionParents.length - 1];
|
|
90
|
-
const currentOptionRootRelations = currentOptionRoot
|
|
91
|
-
? this.references.relations.options[currentOptionId]
|
|
92
|
-
: undefined;
|
|
93
|
-
|
|
94
|
-
// Checking if current option is related to the option that changed
|
|
95
|
-
if (withRelations === true && currentOptionRootRelations) {
|
|
96
|
-
if (
|
|
97
|
-
currentOptionRootRelations.children.options.includes(
|
|
98
|
-
currentOptionId
|
|
99
|
-
) ||
|
|
100
|
-
currentOptionRootRelations.dependants.includes(currentOptionId)
|
|
101
|
-
) {
|
|
102
|
-
value = [value[index]];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
let dataValue: (string | number)[] = value.map(v =>
|
|
107
|
-
v === null ? "" : v
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
// If variable is of type number
|
|
111
|
-
if (variableReference.type === "number") {
|
|
112
|
-
dataValue = dataValue.map(v =>
|
|
113
|
-
typeof v === "number" ? v : parseFloat(v)
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
data.v[currentVariableId] = dataValue;
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
return data;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
31
|
isOptionRelatedToOption(optionA: number, optionB: number): boolean {
|
|
124
32
|
// Getting B relations
|
|
125
33
|
const OptionBParents = this.references.relations.options[optionB].parents;
|
|
@@ -196,7 +104,7 @@ class ConditionsRunner {
|
|
|
196
104
|
|
|
197
105
|
executeCondition(
|
|
198
106
|
condition: {
|
|
199
|
-
dataMap:
|
|
107
|
+
dataMap: ConditionDataMap;
|
|
200
108
|
conditions: ConditionV3;
|
|
201
109
|
},
|
|
202
110
|
id: number,
|
|
@@ -218,16 +126,37 @@ class ConditionsRunner {
|
|
|
218
126
|
// Populating Data
|
|
219
127
|
let currentData;
|
|
220
128
|
if (conditionType === "options" || conditionType === "optionValidator") {
|
|
221
|
-
currentData =
|
|
129
|
+
currentData = new DataPopulator(
|
|
130
|
+
this,
|
|
131
|
+
this.references,
|
|
132
|
+
this.ovc,
|
|
133
|
+
condition.dataMap,
|
|
134
|
+
id,
|
|
135
|
+
index
|
|
136
|
+
).getData();
|
|
222
137
|
} else if (
|
|
223
138
|
conditionType === "variables" ||
|
|
224
139
|
conditionType === "prefillers" ||
|
|
225
140
|
conditionType === "variableValidator"
|
|
226
141
|
) {
|
|
227
142
|
const dataId = this.references.relations.variables[id].parents[0];
|
|
228
|
-
currentData =
|
|
143
|
+
currentData = new DataPopulator(
|
|
144
|
+
this,
|
|
145
|
+
this.references,
|
|
146
|
+
this.ovc,
|
|
147
|
+
condition.dataMap,
|
|
148
|
+
dataId,
|
|
149
|
+
index
|
|
150
|
+
).getData();
|
|
229
151
|
} else if (conditionType === "sections" || conditionType === "documents") {
|
|
230
|
-
currentData =
|
|
152
|
+
currentData = new DataPopulator(
|
|
153
|
+
this,
|
|
154
|
+
this.references,
|
|
155
|
+
this.ovc,
|
|
156
|
+
condition.dataMap,
|
|
157
|
+
id,
|
|
158
|
+
index
|
|
159
|
+
).getData();
|
|
231
160
|
}
|
|
232
161
|
|
|
233
162
|
return LpLogic(condition.conditions, currentData);
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { Types } from "@legalplace/referencesparser";
|
|
2
|
+
import ConditionsRunner from "./ConditionsRunner";
|
|
3
|
+
|
|
4
|
+
type OVC_TYPE = {
|
|
5
|
+
options: {
|
|
6
|
+
[key: string]: boolean[];
|
|
7
|
+
};
|
|
8
|
+
variables: {
|
|
9
|
+
[key: string]: (number | string)[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
class DataPopulator {
|
|
14
|
+
private references: Types.ReferencesType;
|
|
15
|
+
|
|
16
|
+
private ovc: OVC_TYPE;
|
|
17
|
+
|
|
18
|
+
private conditionsRunner: ConditionsRunner;
|
|
19
|
+
|
|
20
|
+
private id?: number;
|
|
21
|
+
|
|
22
|
+
private index?: number;
|
|
23
|
+
|
|
24
|
+
private dataMap: ConditionDataMap;
|
|
25
|
+
|
|
26
|
+
private data: ConditionData = {
|
|
27
|
+
o: {},
|
|
28
|
+
v: {},
|
|
29
|
+
c: {
|
|
30
|
+
o: {},
|
|
31
|
+
s: {}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
conditionsRunner: ConditionsRunner,
|
|
37
|
+
references: Types.ReferencesType,
|
|
38
|
+
ovc: OVC_TYPE,
|
|
39
|
+
dataMap: ConditionDataMap,
|
|
40
|
+
id?: number,
|
|
41
|
+
index?: number
|
|
42
|
+
) {
|
|
43
|
+
this.id = id;
|
|
44
|
+
this.ovc = ovc;
|
|
45
|
+
this.index = index;
|
|
46
|
+
this.dataMap = dataMap;
|
|
47
|
+
this.references = references;
|
|
48
|
+
this.conditionsRunner = conditionsRunner;
|
|
49
|
+
|
|
50
|
+
// Populating variables
|
|
51
|
+
this.populateVariables();
|
|
52
|
+
|
|
53
|
+
// Populating options
|
|
54
|
+
this.populateOptions();
|
|
55
|
+
|
|
56
|
+
// Populating conditions
|
|
57
|
+
this.populateConditions();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns resulting data object
|
|
62
|
+
*/
|
|
63
|
+
public getData(): Readonly<ConditionData> {
|
|
64
|
+
return this.data;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private populateConditions(): void {
|
|
68
|
+
// Sections
|
|
69
|
+
for (let i = 0; i < this.dataMap.c.s.length; i += 1) {
|
|
70
|
+
const sectionId = this.dataMap.c.s[i];
|
|
71
|
+
const sectionCondition = this.sectionConditionValue(sectionId);
|
|
72
|
+
this.data.c.s[sectionId] = [sectionCondition === true];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Options
|
|
76
|
+
for (let i = 0; i < this.dataMap.c.o.length; i += 1) {
|
|
77
|
+
const optionId = this.dataMap.c.o[i];
|
|
78
|
+
const optionCondition = this.optionConditionValue(optionId);
|
|
79
|
+
this.data.c.o[optionId] =
|
|
80
|
+
optionCondition === undefined ? [false] : optionCondition;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Populates variables according to the current dataMap object
|
|
86
|
+
*/
|
|
87
|
+
private populateVariables(): void {
|
|
88
|
+
for (let i = 0; i < this.dataMap.v.length; i += 1) {
|
|
89
|
+
const variableId = this.dataMap.v[i];
|
|
90
|
+
const variableReference = this.references.variables[variableId];
|
|
91
|
+
let values = this.getVariableValues(variableId);
|
|
92
|
+
|
|
93
|
+
// If variable is of type number
|
|
94
|
+
if (variableReference.type === "number") {
|
|
95
|
+
values = values.map(v => (typeof v === "number" ? v : parseFloat(v)));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Adding variables
|
|
99
|
+
this.data.v[variableId] = [...values];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns a give variables value
|
|
105
|
+
* @param variableId Variable's id
|
|
106
|
+
*/
|
|
107
|
+
private getVariableValues(variableId: number): Readonly<(string | number)[]> {
|
|
108
|
+
const conditions = this.variableConditionValue(variableId);
|
|
109
|
+
const value = this.ovc.variables[variableId];
|
|
110
|
+
if (this.id === undefined || this.index === undefined) {
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Getting variable parent option
|
|
115
|
+
const variableRootParent = this.getVariableRootParent(variableId);
|
|
116
|
+
const variableRootRelations = this.references.relations.options[
|
|
117
|
+
variableRootParent
|
|
118
|
+
];
|
|
119
|
+
if (
|
|
120
|
+
variableRootRelations.children.options.includes(this.id) ||
|
|
121
|
+
variableRootRelations.dependants.includes(this.id) ||
|
|
122
|
+
this.id === variableRootParent
|
|
123
|
+
) {
|
|
124
|
+
// TODO: Replace fAl$e with a safer solution
|
|
125
|
+
if (conditions !== undefined && conditions[this.index] === false)
|
|
126
|
+
return ["fAl$e"];
|
|
127
|
+
return [value[this.index]];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const cleanValues = this.cleanArrayFromNullValues([...value]);
|
|
131
|
+
|
|
132
|
+
if (conditions === undefined) return cleanValues;
|
|
133
|
+
|
|
134
|
+
return cleanValues.map((currentValue, index) => {
|
|
135
|
+
if (conditions[index] === false) return "fAl$e";
|
|
136
|
+
return currentValue;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Return a given variable root option relations
|
|
142
|
+
* @param variableId Variable's id
|
|
143
|
+
*/
|
|
144
|
+
private getVariableRootParent(variableId: number) {
|
|
145
|
+
const variableParents = this.references.relations.variables[variableId]
|
|
146
|
+
.parents;
|
|
147
|
+
return variableParents[variableParents.length - 1];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Cleans values array from any null value by replacing it
|
|
152
|
+
* with an empty string
|
|
153
|
+
* @param arr Values array
|
|
154
|
+
*/
|
|
155
|
+
private cleanArrayFromNullValues(
|
|
156
|
+
arr: (string | number | null)[]
|
|
157
|
+
): (string | number)[] {
|
|
158
|
+
return arr.map(v => (v === null ? "" : v));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Populates options data object accodring to current dataMap object
|
|
163
|
+
*/
|
|
164
|
+
populateOptions() {
|
|
165
|
+
for (let i = 0; i < this.dataMap.o.length; i += 1) {
|
|
166
|
+
const optionId = this.dataMap.o[i];
|
|
167
|
+
const inputs = this.getOptionInputs(optionId);
|
|
168
|
+
|
|
169
|
+
// Adding option
|
|
170
|
+
this.data.o[optionId] = [...inputs];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Returns a give option's inputs
|
|
176
|
+
* @param optionId Option's id
|
|
177
|
+
*/
|
|
178
|
+
private getOptionInputs(optionId: number): Readonly<boolean[]> {
|
|
179
|
+
const conditions = this.optionConditionValue(optionId);
|
|
180
|
+
const inputs = this.ovc.options[optionId];
|
|
181
|
+
if (this.id === undefined || this.index === undefined) {
|
|
182
|
+
return inputs;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Getting variable parent option
|
|
186
|
+
const optionRootParent = this.getOptionRootParent(optionId);
|
|
187
|
+
const optionRootRelations = this.references.relations.options[
|
|
188
|
+
optionRootParent
|
|
189
|
+
];
|
|
190
|
+
if (
|
|
191
|
+
optionRootRelations.children.options.includes(this.id) ||
|
|
192
|
+
optionRootRelations.dependants.includes(this.id) ||
|
|
193
|
+
this.id === optionRootParent
|
|
194
|
+
) {
|
|
195
|
+
if (conditions !== undefined && conditions[this.index] === false)
|
|
196
|
+
return [false];
|
|
197
|
+
return [inputs[this.index]];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (conditions === undefined) return inputs;
|
|
201
|
+
|
|
202
|
+
return inputs.map((value, index) => {
|
|
203
|
+
if (conditions[index] === false) return false;
|
|
204
|
+
return value;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Returns option's root parent
|
|
210
|
+
* @param optionId Root parent
|
|
211
|
+
*/
|
|
212
|
+
private getOptionRootParent(optionId: number) {
|
|
213
|
+
const optionParents = this.references.relations.options[optionId].parents;
|
|
214
|
+
return optionParents.length > 0
|
|
215
|
+
? optionParents[optionParents.length - 1]
|
|
216
|
+
: optionId;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Calculates option's condition
|
|
221
|
+
*/
|
|
222
|
+
private optionConditionValue(optionId: number) {
|
|
223
|
+
// Getting option conditions
|
|
224
|
+
const conditions = this.references.conditions.options[optionId];
|
|
225
|
+
if (conditions === undefined) return undefined;
|
|
226
|
+
|
|
227
|
+
// Getting option inputs length
|
|
228
|
+
const inputs = this.ovc.options[optionId];
|
|
229
|
+
|
|
230
|
+
return inputs.map((input, index) => {
|
|
231
|
+
return this.conditionsRunner.executeCondition(
|
|
232
|
+
conditions,
|
|
233
|
+
optionId,
|
|
234
|
+
index,
|
|
235
|
+
"options"
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Calculates variable's condition
|
|
242
|
+
*/
|
|
243
|
+
private variableConditionValue(variableId: number) {
|
|
244
|
+
// Getting variable conditions
|
|
245
|
+
const conditions = this.references.conditions.variables[variableId];
|
|
246
|
+
if (conditions === undefined) return undefined;
|
|
247
|
+
|
|
248
|
+
// Getting variable inputs length
|
|
249
|
+
const inputs = this.ovc.variables[variableId];
|
|
250
|
+
|
|
251
|
+
return inputs.map((input, index) => {
|
|
252
|
+
return this.conditionsRunner.executeCondition(
|
|
253
|
+
conditions,
|
|
254
|
+
variableId,
|
|
255
|
+
index,
|
|
256
|
+
"variables"
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Calculates section's condition
|
|
263
|
+
*/
|
|
264
|
+
private sectionConditionValue(sectionId: number) {
|
|
265
|
+
// Getting section conditions
|
|
266
|
+
const conditions = this.references.conditions.sections.main[sectionId];
|
|
267
|
+
if (conditions === undefined) return undefined;
|
|
268
|
+
|
|
269
|
+
return this.conditionsRunner.executeCondition(conditions, 0, 0, "sections");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface ConditionDataMap {
|
|
274
|
+
o: number[];
|
|
275
|
+
v: number[];
|
|
276
|
+
c: {
|
|
277
|
+
o: number[];
|
|
278
|
+
s: number[];
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export interface ConditionData {
|
|
283
|
+
o: Record<string, boolean[]>;
|
|
284
|
+
v: Record<string, (string | number)[]>;
|
|
285
|
+
c: {
|
|
286
|
+
o: Record<string, boolean[]>;
|
|
287
|
+
s: Record<string, boolean[]>;
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export default DataPopulator;
|
package/src/libs/OutputParser.ts
CHANGED
|
@@ -104,6 +104,9 @@ export const parseOutputWithVariables: parseOutputT = (
|
|
|
104
104
|
'<p class="pagebreak"><!-- pagebreak --></p>'
|
|
105
105
|
);
|
|
106
106
|
|
|
107
|
+
// Cleaning BLURS
|
|
108
|
+
parsedOutput = parsedOutput.replace(/\[(E|B)_BLUR\]/gi, "");
|
|
109
|
+
|
|
107
110
|
// Inserting autonums
|
|
108
111
|
autonums.forEach(currentAn => {
|
|
109
112
|
if (currentAn[3] === null)
|