@legalplace/tagextractor 1.0.0 → 1.1.1

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/.eslintrc CHANGED
@@ -38,7 +38,14 @@
38
38
  "import/prefer-default-export": "warn",
39
39
  "camelcase": "error",
40
40
  "no-undef": 0,
41
- "no-dupe-class-members": "off"
41
+ "no-dupe-class-members": "off",
42
+ "import/extensions": ["error", "ignorePackages", {
43
+ "js": "never",
44
+ "jsx": "never",
45
+ "ts": "never",
46
+ "tsx": "never",
47
+ "mjs": "never"
48
+ }]
42
49
  },
43
50
 
44
51
 
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import TagsExtractor from './libs/Extractor';
2
- export default TagsExtractor;
1
+ import TagExtractor from './libs/Extractor';
2
+ export default TagExtractor;
@@ -57,9 +57,7 @@ var DataPopulator = (function () {
57
57
  }
58
58
  var optionRootParent = this.getOptionRootParent(optionId);
59
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) {
60
+ if (optionRootRelations.children.options.includes(this.id) || optionRootRelations.dependants.includes(this.id) || this.id === optionRootParent) {
63
61
  if (conditions !== undefined && conditions[this.index] === false)
64
62
  return [false];
65
63
  return [conditions[this.index]];
@@ -75,9 +73,7 @@ var DataPopulator = (function () {
75
73
  }
76
74
  var variableRootParent = this.getVariableRootParent(variableId);
77
75
  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) {
76
+ if (variableRootRelations.children.options.includes(this.id) || variableRootRelations.dependants.includes(this.id) || this.id === variableRootParent) {
81
77
  return [conditions[this.index]];
82
78
  }
83
79
  return conditions;
@@ -1,8 +1,8 @@
1
- import { ModelV3 } from "@legalplace/models-v3-types";
2
- import InputsType from "../types/inputs.type";
3
- import OvcType from "../types/ovc.type";
4
- import TagsType from "../types/tags.type";
5
- declare class TagsExtractor {
1
+ import { ModelV3 } from '@legalplace/models-v3-types';
2
+ import InputsType from '../types/inputs.type';
3
+ import OvcType from '../types/ovc.type';
4
+ import TagsType from '../types/tags.type';
5
+ declare class TagExtractor {
6
6
  private references;
7
7
  private inputs;
8
8
  private conditionsRunner;
@@ -11,7 +11,11 @@ declare class TagsExtractor {
11
11
  get getTags(): TagsType;
12
12
  private extractFromOptions;
13
13
  private extractFromVariables;
14
- private optionConditionValue;
15
- private variableConditionValue;
14
+ isVariableDisplayed(id: number, index: number): boolean;
15
+ isOptionDisplayed(id: number, index: number): boolean;
16
+ private getVariableCondition;
17
+ private getOptionCondition;
18
+ private getSectionCondition;
19
+ private getOptionParentSection;
16
20
  }
17
- export default TagsExtractor;
21
+ export default TagExtractor;
@@ -1,4 +1,11 @@
1
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
+ };
2
9
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
10
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
11
  };
@@ -6,8 +13,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
13
  var referencesparser_1 = require("@legalplace/referencesparser");
7
14
  var ConditionsRunner_1 = __importDefault(require("./ConditionsRunner"));
8
15
  var OvcConverter_1 = __importDefault(require("./OvcConverter"));
9
- var TagsExtractor = (function () {
10
- function TagsExtractor(model, ovc) {
16
+ var TagExtractor = (function () {
17
+ function TagExtractor(model, ovc) {
11
18
  this.tags = {
12
19
  options: {},
13
20
  variables: {}
@@ -18,29 +25,28 @@ var TagsExtractor = (function () {
18
25
  this.extractFromOptions();
19
26
  this.extractFromVariables();
20
27
  }
21
- Object.defineProperty(TagsExtractor.prototype, "getTags", {
28
+ Object.defineProperty(TagExtractor.prototype, "getTags", {
22
29
  get: function () {
23
30
  return this.tags;
24
31
  },
25
32
  enumerable: false,
26
33
  configurable: true
27
34
  });
28
- TagsExtractor.prototype.extractFromOptions = function () {
35
+ TagExtractor.prototype.extractFromOptions = function () {
29
36
  var _this = this;
30
37
  Object.keys(this.references.options).forEach(function (id) {
31
38
  var _a = _this.references.options[id].meta, tags = _a.tags, label = _a.label;
32
39
  if (Array.isArray(tags) && tags.length > 0) {
33
- var conditions_1 = _this.optionConditionValue(parseInt(id, 10));
34
40
  _this.inputs.options[id].forEach(function (v, index) {
35
41
  tags.forEach(function (tag) {
36
42
  if (_this.tags.options[tag] === undefined)
37
43
  _this.tags.options[tag] = [];
38
- console.log(conditions_1[index], conditions_1);
39
44
  _this.tags.options[tag].push({
40
45
  id: parseInt(id, 10),
41
46
  index: index,
42
47
  label: label,
43
- condition: conditions_1[index],
48
+ condition: _this.getOptionCondition(parseInt(id, 10), index),
49
+ visible: _this.isOptionDisplayed(parseInt(id, 10), index),
44
50
  value: _this.inputs.options[id][index]
45
51
  });
46
52
  });
@@ -48,12 +54,11 @@ var TagsExtractor = (function () {
48
54
  }
49
55
  });
50
56
  };
51
- TagsExtractor.prototype.extractFromVariables = function () {
57
+ TagExtractor.prototype.extractFromVariables = function () {
52
58
  var _this = this;
53
59
  Object.keys(this.references.variables).forEach(function (id) {
54
60
  var _a = _this.references.variables[id], tags = _a.tags, label = _a.label;
55
61
  if (Array.isArray(tags) && tags.length > 0) {
56
- var conditions_2 = _this.variableConditionValue(parseInt(id, 10));
57
62
  _this.inputs.variables[id].forEach(function (v, index) {
58
63
  tags.forEach(function (tag) {
59
64
  if (_this.tags.variables[tag] === undefined)
@@ -62,7 +67,8 @@ var TagsExtractor = (function () {
62
67
  id: parseInt(id, 10),
63
68
  index: index,
64
69
  label: label,
65
- condition: conditions_2[index],
70
+ condition: _this.getVariableCondition(parseInt(id, 10), index) !== false,
71
+ visible: _this.isVariableDisplayed(parseInt(id, 10), index),
66
72
  value: _this.inputs.variables[id][index]
67
73
  });
68
74
  });
@@ -70,27 +76,50 @@ var TagsExtractor = (function () {
70
76
  }
71
77
  });
72
78
  };
73
- TagsExtractor.prototype.optionConditionValue = function (optionId) {
74
- var _this = this;
75
- var conditions = this.references.conditions.options[optionId];
76
- var inputs = this.inputs.options[optionId];
77
- if (conditions === undefined)
78
- return inputs.map(function () { return true; });
79
- return inputs.map(function (input, index) {
80
- return _this.conditionsRunner.executeCondition(conditions, optionId, index, 'options');
81
- });
79
+ TagExtractor.prototype.isVariableDisplayed = function (id, index) {
80
+ var _a;
81
+ var variableCondition = this.getVariableCondition(id, index);
82
+ var variableParents = ((_a = this.references.relations.variables[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
83
+ var parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index);
84
+ return [variableCondition, parentOptionIsDisplayed].filter(function (c) { return c !== true; }).length === 0;
82
85
  };
83
- TagsExtractor.prototype.variableConditionValue = function (variableId) {
86
+ TagExtractor.prototype.isOptionDisplayed = function (id, index) {
84
87
  var _this = this;
85
- var conditions = this.references.conditions.variables[variableId];
86
- var inputs = this.inputs.variables[variableId];
87
- if (conditions === undefined)
88
- return inputs.map(function () { return true; });
89
- var result = inputs.map(function (input, index) {
90
- return _this.conditionsRunner.executeCondition(conditions, variableId, index, 'variables');
88
+ var _a;
89
+ var optionCondition = this.getOptionCondition(id, index);
90
+ var optionParents = ((_a = this.references.relations.options[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
91
+ var parentsConditions = optionParents.map(function (optionId) {
92
+ return _this.getOptionCondition(optionId, index) !== false;
93
+ });
94
+ var parentsInputs = optionParents.map(function (optionId) {
95
+ return _this.inputs.options[optionId][index];
91
96
  });
92
- return result;
97
+ var parentSectionId = this.getOptionParentSection(id);
98
+ var parentSectionCondition = this.getSectionCondition(parentSectionId) !== false;
99
+ return __spreadArrays([optionCondition, parentSectionCondition], parentsConditions, parentsInputs).filter(function (c) { return c !== true; }).length === 0;
100
+ };
101
+ TagExtractor.prototype.getVariableCondition = function (id, index) {
102
+ var conditionObject = this.references.conditions.variables[id];
103
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables');
104
+ };
105
+ TagExtractor.prototype.getOptionCondition = function (id, index) {
106
+ var conditionObject = this.references.conditions.options[id];
107
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options');
108
+ };
109
+ TagExtractor.prototype.getSectionCondition = function (id) {
110
+ var conditionObject = this.references.conditions.sections.main[id];
111
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections');
112
+ };
113
+ TagExtractor.prototype.getOptionParentSection = function (id) {
114
+ var parents = this.references.relations.options[id].parents;
115
+ var rootId = parents.length > 0 ? parents[parents.length - 1] : id;
116
+ var sections = Object.values(this.references.sections.main);
117
+ for (var i = 0; i < sections.length; i += 1) {
118
+ if (sections[i].options.includes(rootId))
119
+ return sections[i].id;
120
+ }
121
+ throw new Error("Cannot find parent section for option " + id + " (Root option id: " + rootId + ")");
93
122
  };
94
- return TagsExtractor;
123
+ return TagExtractor;
95
124
  }());
96
- exports.default = TagsExtractor;
125
+ exports.default = TagExtractor;
@@ -36,14 +36,11 @@ var OvcConverter = (function () {
36
36
  var occurences_1 = currentOption.length;
37
37
  inputs.options = __assign(__assign({}, inputs.options), _this.pushStaticChildren(inputs.options, id, occurences_1, references));
38
38
  currentOption.forEach(function (childId, index) {
39
- var sanitizedId = typeof childId === 'string' && childId.split('_').length > 0
40
- ? childId.split('_')[0]
41
- : childId;
39
+ var sanitizedId = typeof childId === 'string' && childId.split('_').length > 0 ? childId.split('_')[0] : childId;
42
40
  if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
43
41
  inputs.options[id] = new Array(occurences_1).fill(false);
44
42
  }
45
- if (typeof sanitizedId === 'string' &&
46
- sanitizedId.trim().length === 0) {
43
+ if (typeof sanitizedId === 'string' && sanitizedId.trim().length === 0) {
47
44
  return;
48
45
  }
49
46
  if (!Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)) {
@@ -54,13 +51,19 @@ var OvcConverter = (function () {
54
51
  });
55
52
  }
56
53
  else {
57
- if (typeof currentOption === 'string' &&
58
- currentOption.trim().length === 0)
54
+ if (typeof currentOption === 'string' && currentOption.trim().length === 0)
59
55
  return;
60
56
  inputs.options[currentOption] = [true];
61
57
  }
62
58
  });
63
59
  }
60
+ Object.keys(references.options).forEach(function (optionId) {
61
+ var option = references.options[optionId];
62
+ var parents = references.relations.options[optionId].parents;
63
+ if (inputs.options[optionId] === undefined && parents.length === 0 && option.meta.type === 'static') {
64
+ inputs.options[optionId] = [true];
65
+ }
66
+ });
64
67
  if (typeof ovc.c === 'object') {
65
68
  Object.keys(ovc.c).forEach(function (id) {
66
69
  var currentCheckbox = ovc.c[id];
@@ -7,6 +7,7 @@ export interface OptionTagType {
7
7
  index: number;
8
8
  label: string;
9
9
  condition: boolean;
10
+ visible: boolean;
10
11
  value: boolean;
11
12
  }
12
13
  export interface VariableTagType {
@@ -14,6 +15,7 @@ export interface VariableTagType {
14
15
  index: number;
15
16
  label: string;
16
17
  condition: boolean;
18
+ visible: boolean;
17
19
  value: string | number;
18
20
  }
19
21
  export default TagsType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/tagextractor",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "TagExtractor",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://git.legalplace.eu/legalplace/tagextractor",
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- import TagsExtractor from './libs/Extractor'
1
+ import TagExtractor from './libs/Extractor'
2
2
 
3
- export default TagsExtractor;
3
+ export default TagExtractor
@@ -69,7 +69,7 @@ class DataPopulator {
69
69
  // Options
70
70
  for (let i = 0; i < this.dataMap.c.o.length; i += 1) {
71
71
  const optionId = this.dataMap.c.o[i]
72
- const optionCondition = [...this.getOptionConditions(optionId)];
72
+ const optionCondition = [...this.getOptionConditions(optionId)]
73
73
  this.data.c.o[optionId] = optionCondition === undefined ? [false] : optionCondition
74
74
  }
75
75
 
@@ -80,31 +80,27 @@ class DataPopulator {
80
80
  this.data.c.v[variableId] = variableCondition === undefined ? [false] : variableCondition
81
81
  }
82
82
  }
83
- /**
83
+
84
+ /**
84
85
  * Returns a give option's conditions
85
86
  * @param optionId Option's id
86
87
  */
87
88
  private getOptionConditions(optionId: number): Readonly<boolean[]> {
88
- const conditions = this.optionConditionValue(optionId);
89
- if (conditions === undefined) return [true];
89
+ const conditions = this.optionConditionValue(optionId)
90
+ if (conditions === undefined) return [true]
90
91
  if (this.id === undefined || this.index === undefined) {
91
- return conditions;
92
+ return conditions
92
93
  }
93
94
 
94
95
  // 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]];
96
+ const optionRootParent = this.getOptionRootParent(optionId)
97
+ const optionRootRelations = this.references.relations.options[optionRootParent]
98
+ if (optionRootRelations.children.options.includes(this.id) || optionRootRelations.dependants.includes(this.id) || this.id === optionRootParent) {
99
+ if (conditions !== undefined && conditions[this.index] === false) return [false]
100
+ return [conditions[this.index]]
105
101
  }
106
102
 
107
- return conditions;
103
+ return conditions
108
104
  }
109
105
 
110
106
  /**
@@ -112,24 +108,20 @@ class DataPopulator {
112
108
  * @param variableId Variable's id
113
109
  */
114
110
  private getVariablesConditions(variableId: number): Readonly<boolean[]> {
115
- const conditions = this.variableConditionValue(variableId);
116
- if (conditions === undefined) return [true];
111
+ const conditions = this.variableConditionValue(variableId)
112
+ if (conditions === undefined) return [true]
117
113
  if (this.id === undefined || this.index === undefined) {
118
- return conditions;
114
+ return conditions
119
115
  }
120
116
 
121
117
  // 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]];
118
+ const variableRootParent = this.getVariableRootParent(variableId)
119
+ const variableRootRelations = this.references.relations.options[variableRootParent]
120
+ if (variableRootRelations.children.options.includes(this.id) || variableRootRelations.dependants.includes(this.id) || this.id === variableRootParent) {
121
+ return [conditions[this.index]]
130
122
  }
131
123
 
132
- return conditions;
124
+ return conditions
133
125
  }
134
126
 
135
127
  /**
@@ -1,15 +1,15 @@
1
- import { ModelV3 } from "@legalplace/models-v3-types";
1
+ import { ModelV3 } from '@legalplace/models-v3-types'
2
2
  import { ReferencesParser, Types } from '@legalplace/referencesparser'
3
- import InputsType from "../types/inputs.type";
4
- import OvcType from "../types/ovc.type";
5
- import TagsType from "../types/tags.type";
6
- import ConditionsRunner from "./ConditionsRunner";
7
- import OvcConverter from "./OvcConverter";
3
+ import InputsType from '../types/inputs.type'
4
+ import OvcType from '../types/ovc.type'
5
+ import TagsType from '../types/tags.type'
6
+ import ConditionsRunner from './ConditionsRunner'
7
+ import OvcConverter from './OvcConverter'
8
8
 
9
- class TagsExtractor {
10
- private references: Types.ReferencesType;
9
+ class TagExtractor {
10
+ private references: Types.ReferencesType
11
11
 
12
- private inputs: InputsType;
12
+ private inputs: InputsType
13
13
 
14
14
  private conditionsRunner: ConditionsRunner
15
15
 
@@ -26,13 +26,13 @@ class TagsExtractor {
26
26
  this.inputs = OvcConverter.isOptionsVariables(ovc) ? ovc : OvcConverter.convertToOptionsVariables(ovc, this.references)
27
27
 
28
28
  // Initiaing Conditions runner
29
- this.conditionsRunner = new ConditionsRunner(this.references, this.inputs);
29
+ this.conditionsRunner = new ConditionsRunner(this.references, this.inputs)
30
30
 
31
31
  // Extracting tags from options
32
- this.extractFromOptions();
32
+ this.extractFromOptions()
33
33
 
34
34
  // Extracting tags from variables
35
- this.extractFromVariables();
35
+ this.extractFromVariables()
36
36
  }
37
37
 
38
38
  public get getTags() {
@@ -44,32 +44,23 @@ class TagsExtractor {
44
44
  */
45
45
  private extractFromOptions() {
46
46
  Object.keys(this.references.options).forEach(id => {
47
- const {tags, label} = this.references.options[id].meta
47
+ const { tags, label } = this.references.options[id].meta
48
48
 
49
- if(Array.isArray(tags) && tags.length > 0) {
50
-
51
- // Getting condition
52
- const conditions = this.optionConditionValue(parseInt(id, 10))
53
-
49
+ if (Array.isArray(tags) && tags.length > 0) {
54
50
  this.inputs.options[id].forEach((v, index) => {
55
-
56
- tags.forEach((tag) => {
57
- if(this.tags.options[tag] === undefined) this.tags.options[tag] = []
58
-
59
- console.log(conditions[index], conditions)
51
+ tags.forEach(tag => {
52
+ if (this.tags.options[tag] === undefined) this.tags.options[tag] = []
60
53
  this.tags.options[tag].push({
61
54
  id: parseInt(id, 10),
62
55
  index,
63
- label: label,
64
- condition: conditions[index],
56
+ label,
57
+ condition: this.getOptionCondition(parseInt(id, 10), index),
58
+ visible: this.isOptionDisplayed(parseInt(id, 10), index),
65
59
  value: this.inputs.options[id][index]
66
60
  })
67
61
  })
68
-
69
62
  })
70
-
71
63
  }
72
-
73
64
  })
74
65
  }
75
66
 
@@ -78,69 +69,109 @@ class TagsExtractor {
78
69
  */
79
70
  private extractFromVariables() {
80
71
  Object.keys(this.references.variables).forEach(id => {
81
- const {tags, label} = this.references.variables[id]
72
+ const { tags, label } = this.references.variables[id]
82
73
 
83
- if(Array.isArray(tags) && tags.length > 0) {
84
-
85
- // Getting condition
86
- const conditions = this.variableConditionValue(parseInt(id, 10))
87
-
74
+ if (Array.isArray(tags) && tags.length > 0) {
88
75
  this.inputs.variables[id].forEach((v, index) => {
89
-
90
- tags.forEach((tag) => {
91
- if(this.tags.variables[tag] === undefined) this.tags.variables[tag] = []
76
+ tags.forEach(tag => {
77
+ if (this.tags.variables[tag] === undefined) this.tags.variables[tag] = []
92
78
  this.tags.variables[tag].push({
93
79
  id: parseInt(id, 10),
94
80
  index,
95
- label: label,
96
- condition: conditions[index],
81
+ label,
82
+ condition: this.getVariableCondition(parseInt(id, 10), index) !== false,
83
+ visible: this.isVariableDisplayed(parseInt(id, 10), index),
97
84
  value: this.inputs.variables[id][index]
98
85
  })
99
86
  })
100
-
101
87
  })
102
-
103
88
  }
104
-
105
89
  })
106
90
  }
107
91
 
108
-
109
92
  /**
110
- * Calculates option's condition
93
+ * Checks whether a variable is displayed at a given index
94
+ * @param id Variable's ID
95
+ * @param index Index
111
96
  */
112
- private optionConditionValue(optionId: number) {
113
- // Getting option conditions
114
- const conditions = this.references.conditions.options[optionId]
97
+ isVariableDisplayed(id: number, index: number) {
98
+ // Getting variable's conditions & executing it if any
99
+ const variableCondition = this.getVariableCondition(id, index)
100
+ const variableParents = this.references.relations.variables[id]?.parents || []
101
+ const parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index)
115
102
 
116
- // Getting option inputs length
117
- const inputs = this.inputs.options[optionId]
118
-
119
- if (conditions === undefined) return inputs.map(() => true)
103
+ return [variableCondition, parentOptionIsDisplayed].filter(c => c !== true).length === 0
104
+ }
120
105
 
121
- return inputs.map((input, index) => {
122
- return this.conditionsRunner.executeCondition(conditions, optionId, index, 'options')
106
+ /**
107
+ * Checks whether an option is displayed at a given index
108
+ * @param id Option's ID
109
+ * @param index Index
110
+ */
111
+ isOptionDisplayed(id: number, index: number) {
112
+ // Getting variable's conditions & executing it if any
113
+ const optionCondition = this.getOptionCondition(id, index)
114
+ const optionParents = this.references.relations.options[id]?.parents || []
115
+ const parentsConditions = optionParents.map(optionId => {
116
+ return this.getOptionCondition(optionId, index) !== false
123
117
  })
118
+ const parentsInputs = optionParents.map(optionId => {
119
+ return this.inputs.options[optionId][index]
120
+ })
121
+ const parentSectionId = this.getOptionParentSection(id)
122
+ const parentSectionCondition = this.getSectionCondition(parentSectionId) !== false
123
+
124
+ return [optionCondition, parentSectionCondition, ...parentsConditions, ...parentsInputs].filter(c => c !== true).length === 0
124
125
  }
125
126
 
126
127
  /**
127
- * Calculates variable's condition
128
+ * Returns a variable's conditions
129
+ * @param id Variable's id
130
+ * @param index Variable's index
128
131
  */
129
- private variableConditionValue(variableId: number) {
130
- // Getting variable conditions
131
- const conditions = this.references.conditions.variables[variableId]
132
+ private getVariableCondition(id: number, index: number) {
133
+ const conditionObject = this.references.conditions.variables[id]
134
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables')
135
+ }
132
136
 
133
- // Getting variable inputs length
134
- const inputs = this.inputs.variables[variableId]
137
+ /**
138
+ * Returns an option's conditions
139
+ * @param id Option's id
140
+ * @param index Option's index
141
+ */
142
+ private getOptionCondition(id: number, index: number) {
143
+ const conditionObject = this.references.conditions.options[id]
144
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options')
145
+ }
135
146
 
136
- if (conditions === undefined) return inputs.map(() => true)
147
+ /**
148
+ * Returns a section's conditions
149
+ * @param id Section's id
150
+ */
151
+ private getSectionCondition(id: number) {
152
+ const conditionObject = this.references.conditions.sections.main[id]
153
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections')
154
+ }
137
155
 
138
- const result = inputs.map((input, index) => {
139
- return this.conditionsRunner.executeCondition(conditions, variableId, index, 'variables')
140
- })
156
+ /**
157
+ * Returns option's parent section
158
+ * @param id Option's id
159
+ */
160
+ private getOptionParentSection(id: number) {
161
+ const { parents } = this.references.relations.options[id]
162
+
163
+ // Getting root option id
164
+ const rootId = parents.length > 0 ? parents[parents.length - 1] : id
165
+
166
+ // Looking for section
167
+ const sections = Object.values(this.references.sections.main)
168
+
169
+ for (let i = 0; i < sections.length; i += 1) {
170
+ if (sections[i].options.includes(rootId)) return sections[i].id
171
+ }
141
172
 
142
- return result
173
+ throw new Error(`Cannot find parent section for option ${id} (Root option id: ${rootId})`)
143
174
  }
144
175
  }
145
176
 
146
- export default TagsExtractor
177
+ export default TagExtractor
@@ -1,131 +1,111 @@
1
1
  import { Types } from '@legalplace/referencesparser'
2
- import InputsType from '../types/inputs.type';
3
- import OvcType from '../types/ovc.type';
4
-
2
+ import InputsType from '../types/inputs.type'
3
+ import OvcType from '../types/ovc.type'
5
4
 
6
5
  class OvcConverter {
7
- public convertToOptionsVariables(
8
- ovc: OvcType,
9
- references: Types.ReferencesType
10
- ) {
6
+ public convertToOptionsVariables(ovc: OvcType, references: Types.ReferencesType) {
11
7
  const inputs: InputsType = {
12
8
  options: {},
13
9
  variables: {}
14
10
  }
15
11
 
16
- if (typeof ovc !== 'object') return inputs;
12
+ if (typeof ovc !== 'object') return inputs
17
13
 
18
14
  // Reading options
19
15
  if (typeof ovc.o === 'object') {
20
16
  Object.keys(ovc.o).forEach(id => {
21
- const currentOption = ovc.o[id];
17
+ const currentOption = ovc.o[id]
22
18
 
23
19
  // If it's a multiple
24
20
  if (Array.isArray(currentOption)) {
25
- const occurences = currentOption.length;
21
+ const occurences = currentOption.length
26
22
  // Adding static childrens
27
23
  inputs.options = {
28
24
  ...inputs.options,
29
- ...this.pushStaticChildren(
30
- inputs.options,
31
- id,
32
- occurences,
33
- references
34
- )
35
- };
25
+ ...this.pushStaticChildren(inputs.options, id, occurences, references)
26
+ }
36
27
  currentOption.forEach((childId, index) => {
37
- const sanitizedId =
38
- typeof childId === 'string' && childId.split('_').length > 0
39
- ? childId.split('_')[0]
40
- : childId;
28
+ const sanitizedId = typeof childId === 'string' && childId.split('_').length > 0 ? childId.split('_')[0] : childId
41
29
 
42
30
  // Making sure the input exist
43
31
  if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
44
- inputs.options[id] = new Array(occurences).fill(false);
32
+ inputs.options[id] = new Array(occurences).fill(false)
45
33
  }
46
34
 
47
35
  // Making sure the childId is not empty
48
- if (
49
- typeof sanitizedId === 'string' &&
50
- sanitizedId.trim().length === 0
51
- ) {
52
- return;
36
+ if (typeof sanitizedId === 'string' && sanitizedId.trim().length === 0) {
37
+ return
53
38
  }
54
39
 
55
40
  // Creating input if it doesn't exist
56
- if (
57
- !Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)
58
- ) {
59
- inputs.options[sanitizedId] = new Array(occurences).fill(false);
41
+ if (!Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)) {
42
+ inputs.options[sanitizedId] = new Array(occurences).fill(false)
60
43
  }
61
44
 
62
45
  // Adding static childrens
63
46
  inputs.options = {
64
47
  ...inputs.options,
65
- ...this.pushStaticChildren(
66
- inputs.options,
67
- sanitizedId,
68
- occurences,
69
- references
70
- )
71
- };
48
+ ...this.pushStaticChildren(inputs.options, sanitizedId, occurences, references)
49
+ }
72
50
 
73
51
  // Setting current index to true
74
- inputs.options[sanitizedId][index] = true;
75
- });
52
+ inputs.options[sanitizedId][index] = true
53
+ })
76
54
  } else {
77
- if (
78
- typeof currentOption === 'string' &&
79
- currentOption.trim().length === 0
80
- )
81
- return;
82
- inputs.options[currentOption] = [true];
55
+ if (typeof currentOption === 'string' && currentOption.trim().length === 0) return
56
+ inputs.options[currentOption] = [true]
83
57
  }
84
- });
58
+ })
85
59
  }
86
60
 
61
+ // Adding static root options
62
+ Object.keys(references.options).forEach(optionId => {
63
+ const option = references.options[optionId]
64
+ const { parents } = references.relations.options[optionId]
65
+
66
+ if (inputs.options[optionId] === undefined && parents.length === 0 && option.meta.type === 'static') {
67
+ inputs.options[optionId] = [true]
68
+ }
69
+ })
70
+
87
71
  // Reading checkboxes
88
72
  if (typeof ovc.c === 'object') {
89
73
  Object.keys(ovc.c).forEach(id => {
90
- const currentCheckbox = ovc.c[id];
91
- if (typeof id === 'string' && id.trim().length === 0) return;
74
+ const currentCheckbox = ovc.c[id]
75
+ if (typeof id === 'string' && id.trim().length === 0) return
92
76
  // If it's a multiple
93
77
  if (Array.isArray(currentCheckbox)) {
94
- inputs.options[id] = currentCheckbox;
78
+ inputs.options[id] = currentCheckbox
95
79
  } else {
96
- inputs.options[id] = [currentCheckbox];
80
+ inputs.options[id] = [currentCheckbox]
97
81
  }
98
- });
82
+ })
99
83
  }
100
84
 
101
85
  // Reading variables
102
86
  if (typeof ovc.v === 'object') {
103
87
  Object.keys(ovc.v).forEach(id => {
104
- const currentVariable = ovc.v[id];
88
+ const currentVariable = ovc.v[id]
105
89
 
106
90
  // If it's a multiple
107
91
  if (Array.isArray(currentVariable)) {
108
- inputs.variables[id] = currentVariable;
92
+ inputs.variables[id] = currentVariable
109
93
  // Getting parentsTree & parent id
110
- const parentsTree = references.relations.variables[id].parents;
111
- const parentOption =
112
- inputs.options[`${parentsTree[parentsTree.length - 1]}`];
94
+ const parentsTree = references.relations.variables[id].parents
95
+ const parentOption = inputs.options[`${parentsTree[parentsTree.length - 1]}`]
113
96
  if (Array.isArray(parentOption)) {
114
- const occurences = parentOption.length;
97
+ const occurences = parentOption.length
115
98
  if (currentVariable.length < occurences) {
116
- inputs.variables[id] = [
117
- ...currentVariable,
118
- ...Array(occurences - currentVariable.length).fill('')
119
- ];
99
+ inputs.variables[id] = [...currentVariable, ...Array(occurences - currentVariable.length).fill('')]
120
100
  }
121
101
  }
122
102
  } else {
123
- inputs.variables[id] = [currentVariable];
103
+ inputs.variables[id] = [currentVariable]
124
104
  }
125
- });
105
+ })
126
106
  }
127
107
 
128
- return inputs;
108
+ return inputs
129
109
  }
130
110
 
131
111
  public isOvc(_obj: InputsType | OvcType): _obj is OvcType {
@@ -134,6 +114,7 @@ if (typeof ovc !== 'object') return inputs;
134
114
  const foundKeys = Object.keys(_obj)
135
115
  .map((key): number => (ovcKeys.includes(key) ? 1 : 0))
136
116
  .reduce((a, b) => a + b, 0)
117
+
137
118
  return foundKeys > 1
138
119
  }
139
120
 
@@ -146,31 +127,26 @@ if (typeof ovc !== 'object') return inputs;
146
127
  return foundKeys === 2
147
128
  }
148
129
 
149
- private pushStaticChildren(
150
- inputOptions: InputsType['options'],
151
- id: number | string,
152
- occurences: number,
153
- references: Types.ReferencesType
154
- ) {
155
- const result = { ...inputOptions };
156
- const intId = typeof id === 'number' ? id : parseInt(id, 10);
130
+ private pushStaticChildren(inputOptions: InputsType['options'], id: number | string, occurences: number, references: Types.ReferencesType) {
131
+ const result = { ...inputOptions }
132
+ const intId = typeof id === 'number' ? id : parseInt(id, 10)
157
133
 
158
134
  // Getting children options and variables
159
- const { options } = references.relations.options[intId].children;
135
+ const { options } = references.relations.options[intId].children
160
136
 
161
137
  // Getting static children
162
138
  const staticOptions = options.filter(childId => {
163
- return references.options[childId].meta.type === 'static';
164
- });
139
+ return references.options[childId].meta.type === 'static'
140
+ })
165
141
 
166
142
  staticOptions.forEach(childId => {
167
143
  // Creating input if it doesn't exist
168
144
  if (!Object.prototype.hasOwnProperty.call(result, childId)) {
169
- result[childId] = new Array(occurences).fill(false);
145
+ result[childId] = new Array(occurences).fill(false)
170
146
  }
171
- });
147
+ })
172
148
 
173
- return result;
149
+ return result
174
150
  }
175
151
  }
176
152
 
@@ -10,4 +10,4 @@ interface OvcType {
10
10
  }
11
11
  }
12
12
 
13
- export default OvcType
13
+ export default OvcType
@@ -4,19 +4,21 @@ interface TagsType {
4
4
  }
5
5
 
6
6
  export interface OptionTagType {
7
- id: number;
8
- index: number;
9
- label: string;
10
- condition: boolean;
7
+ id: number
8
+ index: number
9
+ label: string
10
+ condition: boolean
11
+ visible: boolean
11
12
  value: boolean
12
13
  }
13
14
 
14
15
  export interface VariableTagType {
15
- id: number;
16
- index: number;
17
- label: string;
18
- condition: boolean;
16
+ id: number
17
+ index: number
18
+ label: string
19
+ condition: boolean
20
+ visible: boolean
19
21
  value: string | number
20
22
  }
21
23
 
22
- export default TagsType
24
+ export default TagsType
package/model.ts DELETED
@@ -1,84 +0,0 @@
1
- import { ModelV3 } from "@legalplace/models-v3-types"
2
-
3
- export const model: ModelV3 = {
4
- "documents": {
5
- "main": {
6
- "name": "main",
7
- "sections": [{
8
- "id": 1,
9
- "label": "Titre de la section",
10
- "options": [1, 2]
11
- }, {
12
- "id": 2,
13
- "label": "Titre de la section",
14
- "options": [3]
15
- }]
16
- }
17
- },
18
- "options": {
19
- "1": {
20
- "meta": {
21
- "type": "hidden",
22
- "label": "Text de la question",
23
- "output": "<p><span style=\"font-weight:bold;\">[var:1]</span><span style=\"font-weight:bold;\">  </span> \n <span style=\"font-weight:bold;\">[var:2]</span><span style=\"font-weight:bold;\"></span></p><p></p><p>[var:1]  [var:2]</p><p></p><p>blablabla</p><p>blablabla</p>",
24
- "step": "*",
25
- "id": 1
26
- },
27
- "variables": [],
28
- "options": []
29
- },
30
- "2": {
31
- "meta": {
32
- "type": "static",
33
- "id": 2,
34
- "label": "Question",
35
- "step": "*"
36
- },
37
- "options": [],
38
- "variables": [1, 2]
39
- },
40
- "3": {
41
- "meta": {
42
- "type": "checkbox",
43
- "id": 3,
44
- "step": "*",
45
- "label": "Case à cocher",
46
- "tags": ["test", "test2"]
47
- },
48
- "options": [],
49
- "variables": []
50
- }
51
- },
52
- "variables": {
53
- "1": {
54
- "type": "text",
55
- "id": 1,
56
- "label": "ChampA",
57
- "step": "*",
58
- "mandatory": false,
59
- "placeholder": "",
60
- "tags": ["Hello"]
61
- },
62
- "2": {
63
- "type": "text",
64
- "id": 2,
65
- "label": "ChampB",
66
- "step": "*",
67
- "mandatory": false,
68
- "placeholder": "",
69
- "tags": ["Hello"]
70
- }
71
- },
72
- "customization": {}
73
- }
74
-
75
- export const ovc = {
76
- "o": {},
77
- "v": {
78
- "1": "cccc",
79
- "2": "dddddd"
80
- },
81
- "c": {
82
- "3": false
83
- }
84
- }
package/test.ts DELETED
@@ -1,8 +0,0 @@
1
- import * as fs from 'fs'
2
- import { model, ovc} from './model'
3
- import TagsExtractor from './src'
4
-
5
-
6
- const tags = new TagsExtractor(model, ovc).getTags
7
-
8
- console.log(JSON.stringify(tags))