@legalplace/tagextractor 1.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.
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
14
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
15
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
16
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
17
+ r[k] = a[j];
18
+ return r;
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ var OvcConverter = (function () {
22
+ function OvcConverter() {
23
+ }
24
+ OvcConverter.prototype.convertToOptionsVariables = function (ovc, references) {
25
+ var _this = this;
26
+ var inputs = {
27
+ options: {},
28
+ variables: {}
29
+ };
30
+ if (typeof ovc !== 'object')
31
+ return inputs;
32
+ if (typeof ovc.o === 'object') {
33
+ Object.keys(ovc.o).forEach(function (id) {
34
+ var currentOption = ovc.o[id];
35
+ if (Array.isArray(currentOption)) {
36
+ var occurences_1 = currentOption.length;
37
+ inputs.options = __assign(__assign({}, inputs.options), _this.pushStaticChildren(inputs.options, id, occurences_1, references));
38
+ currentOption.forEach(function (childId, index) {
39
+ var sanitizedId = typeof childId === 'string' && childId.split('_').length > 0
40
+ ? childId.split('_')[0]
41
+ : childId;
42
+ if (!Object.prototype.hasOwnProperty.call(inputs.options, id)) {
43
+ inputs.options[id] = new Array(occurences_1).fill(false);
44
+ }
45
+ if (typeof sanitizedId === 'string' &&
46
+ sanitizedId.trim().length === 0) {
47
+ return;
48
+ }
49
+ if (!Object.prototype.hasOwnProperty.call(inputs.options, sanitizedId)) {
50
+ inputs.options[sanitizedId] = new Array(occurences_1).fill(false);
51
+ }
52
+ inputs.options = __assign(__assign({}, inputs.options), _this.pushStaticChildren(inputs.options, sanitizedId, occurences_1, references));
53
+ inputs.options[sanitizedId][index] = true;
54
+ });
55
+ }
56
+ else {
57
+ if (typeof currentOption === 'string' &&
58
+ currentOption.trim().length === 0)
59
+ return;
60
+ inputs.options[currentOption] = [true];
61
+ }
62
+ });
63
+ }
64
+ if (typeof ovc.c === 'object') {
65
+ Object.keys(ovc.c).forEach(function (id) {
66
+ var currentCheckbox = ovc.c[id];
67
+ if (typeof id === 'string' && id.trim().length === 0)
68
+ return;
69
+ if (Array.isArray(currentCheckbox)) {
70
+ inputs.options[id] = currentCheckbox;
71
+ }
72
+ else {
73
+ inputs.options[id] = [currentCheckbox];
74
+ }
75
+ });
76
+ }
77
+ if (typeof ovc.v === 'object') {
78
+ Object.keys(ovc.v).forEach(function (id) {
79
+ var currentVariable = ovc.v[id];
80
+ if (Array.isArray(currentVariable)) {
81
+ inputs.variables[id] = currentVariable;
82
+ var parentsTree = references.relations.variables[id].parents;
83
+ var parentOption = inputs.options["" + parentsTree[parentsTree.length - 1]];
84
+ if (Array.isArray(parentOption)) {
85
+ var occurences = parentOption.length;
86
+ if (currentVariable.length < occurences) {
87
+ inputs.variables[id] = __spreadArrays(currentVariable, Array(occurences - currentVariable.length).fill(''));
88
+ }
89
+ }
90
+ }
91
+ else {
92
+ inputs.variables[id] = [currentVariable];
93
+ }
94
+ });
95
+ }
96
+ return inputs;
97
+ };
98
+ OvcConverter.prototype.isOvc = function (_obj) {
99
+ if (typeof _obj !== 'object' || _obj === null)
100
+ return false;
101
+ var ovcKeys = ['o', 'v', 'c'];
102
+ var foundKeys = Object.keys(_obj)
103
+ .map(function (key) { return (ovcKeys.includes(key) ? 1 : 0); })
104
+ .reduce(function (a, b) { return a + b; }, 0);
105
+ return foundKeys > 1;
106
+ };
107
+ OvcConverter.prototype.isOptionsVariables = function (_obj) {
108
+ if (typeof _obj !== 'object' || _obj === null)
109
+ return false;
110
+ var optionsVariablesKeys = ['options', 'variables'];
111
+ var foundKeys = Object.keys(_obj)
112
+ .map(function (key) { return (optionsVariablesKeys.includes(key) ? 1 : 0); })
113
+ .reduce(function (a, b) { return a + b; }, 0);
114
+ return foundKeys === 2;
115
+ };
116
+ OvcConverter.prototype.pushStaticChildren = function (inputOptions, id, occurences, references) {
117
+ var result = __assign({}, inputOptions);
118
+ var intId = typeof id === 'number' ? id : parseInt(id, 10);
119
+ var options = references.relations.options[intId].children.options;
120
+ var staticOptions = options.filter(function (childId) {
121
+ return references.options[childId].meta.type === 'static';
122
+ });
123
+ staticOptions.forEach(function (childId) {
124
+ if (!Object.prototype.hasOwnProperty.call(result, childId)) {
125
+ result[childId] = new Array(occurences).fill(false);
126
+ }
127
+ });
128
+ return result;
129
+ };
130
+ return OvcConverter;
131
+ }());
132
+ exports.default = new OvcConverter();
@@ -0,0 +1,9 @@
1
+ declare type InputsType = {
2
+ options: {
3
+ [key: string]: boolean[];
4
+ };
5
+ variables: {
6
+ [key: string]: (number | string)[];
7
+ };
8
+ };
9
+ export default InputsType;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
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
+ export default OvcType;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,19 @@
1
+ interface TagsType {
2
+ options: Record<string, OptionTagType[]>;
3
+ variables: Record<string, VariableTagType[]>;
4
+ }
5
+ export interface OptionTagType {
6
+ id: number;
7
+ index: number;
8
+ label: string;
9
+ condition: boolean;
10
+ value: boolean;
11
+ }
12
+ export interface VariableTagType {
13
+ id: number;
14
+ index: number;
15
+ label: string;
16
+ condition: boolean;
17
+ value: string | number;
18
+ }
19
+ export default TagsType;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/model.ts ADDED
@@ -0,0 +1,84 @@
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/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@legalplace/tagextractor",
3
+ "version": "1.0.0",
4
+ "description": "TagExtractor",
5
+ "main": "dist/index.js",
6
+ "repository": "https://git.legalplace.eu/legalplace/tagextractor",
7
+ "author": "Moncef Hammou <moncef@legalplace.fr> <moncef@legalplace.fr>",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "format": "prettier --single-quote --no-semi --trailing-comma none --write \"src/**/*.{js,ts,tsx}\"",
11
+ "lint": "tsc && eslint ./src/ --ext ts,js",
12
+ "lint:fix": "tsc && eslint 'src/**/*.{js,jsx,ts,tsx}' --fix",
13
+ "test": "jest",
14
+ "build": "tsc"
15
+ },
16
+ "dependencies": {
17
+ "@legalplace/lplogic": "^2.1.4",
18
+ "@legalplace/models-v3-types": "^3.4.47",
19
+ "@legalplace/referencesparser": "^1.5.0",
20
+ "crypto": "^1.0.1",
21
+ "typescript": "3.9.3"
22
+ },
23
+ "devDependencies": {
24
+ "@babel/core": "^7.7.2",
25
+ "@babel/preset-typescript": "^7.7.2",
26
+ "@types/jest": "^24.0.15",
27
+ "@types/node": "^12.12.6",
28
+ "@typescript-eslint/eslint-plugin": "^2.6.1",
29
+ "@typescript-eslint/parser": "^2.6.1",
30
+ "airbnb-standard": "^1.0.0",
31
+ "eslint": "^6.6.0",
32
+ "eslint-config-airbnb-base": "^14.0.0",
33
+ "eslint-config-prettier": "^6.5.0",
34
+ "eslint-plugin-import": "^2.18.2",
35
+ "eslint-plugin-prettier": "^3.1.1",
36
+ "jest": "^24.8.0",
37
+ "prettier": "1.19.1",
38
+ "redux-mock-store": "^1.5.3",
39
+ "ts-jest": "^24.0.2"
40
+ }
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import TagsExtractor from './libs/Extractor'
2
+
3
+ export default TagsExtractor;
@@ -0,0 +1,99 @@
1
+ /* eslint-disable max-len */
2
+ import LpLogic from '@legalplace/lplogic'
3
+ import { Types } from '@legalplace/referencesparser'
4
+ import { ConditionV3 } from '@legalplace/models-v3-types'
5
+ import DataPopulator, { ConditionDataMap } from './DataPopulator'
6
+ import InputsType from '../types/inputs.type'
7
+
8
+ class ConditionsRunner {
9
+ private references: Types.ReferencesType
10
+
11
+ private ovc: InputsType
12
+
13
+ constructor(references: Types.ReferencesType, ovc: InputsType) {
14
+ this.references = references
15
+ this.ovc = ovc
16
+ }
17
+
18
+ isOptionRelatedToOption(optionA: number, optionB: number): boolean {
19
+ // Getting B relations
20
+ const OptionBParents = this.references.relations.options[optionB].parents
21
+ const OptionBRoot = OptionBParents[OptionBParents.length - 1]
22
+ const OptionBRootRelations = OptionBRoot ? this.references.relations.options[OptionBRoot] : undefined
23
+
24
+ // Checking if B option is related to the option that changed
25
+ if (OptionBRootRelations) {
26
+ if (optionA === optionB || OptionBRootRelations.children.options.includes(optionA) || OptionBRootRelations.dependants.includes(optionA)) {
27
+ return true
28
+ }
29
+ }
30
+
31
+ return false
32
+ }
33
+
34
+ isVariableRelatedToOption(variableId: number, optionId: number): boolean {
35
+ // Variable parent
36
+ const variableParentId = this.references.relations.variables[variableId].parents[0]
37
+
38
+ // Getting B relations
39
+ const optionParents = this.references.relations.options[variableParentId].parents
40
+ const optionRoot = optionParents[optionParents.length - 1]
41
+ const optionRootRelations = optionRoot ? this.references.relations.options[optionRoot] : undefined
42
+
43
+ // Checking if B option is related to the option that changed
44
+ if (optionRootRelations) {
45
+ if (optionRootRelations.children.options.includes(optionId) || optionRootRelations.dependants.includes(optionId)) {
46
+ return true
47
+ }
48
+ }
49
+
50
+ return false
51
+ }
52
+
53
+ isVariableRelatedToVariable(variableA: number, variableB: number): boolean {
54
+ // Getting variables options
55
+ const optionA = this.references.relations.variables[variableA].parents[0]
56
+ const optionB = this.references.relations.variables[variableB].parents[0]
57
+
58
+ // Getting B relations
59
+ const OptionBParents = this.references.relations.options[optionB].parents
60
+ const OptionBRoot = OptionBParents[OptionBParents.length - 1]
61
+ const OptionBRootRelations = OptionBRoot ? this.references.relations.options[OptionBRoot] : undefined
62
+
63
+ // Checking if B option is related to the option that changed
64
+ if (OptionBRootRelations) {
65
+ if (optionA === optionB || OptionBRootRelations.children.options.includes(optionA) || OptionBRootRelations.dependants.includes(optionA)) {
66
+ return true
67
+ }
68
+ }
69
+
70
+ return false
71
+ }
72
+
73
+ executeCondition(
74
+ condition: {
75
+ dataMap: ConditionDataMap
76
+ conditions: ConditionV3
77
+ },
78
+ id: number,
79
+ index: number,
80
+ conditionType: 'options' | 'optionValidator' | 'variables' | 'prefillers' | 'variableValidator' | 'sections' | 'documents'
81
+ ): any {
82
+ if (Array.isArray(condition)) return condition.map(_condition => this.executeCondition(_condition, id, index, conditionType))
83
+
84
+ // Populating Data
85
+ let currentData
86
+ if (conditionType === 'options' || conditionType === 'optionValidator') {
87
+ currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, id, index).getData()
88
+ } else if (conditionType === 'variables' || conditionType === 'prefillers' || conditionType === 'variableValidator') {
89
+ const dataId = this.references.relations.variables[id].parents[0]
90
+ currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, dataId, index).getData()
91
+ } else if (conditionType === 'sections' || conditionType === 'documents') {
92
+ currentData = new DataPopulator(this, this.references, this.ovc, condition.dataMap, id, index).getData()
93
+ }
94
+
95
+ return LpLogic(condition.conditions, currentData)
96
+ }
97
+ }
98
+
99
+ export default ConditionsRunner