@legalplace/prerenderx 3.0.0 → 3.1.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.
@@ -1,175 +0,0 @@
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
- v: {}
19
- }
20
- };
21
- this.id = id;
22
- this.ovc = ovc;
23
- this.index = index;
24
- this.dataMap = dataMap;
25
- this.references = references;
26
- this.conditionsRunner = conditionsRunner;
27
- this.populateVariables();
28
- this.populateOptions();
29
- this.populateConditions();
30
- }
31
- DataPopulator.prototype.getData = function () {
32
- return this.data;
33
- };
34
- DataPopulator.prototype.populateConditions = function () {
35
- for (var i = 0; i < this.dataMap.c.s.length; i += 1) {
36
- var sectionId = this.dataMap.c.s[i];
37
- var sectionCondition = this.sectionConditionValue(sectionId);
38
- this.data.c.s[sectionId] = [sectionCondition !== false];
39
- }
40
- for (var i = 0; i < this.dataMap.c.o.length; i += 1) {
41
- var optionId = this.dataMap.c.o[i];
42
- var optionCondition = __spreadArrays(this.getOptionConditions(optionId));
43
- this.data.c.o[optionId] = optionCondition === undefined ? [false] : optionCondition;
44
- }
45
- for (var i = 0; i < this.dataMap.c.v.length; i += 1) {
46
- var variableId = this.dataMap.c.v[i];
47
- var variableCondition = __spreadArrays(this.getVariablesConditions(variableId));
48
- this.data.c.v[variableId] = variableCondition === undefined ? [false] : variableCondition;
49
- }
50
- };
51
- DataPopulator.prototype.getOptionConditions = function (optionId) {
52
- var conditions = this.optionConditionValue(optionId);
53
- if (conditions === undefined)
54
- return [true];
55
- if (this.id === undefined || this.index === undefined) {
56
- return conditions;
57
- }
58
- var optionRootParent = this.getOptionRootParent(optionId);
59
- var optionRootRelations = this.references.relations.options[optionRootParent];
60
- if (optionRootRelations.children.options.includes(this.id) ||
61
- optionRootRelations.dependants.includes(this.id) ||
62
- this.id === optionRootParent) {
63
- if (conditions !== undefined && conditions[this.index] === false)
64
- return [false];
65
- return [conditions[this.index]];
66
- }
67
- return conditions;
68
- };
69
- DataPopulator.prototype.getVariablesConditions = function (variableId) {
70
- var conditions = this.variableConditionValue(variableId);
71
- if (conditions === undefined)
72
- return [true];
73
- if (this.id === undefined || this.index === undefined) {
74
- return conditions;
75
- }
76
- var variableRootParent = this.getVariableRootParent(variableId);
77
- var variableRootRelations = this.references.relations.options[variableRootParent];
78
- if (variableRootRelations.children.options.includes(this.id) ||
79
- variableRootRelations.dependants.includes(this.id) ||
80
- this.id === variableRootParent) {
81
- return [conditions[this.index]];
82
- }
83
- return conditions;
84
- };
85
- DataPopulator.prototype.populateVariables = function () {
86
- var _loop_1 = function (i) {
87
- var variableId = this_1.dataMap.v[i];
88
- var variableReference = this_1.references.variables[variableId];
89
- var values = this_1.getVariableValues(variableId);
90
- var parseNumber = function (v) {
91
- var result = parseFloat(v);
92
- return Number.isNaN(result) ? '' : result;
93
- };
94
- if (variableReference.type === 'number') {
95
- values = values.map(function (v) { return (typeof v === 'number' ? v : parseNumber(v)); });
96
- }
97
- this_1.data.v[variableId] = __spreadArrays(values);
98
- };
99
- var this_1 = this;
100
- for (var i = 0; i < this.dataMap.v.length; i += 1) {
101
- _loop_1(i);
102
- }
103
- };
104
- DataPopulator.prototype.getVariableValues = function (variableId) {
105
- var value = this.ovc.variables[variableId];
106
- if (this.id === undefined || this.index === undefined) {
107
- return value;
108
- }
109
- var variableRootParent = this.getVariableRootParent(variableId);
110
- var variableRootRelations = this.references.relations.options[variableRootParent];
111
- if (variableRootRelations.children.options.includes(this.id) || variableRootRelations.dependants.includes(this.id) || this.id === variableRootParent) {
112
- return [value[this.index]];
113
- }
114
- return this.cleanArrayFromNullValues(__spreadArrays(value));
115
- };
116
- DataPopulator.prototype.getVariableRootParent = function (variableId) {
117
- var variableParents = this.references.relations.variables[variableId].parents;
118
- return variableParents[variableParents.length - 1];
119
- };
120
- DataPopulator.prototype.cleanArrayFromNullValues = function (arr) {
121
- return arr.map(function (v) { return (v === null ? '' : v); });
122
- };
123
- DataPopulator.prototype.populateOptions = function () {
124
- for (var i = 0; i < this.dataMap.o.length; i += 1) {
125
- var optionId = this.dataMap.o[i];
126
- var inputs = this.getOptionInputs(optionId);
127
- this.data.o[optionId] = __spreadArrays(inputs);
128
- }
129
- };
130
- DataPopulator.prototype.getOptionInputs = function (optionId) {
131
- var inputs = this.ovc.options[optionId];
132
- if (this.id === undefined || this.index === undefined) {
133
- return inputs;
134
- }
135
- var optionRootParent = this.getOptionRootParent(optionId);
136
- var optionRootRelations = this.references.relations.options[optionRootParent];
137
- if (optionRootRelations.children.options.includes(this.id) || optionRootRelations.dependants.includes(this.id) || this.id === optionRootParent) {
138
- return [inputs[this.index]];
139
- }
140
- return inputs;
141
- };
142
- DataPopulator.prototype.getOptionRootParent = function (optionId) {
143
- var optionParents = this.references.relations.options[optionId].parents;
144
- return optionParents.length > 0 ? optionParents[optionParents.length - 1] : optionId;
145
- };
146
- DataPopulator.prototype.optionConditionValue = function (optionId) {
147
- var _this = this;
148
- var conditions = this.references.conditions.options[optionId];
149
- if (conditions === undefined)
150
- return undefined;
151
- var inputs = this.ovc.options[optionId];
152
- return inputs.map(function (input, index) {
153
- return _this.conditionsRunner.executeCondition(conditions, optionId, index, 'options');
154
- });
155
- };
156
- DataPopulator.prototype.variableConditionValue = function (variableId) {
157
- var _this = this;
158
- var conditions = this.references.conditions.variables[variableId];
159
- if (conditions === undefined)
160
- return undefined;
161
- var inputs = this.ovc.variables[variableId];
162
- var result = inputs.map(function (input, index) {
163
- return _this.conditionsRunner.executeCondition(conditions, variableId, index, 'variables');
164
- });
165
- return result;
166
- };
167
- DataPopulator.prototype.sectionConditionValue = function (sectionId) {
168
- var conditions = this.references.conditions.sections.main[sectionId];
169
- if (conditions === undefined)
170
- return undefined;
171
- return this.conditionsRunner.executeCondition(conditions, 0, 0, 'sections');
172
- };
173
- return DataPopulator;
174
- }());
175
- exports.default = DataPopulator;
@@ -1,21 +0,0 @@
1
- import OVC_TYPE from './ovc.type';
2
- import { Types } from '@legalplace/referencesparser';
3
- export interface OldOVC {
4
- o: {
5
- [k: string]: string | number | (string | number)[];
6
- };
7
- v: {
8
- [k: string]: string | number | (string | number)[];
9
- };
10
- c: {
11
- [k: string]: boolean | boolean[];
12
- };
13
- }
14
- declare class OvcConverter {
15
- convertToOptionsVariables(ovc: OldOVC, references: Types.ReferencesType): OVC_TYPE;
16
- isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC;
17
- isOptionsVariables(_obj: OVC_TYPE | OldOVC): boolean;
18
- private pushStaticChildren;
19
- }
20
- declare const _default: OvcConverter;
21
- export default _default;
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __spreadArrays = (this && this.__spreadArrays) || function () {
14
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
15
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
16
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
17
- r[k] = a[j];
18
- return r;
19
- };
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- var OvcConverter = (function () {
22
- function OvcConverter() {
23
- }
24
- OvcConverter.prototype.convertToOptionsVariables = function (ovc, references) {
25
- var _this = this;
26
- var inputs = {
27
- options: {},
28
- variables: {}
29
- };
30
- if (typeof ovc !== 'object')
31
- return inputs;
32
- if (typeof ovc.o === 'object') {
33
- Object.keys(ovc.o).forEach(function (id) {
34
- var currentOption = ovc.o[id];
35
- if (Array.isArray(currentOption)) {
36
- var occurences_1 = currentOption.length;
37
- inputs.options = __assign(__assign({}, inputs.options), _this.pushStaticChildren(inputs.options, id, occurences_1, references));
38
- currentOption.forEach(function (childId, index) {
39
- var sanitizedId = typeof childId === 'string' && childId.split('_').length > 0
40
- ? childId.split('_')[0]
41
- : childId;
42
- if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
43
- inputs.options[id] = new Array(occurences_1).fill(false);
44
- }
45
- if (typeof sanitizedId === 'string' &&
46
- sanitizedId.trim().length === 0) {
47
- return;
48
- }
49
- if (!Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)) {
50
- inputs.options[sanitizedId] = new Array(occurences_1).fill(false);
51
- }
52
- inputs.options = __assign(__assign({}, inputs.options), _this.pushStaticChildren(inputs.options, sanitizedId, occurences_1, references));
53
- inputs.options[sanitizedId][index] = true;
54
- });
55
- }
56
- else {
57
- if (typeof currentOption === 'string' &&
58
- currentOption.trim().length === 0)
59
- return;
60
- inputs.options[currentOption] = [true];
61
- }
62
- });
63
- }
64
- if (typeof ovc.c === 'object') {
65
- Object.keys(ovc.c).forEach(function (id) {
66
- var currentCheckbox = ovc.c[id];
67
- if (typeof id === 'string' && id.trim().length === 0)
68
- return;
69
- if (Array.isArray(currentCheckbox)) {
70
- inputs.options[id] = currentCheckbox;
71
- }
72
- else {
73
- inputs.options[id] = [currentCheckbox];
74
- }
75
- });
76
- }
77
- if (typeof ovc.v === 'object') {
78
- Object.keys(ovc.v).forEach(function (id) {
79
- var currentVariable = ovc.v[id];
80
- if (Array.isArray(currentVariable)) {
81
- inputs.variables[id] = currentVariable;
82
- var parentsTree = references.relations.variables[id].parents;
83
- var parentOption = inputs.options["" + parentsTree[parentsTree.length - 1]];
84
- if (Array.isArray(parentOption)) {
85
- var occurences = parentOption.length;
86
- if (currentVariable.length < occurences) {
87
- inputs.variables[id] = __spreadArrays(currentVariable, Array(occurences - currentVariable.length).fill(''));
88
- }
89
- }
90
- }
91
- else {
92
- inputs.variables[id] = [currentVariable];
93
- }
94
- });
95
- }
96
- return inputs;
97
- };
98
- OvcConverter.prototype.isOvc = function (_obj) {
99
- if (typeof _obj !== 'object' || _obj === null)
100
- return false;
101
- var ovcKeys = ['o', 'v', 'c'];
102
- var foundKeys = Object.keys(_obj)
103
- .map(function (key) { return (ovcKeys.includes(key) ? 1 : 0); })
104
- .reduce(function (a, b) { return a + b; }, 0);
105
- return foundKeys > 1;
106
- };
107
- OvcConverter.prototype.isOptionsVariables = function (_obj) {
108
- if (typeof _obj !== 'object' || _obj === null)
109
- return false;
110
- var optionsVariablesKeys = ['options', 'variables'];
111
- var foundKeys = Object.keys(_obj)
112
- .map(function (key) { return (optionsVariablesKeys.includes(key) ? 1 : 0); })
113
- .reduce(function (a, b) { return a + b; }, 0);
114
- return foundKeys === 2;
115
- };
116
- OvcConverter.prototype.pushStaticChildren = function (inputOptions, id, occurences, references) {
117
- var result = __assign({}, inputOptions);
118
- var intId = typeof id === 'number' ? id : parseInt(id, 10);
119
- var options = references.relations.options[intId].children.options;
120
- var staticOptions = options.filter(function (childId) {
121
- return references.options[childId].meta.type === 'static';
122
- });
123
- staticOptions.forEach(function (childId) {
124
- if (!Object.prototype.hasOwnProperty.call(result, childId)) {
125
- result[childId] = new Array(occurences).fill(false);
126
- }
127
- });
128
- return result;
129
- };
130
- return OvcConverter;
131
- }());
132
- exports.default = new OvcConverter();
@@ -1,9 +0,0 @@
1
- declare type OVC_TYPE = {
2
- options: {
3
- [key: string]: boolean[];
4
- };
5
- variables: {
6
- [key: string]: (number | string)[];
7
- };
8
- };
9
- export default OVC_TYPE;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,118 +0,0 @@
1
- /* eslint-disable max-len */
2
- import LpLogic from '@legalplace/lplogic'
3
- import { Types } from '@legalplace/referencesparser'
4
- import { ConditionV3 } from '@legalplace/models-v3-types'
5
- import DataPopulator, { ConditionDataMap } from './DataPopulator'
6
-
7
- type OVC_TYPE = {
8
- options: {
9
- [key: string]: boolean[]
10
- }
11
- variables: {
12
- [key: string]: (number | string)[]
13
- }
14
- }
15
-
16
- interface ConditionData {
17
- o: Record<string, boolean[]>
18
- v: Record<string, (string | number)[]>
19
- }
20
-
21
- const conditionsCache: {
22
- [key: string]: boolean
23
- } = {}
24
-
25
- class ConditionsRunner {
26
- private references: Types.ReferencesType
27
-
28
- private ovc: OVC_TYPE
29
-
30
- constructor(references: Types.ReferencesType, ovc: OVC_TYPE) {
31
- this.references = references
32
- this.ovc = ovc
33
- }
34
-
35
- isOptionRelatedToOption(optionA: number, optionB: number): boolean {
36
- // Getting B relations
37
- const OptionBParents = this.references.relations.options[optionB].parents
38
- const OptionBRoot = OptionBParents[OptionBParents.length - 1]
39
- const OptionBRootRelations = OptionBRoot ? this.references.relations.options[OptionBRoot] : undefined
40
-
41
- // Checking if B option is related to the option that changed
42
- if (OptionBRootRelations) {
43
- if (optionA === optionB || OptionBRootRelations.children.options.includes(optionA) || OptionBRootRelations.dependants.includes(optionA)) {
44
- return true
45
- }
46
- }
47
-
48
- return false
49
- }
50
-
51
- isVariableRelatedToOption(variableId: number, optionId: number): boolean {
52
- // Variable parent
53
- const variableParentId = this.references.relations.variables[variableId].parents[0]
54
-
55
- // Getting B relations
56
- const optionParents = this.references.relations.options[variableParentId].parents
57
- const optionRoot = optionParents[optionParents.length - 1]
58
- const optionRootRelations = optionRoot ? this.references.relations.options[optionRoot] : undefined
59
-
60
- // Checking if B option is related to the option that changed
61
- if (optionRootRelations) {
62
- if (optionRootRelations.children.options.includes(optionId) || optionRootRelations.dependants.includes(optionId)) {
63
- return true
64
- }
65
- }
66
-
67
- return false
68
- }
69
-
70
- isVariableRelatedToVariable(variableA: number, variableB: number): boolean {
71
- // Getting variables options
72
- const optionA = this.references.relations.variables[variableA].parents[0]
73
- const optionB = this.references.relations.variables[variableB].parents[0]
74
-
75
- // Getting B relations
76
- const OptionBParents = this.references.relations.options[optionB].parents
77
- const OptionBRoot = OptionBParents[OptionBParents.length - 1]
78
- const OptionBRootRelations = OptionBRoot ? this.references.relations.options[OptionBRoot] : undefined
79
-
80
- // Checking if B option is related to the option that changed
81
- if (OptionBRootRelations) {
82
- if (optionA === optionB || OptionBRootRelations.children.options.includes(optionA) || OptionBRootRelations.dependants.includes(optionA)) {
83
- return true
84
- }
85
- }
86
-
87
- return false
88
- }
89
-
90
- executeCondition(
91
- condition: {
92
- dataMap: ConditionDataMap
93
- conditions: ConditionV3
94
- },
95
- id: number,
96
- index: number,
97
- conditionType: 'options' | 'optionValidator' | 'variables' | 'prefillers' | 'variableValidator' | 'sections' | 'documents'
98
- ): any {
99
- // In case we have multiple conditions let's run them here
100
- if (Array.isArray(condition)) return condition.map(_condition => this.executeCondition(_condition, id, index, conditionType))
101
-
102
- // Populating Data
103
- let currentData
104
- if (conditionType === 'options' || conditionType === 'optionValidator') {
105
- currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, id, index).getData()
106
- } else if (conditionType === 'variables' || conditionType === 'prefillers' || conditionType === 'variableValidator') {
107
- const dataId = this.references.relations.variables[id].parents[0]
108
- currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, dataId, index).getData()
109
- } else if (conditionType === 'sections' || conditionType === 'documents') {
110
- currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, id, index).getData()
111
- }
112
-
113
- // Returning cache result
114
- return LpLogic(condition.conditions, currentData)
115
- }
116
- }
117
-
118
- export default ConditionsRunner