@legalplace/tagextractor 1.0.0 → 1.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.
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;
@@ -12,6 +12,11 @@ declare class TagsExtractor {
12
12
  private extractFromOptions;
13
13
  private extractFromVariables;
14
14
  private optionConditionValue;
15
- private variableConditionValue;
15
+ isVariableDisplayed(id: number, index: number): boolean;
16
+ isOptionDisplayed(id: number, index: number): boolean;
17
+ private getVariableCondition;
18
+ private getOptionCondition;
19
+ private getSectionCondition;
20
+ private getOptionParentSection;
16
21
  }
17
- export default TagsExtractor;
22
+ 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,7 +76,7 @@ var TagsExtractor = (function () {
70
76
  }
71
77
  });
72
78
  };
73
- TagsExtractor.prototype.optionConditionValue = function (optionId) {
79
+ TagExtractor.prototype.optionConditionValue = function (optionId) {
74
80
  var _this = this;
75
81
  var conditions = this.references.conditions.options[optionId];
76
82
  var inputs = this.inputs.options[optionId];
@@ -80,17 +86,50 @@ var TagsExtractor = (function () {
80
86
  return _this.conditionsRunner.executeCondition(conditions, optionId, index, 'options');
81
87
  });
82
88
  };
83
- TagsExtractor.prototype.variableConditionValue = function (variableId) {
89
+ TagExtractor.prototype.isVariableDisplayed = function (id, index) {
90
+ var _a;
91
+ var variableCondition = this.getVariableCondition(id, index);
92
+ var variableParents = ((_a = this.references.relations.variables[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
93
+ var parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index);
94
+ return [variableCondition, parentOptionIsDisplayed].filter(function (c) { return c !== true; }).length === 0;
95
+ };
96
+ TagExtractor.prototype.isOptionDisplayed = function (id, index) {
84
97
  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');
98
+ var _a;
99
+ var optionCondition = this.getOptionCondition(id, index);
100
+ var optionParents = ((_a = this.references.relations.options[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
101
+ var parentsConditions = optionParents.map(function (optionId) {
102
+ return _this.getOptionCondition(optionId, index) !== false;
103
+ });
104
+ var parentsInputs = optionParents.map(function (optionId) {
105
+ return _this.inputs.options[optionId][index];
91
106
  });
92
- return result;
107
+ var parentSectionId = this.getOptionParentSection(id);
108
+ var parentSectionCondition = this.getSectionCondition(parentSectionId) !== false;
109
+ return __spreadArrays([optionCondition, parentSectionCondition], parentsConditions, parentsInputs).filter(function (c) { return c !== true; }).length === 0;
110
+ };
111
+ TagExtractor.prototype.getVariableCondition = function (id, index) {
112
+ var conditionObject = this.references.conditions.variables[id];
113
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables');
114
+ };
115
+ TagExtractor.prototype.getOptionCondition = function (id, index) {
116
+ var conditionObject = this.references.conditions.options[id];
117
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options');
118
+ };
119
+ TagExtractor.prototype.getSectionCondition = function (id) {
120
+ var conditionObject = this.references.conditions.sections.main[id];
121
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections');
122
+ };
123
+ TagExtractor.prototype.getOptionParentSection = function (id) {
124
+ var parents = this.references.relations.options[id].parents;
125
+ var rootId = parents.length > 0 ? parents[parents.length - 1] : id;
126
+ var sections = Object.values(this.references.sections.main);
127
+ for (var i = 0; i < sections.length; i += 1) {
128
+ if (sections[i].options.includes(rootId))
129
+ return sections[i].id;
130
+ }
131
+ throw new Error("Cannot find parent section for option " + id + " (Root option id: " + rootId + ")");
93
132
  };
94
- return TagsExtractor;
133
+ return TagExtractor;
95
134
  }());
96
- exports.default = TagsExtractor;
135
+ 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,8 +51,7 @@ 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
  }
@@ -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.0",
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,34 +69,26 @@ 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]
82
-
83
- if(Array.isArray(tags) && tags.length > 0) {
72
+ const { tags, label } = this.references.variables[id]
84
73
 
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
93
  * Calculates option's condition
111
94
  */
@@ -115,7 +98,7 @@ class TagsExtractor {
115
98
 
116
99
  // Getting option inputs length
117
100
  const inputs = this.inputs.options[optionId]
118
-
101
+
119
102
  if (conditions === undefined) return inputs.map(() => true)
120
103
 
121
104
  return inputs.map((input, index) => {
@@ -124,23 +107,88 @@ class TagsExtractor {
124
107
  }
125
108
 
126
109
  /**
127
- * Calculates variable's condition
110
+ * Checks whether a variable is displayed at a given index
111
+ * @param id Variable's ID
112
+ * @param index Index
128
113
  */
129
- private variableConditionValue(variableId: number) {
130
- // Getting variable conditions
131
- const conditions = this.references.conditions.variables[variableId]
114
+ isVariableDisplayed(id: number, index: number) {
115
+ // Getting variable's conditions & executing it if any
116
+ const variableCondition = this.getVariableCondition(id, index)
117
+ const variableParents = this.references.relations.variables[id]?.parents || []
118
+ const parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index)
132
119
 
133
- // Getting variable inputs length
134
- const inputs = this.inputs.variables[variableId]
135
-
136
- if (conditions === undefined) return inputs.map(() => true)
120
+ return [variableCondition, parentOptionIsDisplayed].filter(c => c !== true).length === 0
121
+ }
137
122
 
138
- const result = inputs.map((input, index) => {
139
- return this.conditionsRunner.executeCondition(conditions, variableId, index, 'variables')
123
+ /**
124
+ * Checks whether an option is displayed at a given index
125
+ * @param id Option's ID
126
+ * @param index Index
127
+ */
128
+ isOptionDisplayed(id: number, index: number) {
129
+ // Getting variable's conditions & executing it if any
130
+ const optionCondition = this.getOptionCondition(id, index)
131
+ const optionParents = this.references.relations.options[id]?.parents || []
132
+ const parentsConditions = optionParents.map(optionId => {
133
+ return this.getOptionCondition(optionId, index) !== false
140
134
  })
135
+ const parentsInputs = optionParents.map(optionId => {
136
+ return this.inputs.options[optionId][index]
137
+ })
138
+ const parentSectionId = this.getOptionParentSection(id)
139
+ const parentSectionCondition = this.getSectionCondition(parentSectionId) !== false
140
+
141
+ return [optionCondition, parentSectionCondition, ...parentsConditions, ...parentsInputs].filter(c => c !== true).length === 0
142
+ }
143
+
144
+ /**
145
+ * Returns a variable's conditions
146
+ * @param id Variable's id
147
+ * @param index Variable's index
148
+ */
149
+ private getVariableCondition(id: number, index: number) {
150
+ const conditionObject = this.references.conditions.variables[id]
151
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables')
152
+ }
153
+
154
+ /**
155
+ * Returns an option's conditions
156
+ * @param id Option's id
157
+ * @param index Option's index
158
+ */
159
+ private getOptionCondition(id: number, index: number) {
160
+ const conditionObject = this.references.conditions.options[id]
161
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options')
162
+ }
163
+
164
+ /**
165
+ * Returns a section's conditions
166
+ * @param id Section's id
167
+ */
168
+ private getSectionCondition(id: number) {
169
+ const conditionObject = this.references.conditions.sections.main[id]
170
+ return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections')
171
+ }
172
+
173
+ /**
174
+ * Returns option's parent section
175
+ * @param id Option's id
176
+ */
177
+ private getOptionParentSection(id: number) {
178
+ const { parents } = this.references.relations.options[id]
179
+
180
+ // Getting root option id
181
+ const rootId = parents.length > 0 ? parents[parents.length - 1] : id
182
+
183
+ // Looking for section
184
+ const sections = Object.values(this.references.sections.main)
185
+
186
+ for (let i = 0; i < sections.length; i += 1) {
187
+ if (sections[i].options.includes(rootId)) return sections[i].id
188
+ }
141
189
 
142
- return result
190
+ throw new Error(`Cannot find parent section for option ${id} (Root option id: ${rootId})`)
143
191
  }
144
192
  }
145
193
 
146
- export default TagsExtractor
194
+ export default TagExtractor
@@ -1,131 +1,101 @@
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
 
87
61
  // Reading checkboxes
88
62
  if (typeof ovc.c === 'object') {
89
63
  Object.keys(ovc.c).forEach(id => {
90
- const currentCheckbox = ovc.c[id];
91
- if (typeof id === 'string' && id.trim().length === 0) return;
64
+ const currentCheckbox = ovc.c[id]
65
+ if (typeof id === 'string' && id.trim().length === 0) return
92
66
  // If it's a multiple
93
67
  if (Array.isArray(currentCheckbox)) {
94
- inputs.options[id] = currentCheckbox;
68
+ inputs.options[id] = currentCheckbox
95
69
  } else {
96
- inputs.options[id] = [currentCheckbox];
70
+ inputs.options[id] = [currentCheckbox]
97
71
  }
98
- });
72
+ })
99
73
  }
100
74
 
101
75
  // Reading variables
102
76
  if (typeof ovc.v === 'object') {
103
77
  Object.keys(ovc.v).forEach(id => {
104
- const currentVariable = ovc.v[id];
78
+ const currentVariable = ovc.v[id]
105
79
 
106
80
  // If it's a multiple
107
81
  if (Array.isArray(currentVariable)) {
108
- inputs.variables[id] = currentVariable;
82
+ inputs.variables[id] = currentVariable
109
83
  // Getting parentsTree & parent id
110
- const parentsTree = references.relations.variables[id].parents;
111
- const parentOption =
112
- inputs.options[`${parentsTree[parentsTree.length - 1]}`];
84
+ const parentsTree = references.relations.variables[id].parents
85
+ const parentOption = inputs.options[`${parentsTree[parentsTree.length - 1]}`]
113
86
  if (Array.isArray(parentOption)) {
114
- const occurences = parentOption.length;
87
+ const occurences = parentOption.length
115
88
  if (currentVariable.length < occurences) {
116
- inputs.variables[id] = [
117
- ...currentVariable,
118
- ...Array(occurences - currentVariable.length).fill('')
119
- ];
89
+ inputs.variables[id] = [...currentVariable, ...Array(occurences - currentVariable.length).fill('')]
120
90
  }
121
91
  }
122
92
  } else {
123
- inputs.variables[id] = [currentVariable];
93
+ inputs.variables[id] = [currentVariable]
124
94
  }
125
- });
95
+ })
126
96
  }
127
97
 
128
- return inputs;
98
+ return inputs
129
99
  }
130
100
 
131
101
  public isOvc(_obj: InputsType | OvcType): _obj is OvcType {
@@ -146,31 +116,26 @@ if (typeof ovc !== 'object') return inputs;
146
116
  return foundKeys === 2
147
117
  }
148
118
 
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);
119
+ private pushStaticChildren(inputOptions: InputsType['options'], id: number | string, occurences: number, references: Types.ReferencesType) {
120
+ const result = { ...inputOptions }
121
+ const intId = typeof id === 'number' ? id : parseInt(id, 10)
157
122
 
158
123
  // Getting children options and variables
159
- const { options } = references.relations.options[intId].children;
124
+ const { options } = references.relations.options[intId].children
160
125
 
161
126
  // Getting static children
162
127
  const staticOptions = options.filter(childId => {
163
- return references.options[childId].meta.type === 'static';
164
- });
128
+ return references.options[childId].meta.type === 'static'
129
+ })
165
130
 
166
131
  staticOptions.forEach(childId => {
167
132
  // Creating input if it doesn't exist
168
133
  if (!Object.prototype.hasOwnProperty.call(result, childId)) {
169
- result[childId] = new Array(occurences).fill(false);
134
+ result[childId] = new Array(occurences).fill(false)
170
135
  }
171
- });
136
+ })
172
137
 
173
- return result;
138
+ return result
174
139
  }
175
140
  }
176
141
 
@@ -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))