@legalplace/prerenderx 2.6.4 → 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,304 +0,0 @@
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
- v: {}
33
- }
34
- }
35
-
36
- constructor(conditionsRunner: ConditionsRunner, references: Types.ReferencesType, ovc: OVC_TYPE, dataMap: ConditionDataMap, id?: number, index?: number) {
37
- this.id = id
38
- this.ovc = ovc
39
- this.index = index
40
- this.dataMap = dataMap
41
- this.references = references
42
- this.conditionsRunner = conditionsRunner
43
-
44
- // Populating variables
45
- this.populateVariables()
46
-
47
- // Populating options
48
- this.populateOptions()
49
-
50
- // Populating conditions
51
- this.populateConditions()
52
- }
53
-
54
- /**
55
- * Returns resulting data object
56
- */
57
- public getData(): Readonly<ConditionData> {
58
- return this.data
59
- }
60
-
61
- private populateConditions(): void {
62
- // Sections
63
- for (let i = 0; i < this.dataMap.c.s.length; i += 1) {
64
- const sectionId = this.dataMap.c.s[i]
65
- const sectionCondition = this.sectionConditionValue(sectionId)
66
- this.data.c.s[sectionId] = [sectionCondition !== false]
67
- }
68
-
69
- // Options
70
- for (let i = 0; i < this.dataMap.c.o.length; i += 1) {
71
- const optionId = this.dataMap.c.o[i]
72
- const optionCondition = [...this.getOptionConditions(optionId)];
73
- this.data.c.o[optionId] = optionCondition === undefined ? [false] : optionCondition
74
- }
75
-
76
- // Variables
77
- for (let i = 0; i < this.dataMap.c.v.length; i += 1) {
78
- const variableId = this.dataMap.c.v[i]
79
- const variableCondition = [...this.getVariablesConditions(variableId)]
80
- this.data.c.v[variableId] = variableCondition === undefined ? [false] : variableCondition
81
- }
82
- }
83
- /**
84
- * Returns a give option's conditions
85
- * @param optionId Option's id
86
- */
87
- private getOptionConditions(optionId: number): Readonly<boolean[]> {
88
- const conditions = this.optionConditionValue(optionId);
89
- if (conditions === undefined) return [true];
90
- if (this.id === undefined || this.index === undefined) {
91
- return conditions;
92
- }
93
-
94
- // Getting variable parent option
95
- const optionRootParent = this.getOptionRootParent(optionId);
96
- const optionRootRelations = this.references.relations.options[optionRootParent];
97
- if (
98
- optionRootRelations.children.options.includes(this.id) ||
99
- optionRootRelations.dependants.includes(this.id) ||
100
- this.id === optionRootParent
101
- ) {
102
- if (conditions !== undefined && conditions[this.index] === false)
103
- return [false];
104
- return [conditions[this.index]];
105
- }
106
-
107
- return conditions;
108
- }
109
-
110
- /**
111
- * Returns a give variables conditions
112
- * @param variableId Variable's id
113
- */
114
- private getVariablesConditions(variableId: number): Readonly<boolean[]> {
115
- const conditions = this.variableConditionValue(variableId);
116
- if (conditions === undefined) return [true];
117
- if (this.id === undefined || this.index === undefined) {
118
- return conditions;
119
- }
120
-
121
- // Getting variable parent option
122
- const variableRootParent = this.getVariableRootParent(variableId);
123
- const variableRootRelations = this.references.relations.options[variableRootParent];
124
- if (
125
- variableRootRelations.children.options.includes(this.id) ||
126
- variableRootRelations.dependants.includes(this.id) ||
127
- this.id === variableRootParent
128
- ) {
129
- return [conditions[this.index]];
130
- }
131
-
132
- return conditions;
133
- }
134
-
135
- /**
136
- * Populates variables according to the current dataMap object
137
- */
138
- private populateVariables(): void {
139
- for (let i = 0; i < this.dataMap.v.length; i += 1) {
140
- const variableId = this.dataMap.v[i]
141
- const variableReference = this.references.variables[variableId]
142
- let values = this.getVariableValues(variableId)
143
-
144
- const parseNumber = (v: string) => {
145
- const result = parseFloat(v)
146
- return Number.isNaN(result) ? '' : result
147
- }
148
- // If variable is of type number
149
- if (variableReference.type === 'number') {
150
- values = values.map(v => (typeof v === 'number' ? v : parseNumber(v)))
151
- }
152
-
153
- // Adding variables
154
- this.data.v[variableId] = [...values]
155
- }
156
- }
157
-
158
- /**
159
- * Returns a give variables value
160
- * @param variableId Variable's id
161
- */
162
- private getVariableValues(variableId: number): Readonly<(string | number)[]> {
163
- const value = this.ovc.variables[variableId]
164
- if (this.id === undefined || this.index === undefined) {
165
- return value
166
- }
167
-
168
- // Getting variable parent option
169
- const variableRootParent = this.getVariableRootParent(variableId)
170
- const variableRootRelations = this.references.relations.options[variableRootParent]
171
- if (variableRootRelations.children.options.includes(this.id) || variableRootRelations.dependants.includes(this.id) || this.id === variableRootParent) {
172
- return [value[this.index]]
173
- }
174
-
175
- return this.cleanArrayFromNullValues([...value])
176
- }
177
-
178
- /**
179
- * Return a given variable root option relations
180
- * @param variableId Variable's id
181
- */
182
- private getVariableRootParent(variableId: number) {
183
- const variableParents = this.references.relations.variables[variableId].parents
184
- return variableParents[variableParents.length - 1]
185
- }
186
-
187
- /**
188
- * Cleans values array from any null value by replacing it
189
- * with an empty string
190
- * @param arr Values array
191
- */
192
- private cleanArrayFromNullValues(arr: (string | number | null)[]): (string | number)[] {
193
- return arr.map(v => (v === null ? '' : v))
194
- }
195
-
196
- /**
197
- * Populates options data object accodring to current dataMap object
198
- */
199
- populateOptions() {
200
- for (let i = 0; i < this.dataMap.o.length; i += 1) {
201
- const optionId = this.dataMap.o[i]
202
- const inputs = this.getOptionInputs(optionId)
203
-
204
- // Adding option
205
- this.data.o[optionId] = [...inputs]
206
- }
207
- }
208
-
209
- /**
210
- * Returns a give option's inputs
211
- * @param optionId Option's id
212
- */
213
- private getOptionInputs(optionId: number): Readonly<boolean[]> {
214
- const inputs = this.ovc.options[optionId]
215
- if (this.id === undefined || this.index === undefined) {
216
- return inputs
217
- }
218
-
219
- // Getting variable parent option
220
- const optionRootParent = this.getOptionRootParent(optionId)
221
- const optionRootRelations = this.references.relations.options[optionRootParent]
222
- if (optionRootRelations.children.options.includes(this.id) || optionRootRelations.dependants.includes(this.id) || this.id === optionRootParent) {
223
- return [inputs[this.index]]
224
- }
225
-
226
- return inputs
227
- }
228
-
229
- /**
230
- * Returns option's root parent
231
- * @param optionId Root parent
232
- */
233
- private getOptionRootParent(optionId: number) {
234
- const optionParents = this.references.relations.options[optionId].parents
235
- return optionParents.length > 0 ? optionParents[optionParents.length - 1] : optionId
236
- }
237
-
238
- /**
239
- * Calculates option's condition
240
- */
241
- private optionConditionValue(optionId: number) {
242
- // Getting option conditions
243
- const conditions = this.references.conditions.options[optionId]
244
- if (conditions === undefined) return undefined
245
-
246
- // Getting option inputs length
247
- const inputs = this.ovc.options[optionId]
248
-
249
- return inputs.map((input, index) => {
250
- return this.conditionsRunner.executeCondition(conditions, optionId, index, 'options')
251
- })
252
- }
253
-
254
- /**
255
- * Calculates variable's condition
256
- */
257
- private variableConditionValue(variableId: number) {
258
- // Getting variable conditions
259
- const conditions = this.references.conditions.variables[variableId]
260
- if (conditions === undefined) return undefined
261
-
262
- // Getting variable inputs length
263
- const inputs = this.ovc.variables[variableId]
264
-
265
- const result = inputs.map((input, index) => {
266
- return this.conditionsRunner.executeCondition(conditions, variableId, index, 'variables')
267
- })
268
-
269
- return result
270
- }
271
-
272
- /**
273
- * Calculates section's condition
274
- */
275
- private sectionConditionValue(sectionId: number) {
276
- // Getting section conditions
277
- const conditions = this.references.conditions.sections.main[sectionId]
278
- if (conditions === undefined) return undefined
279
-
280
- return this.conditionsRunner.executeCondition(conditions, 0, 0, 'sections')
281
- }
282
- }
283
-
284
- export interface ConditionDataMap {
285
- o: number[]
286
- v: number[]
287
- c: {
288
- o: number[]
289
- s: number[]
290
- v: number[]
291
- }
292
- }
293
-
294
- export interface ConditionData {
295
- o: Record<string, boolean[]>
296
- v: Record<string, (string | number)[]>
297
- c: {
298
- o: Record<string, boolean[]>
299
- s: Record<string, boolean[]>
300
- v: Record<string, boolean[]>
301
- }
302
- }
303
-
304
- export default DataPopulator
@@ -1,187 +0,0 @@
1
- import OVC_TYPE from './ovc.type'
2
- import { Types } from '@legalplace/referencesparser'
3
-
4
- export interface OldOVC {
5
- o: {
6
- [k: string]: string | number | (string | number)[]
7
- }
8
- v: {
9
- [k: string]: string | number | (string | number)[]
10
- }
11
- c: {
12
- [k: string]: boolean | boolean[]
13
- }
14
- }
15
-
16
- class OvcConverter {
17
- public convertToOptionsVariables(
18
- ovc: OldOVC,
19
- references: Types.ReferencesType
20
- ) {
21
- const inputs: OVC_TYPE = {
22
- options: {},
23
- variables: {}
24
- }
25
-
26
- if (typeof ovc !== 'object') return inputs;
27
-
28
- // Reading options
29
- if (typeof ovc.o === 'object') {
30
- Object.keys(ovc.o).forEach(id => {
31
- const currentOption = ovc.o[id];
32
-
33
- // If it's a multiple
34
- if (Array.isArray(currentOption)) {
35
- const occurences = currentOption.length;
36
- // Adding static childrens
37
- inputs.options = {
38
- ...inputs.options,
39
- ...this.pushStaticChildren(
40
- inputs.options,
41
- id,
42
- occurences,
43
- references
44
- )
45
- };
46
- currentOption.forEach((childId, index) => {
47
- const sanitizedId =
48
- typeof childId === 'string' && childId.split('_').length > 0
49
- ? childId.split('_')[0]
50
- : childId;
51
-
52
- // Making sure the input exist
53
- if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
54
- inputs.options[id] = new Array(occurences).fill(false);
55
- }
56
-
57
- // Making sure the childId is not empty
58
- if (
59
- typeof sanitizedId === 'string' &&
60
- sanitizedId.trim().length === 0
61
- ) {
62
- return;
63
- }
64
-
65
- // Creating input if it doesn't exist
66
- if (
67
- !Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)
68
- ) {
69
- inputs.options[sanitizedId] = new Array(occurences).fill(false);
70
- }
71
-
72
- // Adding static childrens
73
- inputs.options = {
74
- ...inputs.options,
75
- ...this.pushStaticChildren(
76
- inputs.options,
77
- sanitizedId,
78
- occurences,
79
- references
80
- )
81
- };
82
-
83
- // Setting current index to true
84
- inputs.options[sanitizedId][index] = true;
85
- });
86
- } else {
87
- if (
88
- typeof currentOption === 'string' &&
89
- currentOption.trim().length === 0
90
- )
91
- return;
92
- inputs.options[currentOption] = [true];
93
- }
94
- });
95
- }
96
-
97
- // Reading checkboxes
98
- if (typeof ovc.c === 'object') {
99
- Object.keys(ovc.c).forEach(id => {
100
- const currentCheckbox = ovc.c[id];
101
- if (typeof id === 'string' && id.trim().length === 0) return;
102
- // If it's a multiple
103
- if (Array.isArray(currentCheckbox)) {
104
- inputs.options[id] = currentCheckbox;
105
- } else {
106
- inputs.options[id] = [currentCheckbox];
107
- }
108
- });
109
- }
110
-
111
- // Reading variables
112
- if (typeof ovc.v === 'object') {
113
- Object.keys(ovc.v).forEach(id => {
114
- const currentVariable = ovc.v[id];
115
-
116
- // If it's a multiple
117
- if (Array.isArray(currentVariable)) {
118
- inputs.variables[id] = currentVariable;
119
- // Getting parentsTree & parent id
120
- const parentsTree = references.relations.variables[id].parents;
121
- const parentOption =
122
- inputs.options[`${parentsTree[parentsTree.length - 1]}`];
123
- if (Array.isArray(parentOption)) {
124
- const occurences = parentOption.length;
125
- if (currentVariable.length < occurences) {
126
- inputs.variables[id] = [
127
- ...currentVariable,
128
- ...Array(occurences - currentVariable.length).fill('')
129
- ];
130
- }
131
- }
132
- } else {
133
- inputs.variables[id] = [currentVariable];
134
- }
135
- });
136
- }
137
-
138
- return inputs;
139
- }
140
-
141
- public isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC {
142
- if (typeof _obj !== 'object' || _obj === null) return false
143
- const ovcKeys = ['o', 'v', 'c']
144
- const foundKeys = Object.keys(_obj)
145
- .map((key): number => (ovcKeys.includes(key) ? 1 : 0))
146
- .reduce((a, b) => a + b, 0)
147
- return foundKeys > 1
148
- }
149
-
150
- public isOptionsVariables(_obj: OVC_TYPE | OldOVC) {
151
- if (typeof _obj !== 'object' || _obj === null) return false
152
- const optionsVariablesKeys = ['options', 'variables']
153
- const foundKeys = Object.keys(_obj)
154
- .map((key): number => (optionsVariablesKeys.includes(key) ? 1 : 0))
155
- .reduce((a, b) => a + b, 0)
156
- return foundKeys === 2
157
- }
158
-
159
- private pushStaticChildren(
160
- inputOptions: OVC_TYPE['options'],
161
- id: number | string,
162
- occurences: number,
163
- references: Types.ReferencesType
164
- ) {
165
- const result = { ...inputOptions };
166
- const intId = typeof id === 'number' ? id : parseInt(id, 10);
167
-
168
- // Getting children options and variables
169
- const { options } = references.relations.options[intId].children;
170
-
171
- // Getting static children
172
- const staticOptions = options.filter(childId => {
173
- return references.options[childId].meta.type === 'static';
174
- });
175
-
176
- staticOptions.forEach(childId => {
177
- // Creating input if it doesn't exist
178
- if (!Object.prototype.hasOwnProperty.call(result, childId)) {
179
- result[childId] = new Array(occurences).fill(false);
180
- }
181
- });
182
-
183
- return result;
184
- }
185
- }
186
-
187
- export default new OvcConverter()
@@ -1,10 +0,0 @@
1
- type OVC_TYPE = {
2
- options: {
3
- [key: string]: boolean[]
4
- }
5
- variables: {
6
- [key: string]: (number | string)[]
7
- }
8
- }
9
-
10
- export default OVC_TYPE