@legalplace/tagextractor 1.3.0-alpha → 2.0.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,218 +0,0 @@
1
- import { OptionV3 } from '@legalplace/models-v3-types'
2
- import { Types } from '@legalplace/referencesparser'
3
- import InputsType from '../types/inputs.type'
4
- import OvcType from '../types/ovc.type'
5
-
6
- class OvcConverter {
7
- public convertToOptionsVariables(ovc: OvcType, references: Types.ReferencesType) {
8
- const inputs: InputsType = {
9
- options: {},
10
- variables: {}
11
- }
12
-
13
- if (typeof ovc !== 'object') return inputs
14
-
15
- // Reading options
16
- if (typeof ovc.o === 'object') {
17
- Object.keys(ovc.o).forEach(id => {
18
- const currentOption = ovc.o[id]
19
-
20
- // If it's a multiple
21
- if (Array.isArray(currentOption)) {
22
- const occurences = currentOption.length
23
- // Adding static childrens
24
- inputs.options = {
25
- ...inputs.options,
26
- ...this.pushStaticChildren(inputs.options, id, occurences, references)
27
- }
28
- currentOption.forEach((childId, index) => {
29
- const sanitizedId = typeof childId === 'string' && childId.split('_').length > 0 ? childId.split('_')[0] : childId
30
-
31
- // Making sure the input exist
32
- if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
33
- const defaultValue = references.options[id].meta.type === 'static'
34
- inputs.options[id] = new Array(occurences).fill(defaultValue)
35
- }
36
-
37
- // Making sure the childId is not empty
38
- if (typeof sanitizedId === 'string' && sanitizedId.trim().length === 0) {
39
- return
40
- }
41
-
42
- // Creating input if it doesn't exist
43
- if (!Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)) {
44
- const defaultValue = references.options[sanitizedId].meta.type === 'static'
45
- inputs.options[sanitizedId] = new Array(occurences).fill(defaultValue)
46
- }
47
-
48
- // Adding static childrens
49
- inputs.options = {
50
- ...inputs.options,
51
- ...this.pushStaticChildren(inputs.options, sanitizedId, occurences, references)
52
- }
53
-
54
- // Setting current index to true
55
- inputs.options[sanitizedId][index] = true
56
- })
57
- } else {
58
- if (typeof currentOption === 'string' && currentOption.trim().length === 0) return
59
- inputs.options[currentOption] = [true]
60
- }
61
- })
62
- }
63
-
64
- // Adding static root options
65
- Object.keys(references.options).forEach(optionId => {
66
- const option = references.options[optionId]
67
- const { parents } = references.relations.options[optionId]
68
-
69
- if (inputs.options[optionId] === undefined && parents.length === 0 && option.meta.type === 'static') {
70
- inputs.options[optionId] = [true]
71
- }
72
- })
73
-
74
- // Reading checkboxes
75
- if (typeof ovc.c === 'object') {
76
- Object.keys(ovc.c).forEach(id => {
77
- const currentCheckbox = ovc.c[id]
78
- if (typeof id === 'string' && id.trim().length === 0) return
79
- // If it's a multiple
80
- if (Array.isArray(currentCheckbox)) {
81
- inputs.options[id] = currentCheckbox
82
- } else {
83
- inputs.options[id] = [currentCheckbox]
84
- }
85
- })
86
- }
87
-
88
- // Reading variables
89
- if (typeof ovc.v === 'object') {
90
- Object.keys(ovc.v).forEach(id => {
91
- const currentVariable = ovc.v[id]
92
-
93
- // If it's a multiple
94
- if (Array.isArray(currentVariable)) {
95
- inputs.variables[id] = currentVariable
96
- // Getting parentsTree & parent id
97
- const parentsTree = references.relations.variables[id].parents
98
- const parentOption = inputs.options[`${parentsTree[parentsTree.length - 1]}`]
99
- if (Array.isArray(parentOption)) {
100
- const occurences = parentOption.length
101
- if (currentVariable.length < occurences) {
102
- inputs.variables[id] = [...currentVariable, ...Array(occurences - currentVariable.length).fill('')]
103
- }
104
- }
105
- } else {
106
- inputs.variables[id] = [currentVariable]
107
- }
108
- })
109
- }
110
-
111
- // Pushing left over and returning inputs
112
- return this.pushLeftOver(inputs, references)
113
- }
114
-
115
- public isOvc(_obj: InputsType | OvcType): _obj is OvcType {
116
- if (typeof _obj !== 'object' || _obj === null) return false
117
- const ovcKeys = ['o', 'v', 'c']
118
- const foundKeys = Object.keys(_obj)
119
- .map((key): number => (ovcKeys.includes(key) ? 1 : 0))
120
- .reduce((a, b) => a + b, 0)
121
-
122
- return foundKeys > 1
123
- }
124
-
125
- public isOptionsVariables(_obj: InputsType | OvcType): _obj is InputsType {
126
- if (typeof _obj !== 'object' || _obj === null) return false
127
- const optionsVariablesKeys = ['options', 'variables']
128
- const foundKeys = Object.keys(_obj)
129
- .map((key): number => (optionsVariablesKeys.includes(key) ? 1 : 0))
130
- .reduce((a, b) => a + b, 0)
131
- return foundKeys === 2
132
- }
133
-
134
- private pushStaticChildren(inputOptions: InputsType['options'], id: number | string, occurences: number, references: Types.ReferencesType) {
135
- const result = { ...inputOptions }
136
- const intId = typeof id === 'number' ? id : parseInt(id, 10)
137
-
138
- // Getting children options and variables
139
- const { options } = references.relations.options[intId].children
140
-
141
- // Getting static children
142
- const staticOptions = options.filter(childId => {
143
- return references.options[childId].meta.type === 'static'
144
- })
145
-
146
- staticOptions.forEach(childId => {
147
- // Creating input if it doesn't exist
148
- if (!Object.prototype.hasOwnProperty.call(result, childId)) {
149
- result[childId] = new Array(occurences).fill(true)
150
- }
151
- })
152
-
153
- return result
154
- }
155
-
156
- private pushLeftOver(inputs: InputsType, references: Types.ReferencesType): InputsType {
157
- let result = {
158
- ...inputs
159
- }
160
-
161
- Object.values(references.sections).forEach(document => {
162
- Object.values(document).forEach(section => {
163
- section.options.forEach(optionId => {
164
- const occurences = inputs.options[optionId] === undefined ? 1 : inputs.options[optionId].length
165
- result = this.pushLeftOverOption(result, references, references.options[optionId], occurences)
166
- })
167
- })
168
- })
169
-
170
- return result
171
- }
172
-
173
- private pushLeftOverOption(inputs: InputsType, references: Types.ReferencesType, option: OptionV3, occurences: number): InputsType {
174
- let result = {
175
- ...inputs
176
- }
177
-
178
- const defaultValue = !['radio', 'checkbox'].includes(option.meta.type)
179
-
180
- // Adding option if it doesn't exist
181
- if (!Object.prototype.hasOwnProperty.call(result.options, option.meta.id)) {
182
- result = {
183
- ...result,
184
- options: {
185
- ...result.options,
186
- [option.meta.id]: new Array(occurences).fill(defaultValue)
187
- }
188
- }
189
- }
190
-
191
- // Reading option's children
192
- option.options.forEach(optionId => {
193
- result = this.pushLeftOverOption(result, references, references.options[optionId], occurences)
194
- })
195
-
196
- // Reading option variables
197
- option.variables.forEach(variableId => {
198
- result = this.pushLeftOverVariable(result, variableId, occurences)
199
- })
200
-
201
- return result
202
- }
203
-
204
- private pushLeftOverVariable(inputs: InputsType, id: number, occurences: number): InputsType {
205
- if (!Object.prototype.hasOwnProperty.call(inputs.variables, id)) {
206
- return {
207
- ...inputs,
208
- variables: {
209
- ...inputs.variables,
210
- [id]: new Array(occurences).fill('')
211
- }
212
- }
213
- }
214
- return inputs
215
- }
216
- }
217
-
218
- export default new OvcConverter()
@@ -1,10 +0,0 @@
1
- type InputsType = {
2
- options: {
3
- [key: string]: boolean[]
4
- }
5
- variables: {
6
- [key: string]: (number | string)[]
7
- }
8
- }
9
-
10
- export default InputsType
@@ -1,13 +0,0 @@
1
- interface OvcType {
2
- o: {
3
- [k: string]: string | number | (string | number)[]
4
- }
5
- v: {
6
- [k: string]: string | number | (string | number)[]
7
- }
8
- c: {
9
- [k: string]: boolean | boolean[]
10
- }
11
- }
12
-
13
- export default OvcType