@legalplace/prerenderx 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,12 @@
1
1
  {
2
2
  "eslint.enable": true,
3
- "eslint.autoFixOnSave": true,
4
3
  "editor.tabSize": 2,
5
- "eslint.validate": [
6
- "javascript",
7
- {
8
- "autoFix": true,
9
- "language": "typescript"
10
- }
11
- ],
12
4
  "eslint.options": {
13
5
  "extensions": [".ts", ".js"]
14
6
  },
7
+ "editor.codeActionsOnSave": {
8
+ "source.fixAll.eslint": true
9
+ },
15
10
  "files.autoSave": "off",
16
11
  "editor.formatOnSave": false,
17
12
  "editor.formatOnType": true,
@@ -0,0 +1,19 @@
1
+ import OVC_TYPE from "./ovc.type";
2
+ export interface OldOVC {
3
+ o: {
4
+ [k: string]: string | number | (string | number)[];
5
+ };
6
+ v: {
7
+ [k: string]: string | number | (string | number)[];
8
+ };
9
+ c: {
10
+ [k: string]: boolean | boolean[];
11
+ };
12
+ }
13
+ declare class OvcConverter {
14
+ convertToOptionsVariables(ovc: OldOVC): OVC_TYPE;
15
+ isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC;
16
+ isOptionsVariables(_obj: OVC_TYPE | OldOVC): boolean;
17
+ }
18
+ declare const _default: OvcConverter;
19
+ export default _default;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var OvcConverter = (function () {
4
+ function OvcConverter() {
5
+ }
6
+ OvcConverter.prototype.convertToOptionsVariables = function (ovc) {
7
+ var inputs = {
8
+ options: {},
9
+ variables: {}
10
+ };
11
+ if (typeof ovc !== "object")
12
+ return inputs;
13
+ if (typeof ovc.o === "object") {
14
+ Object.keys(ovc.o).forEach(function (id) {
15
+ var currentOption = ovc.o[id];
16
+ if (Array.isArray(currentOption)) {
17
+ var occurences_1 = currentOption.length;
18
+ currentOption.forEach(function (childId, index) {
19
+ if (typeof childId === "string" && childId.trim().length === 0)
20
+ return;
21
+ if (!Object.prototype.hasOwnProperty.call(inputs.options, childId)) {
22
+ inputs.options[childId] = new Array(occurences_1).fill(false);
23
+ }
24
+ inputs.options[childId][index] = true;
25
+ });
26
+ }
27
+ else {
28
+ if (typeof currentOption === "string" &&
29
+ currentOption.trim().length === 0)
30
+ return;
31
+ inputs.options[currentOption] = [true];
32
+ }
33
+ });
34
+ }
35
+ if (typeof ovc.v === "object") {
36
+ Object.keys(ovc.v).forEach(function (id) {
37
+ var currentVariable = ovc.v[id];
38
+ if (Array.isArray(currentVariable)) {
39
+ inputs.variables[id] = currentVariable;
40
+ }
41
+ else {
42
+ inputs.variables[id] = [currentVariable];
43
+ }
44
+ });
45
+ }
46
+ if (typeof ovc.c === "object") {
47
+ Object.keys(ovc.c).forEach(function (id) {
48
+ var currentCheckbox = ovc.c[id];
49
+ if (typeof id === "string" && id.trim().length === 0)
50
+ return;
51
+ if (Array.isArray(currentCheckbox)) {
52
+ inputs.options[id] = currentCheckbox;
53
+ }
54
+ else {
55
+ inputs.options[id] = [currentCheckbox];
56
+ }
57
+ });
58
+ }
59
+ return inputs;
60
+ };
61
+ OvcConverter.prototype.isOvc = function (_obj) {
62
+ if (typeof _obj !== "object" || _obj === null)
63
+ return false;
64
+ var ovcKeys = ["o", "v", "c"];
65
+ var foundKeys = Object.keys(_obj)
66
+ .map(function (key) { return (ovcKeys.includes(key) ? 1 : 0); })
67
+ .reduce(function (a, b) { return a + b; }, 0);
68
+ return foundKeys > 1;
69
+ };
70
+ OvcConverter.prototype.isOptionsVariables = function (_obj) {
71
+ if (typeof _obj !== "object" || _obj === null)
72
+ return false;
73
+ var optionsVariablesKeys = ["options", "variables"];
74
+ var foundKeys = Object.keys(_obj)
75
+ .map(function (key) { return (optionsVariablesKeys.includes(key) ? 1 : 0); })
76
+ .reduce(function (a, b) { return a + b; }, 0);
77
+ return foundKeys === 2;
78
+ };
79
+ return OvcConverter;
80
+ }());
81
+ exports.default = new OvcConverter();
@@ -1,5 +1,6 @@
1
1
  import { ModelV3 } from "@legalplace/models-v3-types";
2
2
  import OVC_TYPE from "./ovc.type";
3
+ import { OldOVC } from "./OvcConverter";
3
4
  declare class Prerender {
4
5
  private model;
5
6
  private references;
@@ -9,7 +10,7 @@ declare class Prerender {
9
10
  private NumAuto;
10
11
  private documentsIndex;
11
12
  private renderedDocuments;
12
- constructor(model: ModelV3, ovc: OVC_TYPE);
13
+ constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC);
13
14
  getDocumentsIndex(): Record<string, {
14
15
  name: string;
15
16
  pdf: boolean;
@@ -7,13 +7,16 @@ var referencesparser_1 = require("@legalplace/referencesparser");
7
7
  var crypto_1 = __importDefault(require("crypto"));
8
8
  var DocumentRender_1 = __importDefault(require("./DocumentRender"));
9
9
  var ConditionsRunner_1 = __importDefault(require("./ConditionsRunner"));
10
+ var OvcConverter_1 = __importDefault(require("./OvcConverter"));
10
11
  var NumAuto_1 = __importDefault(require("./NumAuto"));
11
12
  var Prerender = (function () {
12
13
  function Prerender(model, ovc) {
13
14
  this.documentsIndex = {};
14
15
  this.renderedDocuments = {};
15
16
  this.model = model;
16
- this.ovc = ovc;
17
+ this.ovc = OvcConverter_1.default.isOvc(ovc)
18
+ ? OvcConverter_1.default.convertToOptionsVariables(ovc)
19
+ : ovc;
17
20
  this.NumAuto = new NumAuto_1.default();
18
21
  this.references = new referencesparser_1.ReferencesParser(model).getReferences();
19
22
  this.conditions = new ConditionsRunner_1.default(this.references, this.ovc);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/prerenderx",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "PrerenderX",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://git.legalplace.eu/legalplace/prerenderx",
@@ -0,0 +1,107 @@
1
+ import OVC_TYPE from "./ovc.type";
2
+
3
+ export interface OldOVC {
4
+ o: {
5
+ [k: string]: string | number | (string | number)[];
6
+ };
7
+ v: {
8
+ [k: string]: string | number | (string | number)[];
9
+ };
10
+ c: {
11
+ [k: string]: boolean | boolean[];
12
+ };
13
+ }
14
+
15
+ class OvcConverter {
16
+ public convertToOptionsVariables(ovc: OldOVC) {
17
+ const inputs: OVC_TYPE = {
18
+ options: {},
19
+ variables: {}
20
+ };
21
+
22
+ if (typeof ovc !== "object") return inputs;
23
+
24
+ // Reading options
25
+ if (typeof ovc.o === "object") {
26
+ Object.keys(ovc.o).forEach(id => {
27
+ const currentOption = ovc.o[id];
28
+
29
+ // If it's a multiple
30
+ if (Array.isArray(currentOption)) {
31
+ const occurences = currentOption.length;
32
+ currentOption.forEach((childId, index) => {
33
+ if (typeof childId === "string" && childId.trim().length === 0)
34
+ return;
35
+
36
+ // Creating input if it doesn't exist
37
+ if (
38
+ !Object.prototype.hasOwnProperty.call(inputs.options, childId)
39
+ ) {
40
+ inputs.options[childId] = new Array(occurences).fill(false);
41
+ }
42
+
43
+ // Setting current index to true
44
+ inputs.options[childId][index] = true;
45
+ });
46
+ } else {
47
+ if (
48
+ typeof currentOption === "string" &&
49
+ currentOption.trim().length === 0
50
+ )
51
+ return;
52
+ inputs.options[currentOption] = [true];
53
+ }
54
+ });
55
+ }
56
+
57
+ // Reading variables
58
+ if (typeof ovc.v === "object") {
59
+ Object.keys(ovc.v).forEach(id => {
60
+ const currentVariable = ovc.v[id];
61
+
62
+ // If it's a multiple
63
+ if (Array.isArray(currentVariable)) {
64
+ inputs.variables[id] = currentVariable;
65
+ } else {
66
+ inputs.variables[id] = [currentVariable];
67
+ }
68
+ });
69
+ }
70
+
71
+ // Reading checkboxes
72
+ if (typeof ovc.c === "object") {
73
+ Object.keys(ovc.c).forEach(id => {
74
+ const currentCheckbox = ovc.c[id];
75
+ if (typeof id === "string" && id.trim().length === 0) return;
76
+ // If it's a multiple
77
+ if (Array.isArray(currentCheckbox)) {
78
+ inputs.options[id] = currentCheckbox;
79
+ } else {
80
+ inputs.options[id] = [currentCheckbox];
81
+ }
82
+ });
83
+ }
84
+
85
+ return inputs;
86
+ }
87
+
88
+ public isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC {
89
+ if (typeof _obj !== "object" || _obj === null) return false;
90
+ const ovcKeys = ["o", "v", "c"];
91
+ const foundKeys = Object.keys(_obj)
92
+ .map((key): number => (ovcKeys.includes(key) ? 1 : 0))
93
+ .reduce((a, b) => a + b, 0);
94
+ return foundKeys > 1;
95
+ }
96
+
97
+ public isOptionsVariables(_obj: OVC_TYPE | OldOVC) {
98
+ if (typeof _obj !== "object" || _obj === null) return false;
99
+ const optionsVariablesKeys = ["options", "variables"];
100
+ const foundKeys = Object.keys(_obj)
101
+ .map((key): number => (optionsVariablesKeys.includes(key) ? 1 : 0))
102
+ .reduce((a, b) => a + b, 0);
103
+ return foundKeys === 2;
104
+ }
105
+ }
106
+
107
+ export default new OvcConverter();
@@ -4,6 +4,7 @@ import crypto from "crypto";
4
4
  import DocumentRender from "./DocumentRender";
5
5
  import ConditionsRunner from "./ConditionsRunner";
6
6
  import OVC_TYPE from "./ovc.type";
7
+ import OvcConverter, { OldOVC } from "./OvcConverter";
7
8
  import NumAuto from "./NumAuto";
8
9
 
9
10
  class Prerender {
@@ -30,12 +31,18 @@ class Prerender {
30
31
 
31
32
  private renderedDocuments: Record<string, string> = {};
32
33
 
33
- constructor(model: ModelV3, ovc: OVC_TYPE) {
34
+ constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC) {
34
35
  this.model = model;
35
- this.ovc = ovc;
36
+
37
+ // Converting OVC if needed
38
+ this.ovc = OvcConverter.isOvc(ovc)
39
+ ? OvcConverter.convertToOptionsVariables(ovc)
40
+ : ovc;
41
+
42
+ // Initiating num auto
36
43
  this.NumAuto = new NumAuto();
37
44
 
38
- // @ts-ignore
45
+ // Parsing references
39
46
  this.references = new ReferencesParser(model).getReferences();
40
47
 
41
48
  // Initiating ConditionsRunner