@legalplace/prerenderx 2.5.2 → 2.5.3

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.
@@ -6,4 +6,6 @@ export declare const getRelatedVariablesValues: getRelatedVarsT;
6
6
  declare type autonumsType = [string, string, number, string | null][];
7
7
  declare type parseOutputT = (output: string, index: number, variables: relVarsValsT, autonums: autonumsType) => string;
8
8
  export declare const parseOutputWithVariables: parseOutputT;
9
+ declare type TParseDocumentNameWithVariables = (text: string, ovc: OVC_TYPE, references: Types.ReferencesType, fallback?: string) => string;
10
+ export declare const parseDocumentNameWithVariables: TParseDocumentNameWithVariables;
9
11
  export {};
@@ -1,6 +1,13 @@
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
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseOutputWithVariables = exports.getRelatedVariablesValues = void 0;
10
+ exports.parseDocumentNameWithVariables = exports.parseOutputWithVariables = exports.getRelatedVariablesValues = void 0;
4
11
  exports.getRelatedVariablesValues = function (optionId, index, variables, ovc, references) {
5
12
  var variablesValues = {};
6
13
  variables.forEach(function (currentVariableId) {
@@ -49,3 +56,34 @@ exports.parseOutputWithVariables = function (output, index, variables, autonums)
49
56
  });
50
57
  return parsedOutput;
51
58
  };
59
+ exports.parseDocumentNameWithVariables = function (text, ovc, references, fallback) {
60
+ var tagsMatch = text.match(/\[var:([0-9]+)\]/gi);
61
+ var variables = [];
62
+ if (tagsMatch !== null) {
63
+ var variablesTags = __spreadArrays(tagsMatch);
64
+ variables = variablesTags.map(function (current) {
65
+ var outputVariableMatch = current.match(/([0-9]+)/);
66
+ if (outputVariableMatch === null)
67
+ return 0;
68
+ return parseInt(outputVariableMatch[0], 10);
69
+ });
70
+ }
71
+ variables = variables.filter(function (c, i) { return variables.indexOf(c) === i; });
72
+ variables = variables.filter(function (c) { return ovc !== undefined || references.variables[c] !== undefined; });
73
+ var variablesValues = {};
74
+ variables.forEach(function (variableId) {
75
+ var _a;
76
+ var value = ((_a = ovc.variables[variableId]) === null || _a === void 0 ? void 0 : _a[0]) || '';
77
+ variablesValues[variableId] = value.toString();
78
+ });
79
+ var parsedText = "" + text;
80
+ variables.forEach(function (variableId) {
81
+ var value = variablesValues[variableId];
82
+ if (value.trim().length === 0 && fallback)
83
+ parsedText = fallback;
84
+ if (parsedText === fallback && fallback)
85
+ return;
86
+ parsedText = parsedText.replace(new RegExp("\\[var:" + variableId + "\\]", 'gi'), value);
87
+ });
88
+ return parsedText;
89
+ };
@@ -11,6 +11,7 @@ var OvcConverter_1 = __importDefault(require("./OvcConverter"));
11
11
  var NumAuto_1 = __importDefault(require("./NumAuto"));
12
12
  var InputsInitiator_1 = __importDefault(require("./InputsInitiator"));
13
13
  var FillPdfForm_1 = __importDefault(require("./FillPdfForm"));
14
+ var OutputParser_1 = require("./OutputParser");
14
15
  var Prerender = (function () {
15
16
  function Prerender(model, ovc) {
16
17
  this.documentsIndex = {};
@@ -37,21 +38,21 @@ var Prerender = (function () {
37
38
  };
38
39
  Prerender.prototype.generateDocumentNamesHash = function () {
39
40
  var _this = this;
40
- Object.keys(this.model.documents).forEach(function (documentName) {
41
+ Object.keys(this.model.documents).forEach(function (slug) {
41
42
  var _a, _b;
42
- var document = _this.model.documents[documentName];
43
- var documentParams = _this.model.documents[documentName].params;
44
- var conditionObject = _this.references.conditions.documents[documentName];
43
+ var document = _this.model.documents[slug];
44
+ var documentParams = _this.model.documents[slug].params;
45
+ var conditionObject = _this.references.conditions.documents[slug];
45
46
  var condition = conditionObject === undefined ? true : _this.conditions.executeCondition(conditionObject, 0, 0, 'documents');
46
47
  if (condition === true) {
47
48
  var hash = crypto_1.default
48
49
  .createHash('sha1')
49
- .update(documentName)
50
+ .update(slug)
50
51
  .digest('hex');
51
52
  if (typeof document.pdf === 'object' && typeof document.pdf.id === 'string' && typeof document.pdf.form === 'object') {
52
53
  _this.documentsIndex[hash] = {
53
- name: document.name,
54
- slug: documentName,
54
+ name: OutputParser_1.parseDocumentNameWithVariables(document.name, _this.ovc, _this.references, document.fallbackName),
55
+ slug: slug,
55
56
  form: true,
56
57
  fdf: {
57
58
  id: '',
@@ -61,8 +62,8 @@ var Prerender = (function () {
61
62
  }
62
63
  else {
63
64
  _this.documentsIndex[hash] = {
64
- name: document.name,
65
- slug: documentName,
65
+ name: OutputParser_1.parseDocumentNameWithVariables(document.name, _this.ovc, _this.references, document.fallbackName),
66
+ slug: slug,
66
67
  pdf: ((_a = documentParams === null || documentParams === void 0 ? void 0 : documentParams.formats) === null || _a === void 0 ? void 0 : _a.pdf) === undefined ? true : documentParams.formats.pdf,
67
68
  docx: ((_b = documentParams === null || documentParams === void 0 ? void 0 : documentParams.formats) === null || _b === void 0 ? void 0 : _b.docx) === undefined ? true : documentParams.formats.docx
68
69
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/prerenderx",
3
- "version": "2.5.2",
3
+ "version": "2.5.3",
4
4
  "description": "PrerenderX",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://git.legalplace.eu/legalplace/prerenderx",
@@ -15,8 +15,8 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@legalplace/lplogic": "^2.1.6",
18
- "@legalplace/models-v3-types": "^3.4.47",
19
- "@legalplace/referencesparser": "^1.6.1",
18
+ "@legalplace/models-v3-types": "^3.5.1",
19
+ "@legalplace/referencesparser": "^1.6.3",
20
20
  "crypto": "^1.0.1",
21
21
  "typescript": "3.9.3"
22
22
  },
@@ -87,3 +87,63 @@ export const parseOutputWithVariables: parseOutputT = (output, index, variables,
87
87
 
88
88
  return parsedOutput
89
89
  }
90
+
91
+
92
+
93
+ /**
94
+ * Parses document name with variables
95
+ * @param text Document's name
96
+ */
97
+ type TParseDocumentNameWithVariables = (
98
+ text: string,
99
+ ovc: OVC_TYPE,
100
+ references: Types.ReferencesType,
101
+ fallback?: string
102
+ ) => string;
103
+ export const parseDocumentNameWithVariables: TParseDocumentNameWithVariables = (text, ovc, references, fallback) => {
104
+ /**
105
+ * Variables
106
+ */
107
+
108
+ // Extracting list of variables
109
+ const tagsMatch = text.match(/\[var:([0-9]+)\]/gi);
110
+ let variables: number[] = [];
111
+ if (tagsMatch !== null) {
112
+ // Tranforming tags into integer ids
113
+ const variablesTags: string[] = [...tagsMatch];
114
+ variables = variablesTags.map((current) => {
115
+ const outputVariableMatch = current.match(/([0-9]+)/);
116
+ if (outputVariableMatch === null) return 0;
117
+ return parseInt(outputVariableMatch[0], 10);
118
+ });
119
+ }
120
+
121
+ // Removing duplicated variables
122
+ variables = variables.filter((c, i) => variables.indexOf(c) === i);
123
+
124
+ // Making sure all variables exist
125
+ variables = variables.filter(
126
+ (c) => ovc !== undefined || references.variables[c] !== undefined
127
+ );
128
+
129
+ // Getting values
130
+ const variablesValues: Record<string, string> = {};
131
+ variables.forEach((variableId) => {
132
+ const value = ovc.variables[variableId]?.[0] || ''
133
+ variablesValues[variableId] = value.toString();
134
+ });
135
+
136
+ // Inserting values
137
+ let parsedText = `${text}`;
138
+ variables.forEach((variableId) => {
139
+ const value: string = variablesValues[variableId];
140
+ if (value.trim().length === 0 && fallback) parsedText = fallback;
141
+ if (parsedText === fallback && fallback) return;
142
+ parsedText = parsedText.replace(
143
+ new RegExp(`\\[var:${variableId}\\]`, 'gi'),
144
+ value
145
+ );
146
+ });
147
+
148
+ return parsedText;
149
+ };
@@ -8,6 +8,7 @@ import OvcConverter, { OldOVC } from './OvcConverter'
8
8
  import NumAuto from './NumAuto'
9
9
  import IntputsInitiator from './InputsInitiator'
10
10
  import FillPdfForm from './FillPdfForm'
11
+ import { parseDocumentNameWithVariables } from './OutputParser'
11
12
 
12
13
  type DocumentOutputIndex = {
13
14
  name: string
@@ -91,24 +92,24 @@ class Prerender {
91
92
  }
92
93
 
93
94
  private generateDocumentNamesHash() {
94
- Object.keys(this.model.documents).forEach(documentName => {
95
- const document = this.model.documents[documentName]
96
- const documentParams = this.model.documents[documentName].params
97
- const conditionObject = this.references.conditions.documents[documentName]
95
+ Object.keys(this.model.documents).forEach(slug => {
96
+ const document = this.model.documents[slug]
97
+ const documentParams = this.model.documents[slug].params
98
+ const conditionObject = this.references.conditions.documents[slug]
98
99
  const condition = conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, 0, 0, 'documents')
99
100
 
100
101
  // Rendering documents sections
101
102
  if (condition === true) {
102
103
  const hash = crypto
103
104
  .createHash('sha1')
104
- .update(documentName)
105
+ .update(slug)
105
106
  .digest('hex')
106
107
 
107
108
  // Referencing in index
108
109
  if (typeof document.pdf === 'object' && typeof document.pdf.id === 'string' && typeof document.pdf.form === 'object') {
109
110
  this.documentsIndex[hash] = {
110
- name: document.name,
111
- slug: documentName,
111
+ name: parseDocumentNameWithVariables(document.name, this.ovc, this.references, document.fallbackName),
112
+ slug,
112
113
  form: true,
113
114
  fdf: {
114
115
  id: '',
@@ -117,8 +118,8 @@ class Prerender {
117
118
  }
118
119
  } else {
119
120
  this.documentsIndex[hash] = {
120
- name: document.name,
121
- slug: documentName,
121
+ name: parseDocumentNameWithVariables(document.name, this.ovc, this.references, document.fallbackName),
122
+ slug,
122
123
  pdf: documentParams?.formats?.pdf === undefined ? true : documentParams.formats.pdf,
123
124
  docx: documentParams?.formats?.docx === undefined ? true : documentParams.formats.docx
124
125
  }