@ejfdelgado/ejflab-common 1.8.1 → 1.8.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-common",
3
3
  "type": "commonjs",
4
- "version": "1.8.1",
4
+ "version": "1.8.3",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ejfdelgado/ejflab-common.git"
package/src/MyTemplate.js CHANGED
@@ -194,7 +194,7 @@ class MyTemplate extends CsvWithFilters {
194
194
  };
195
195
  }
196
196
  }
197
- render(template, data) {
197
+ render(template, data, skipUndefined = false) {
198
198
 
199
199
  let iteration = {};
200
200
  do {
@@ -209,19 +209,32 @@ class MyTemplate extends CsvWithFilters {
209
209
  } while (iteration.times > 0);
210
210
 
211
211
  // Replace values
212
- return this.replaceBareValues(template, data);
212
+ return this.replaceBareValues(template, data, "\\{", "\\}", "}", false, skipUndefined);
213
213
  }
214
- replaceBareValues(template, data, open = "\\{", close = "\\}", closeNoScape = "}", useStringify = false) {
214
+ replaceBareValues(template, data, open = "\\{", close = "\\}", closeNoScape = "}", useStringify = false, skipUndefined = false) {
215
215
  const myPattern = new RegExp("\\$\\s*" + open + "([^" + closeNoScape + "]+)\\s*" + close, "g");
216
216
  template = template.replace(myPattern, (match, g1) => {
217
217
  const response = this.getColumnDescription(g1)[0];
218
218
  const valor = SimpleObj.getValue(data, response.id);
219
219
  if (!useStringify) {
220
220
  const temp = this.filterValue(valor, response, undefined, response.id);
221
- if (typeof temp == "object") {
222
- return JSON.stringify(temp);
221
+ if (!skipUndefined) {
222
+ // Duplicated code...
223
+ if (typeof temp == "object") {
224
+ return JSON.stringify(temp);
225
+ }
226
+ return temp;
227
+ } else {
228
+ if (temp === undefined) {
229
+ return match;
230
+ } else {
231
+ // Duplicated code...
232
+ if (typeof temp == "object") {
233
+ return JSON.stringify(temp);
234
+ }
235
+ return temp;
236
+ }
223
237
  }
224
- return temp;
225
238
  } else {
226
239
  const temp = this.filterValue(valor, response, undefined, response.id);
227
240
  return JSON.stringify(temp);
@@ -63,7 +63,19 @@ function myTest() {
63
63
  ]
64
64
  },
65
65
  exp: "Filtrado 3 sí debe salir",
66
- }
66
+ },
67
+ {
68
+ txt: "my name is ${name} ${lastname} and I like ${colors.0.id} ${colors.0.id}",
69
+ data: { lastname: "Delgado" },
70
+ exp: "my name is ${name} Delgado and I like ${colors.0.id} ${colors.0.id}",
71
+ skipUndefined: true,
72
+ },
73
+ {
74
+ txt: "my name is ${name} ${lastname} and I like ${colors.0.id} ${colors.0.id}",
75
+ data: { lastname: "Delgado" },
76
+ exp: "my name is undefined Delgado and I like undefined undefined",
77
+ skipUndefined: false,
78
+ },
67
79
  ];
68
80
 
69
81
  renderer.registerFunction("json", CsvFormatterFilters.json);
@@ -75,7 +87,7 @@ function myTest() {
75
87
 
76
88
  for (let i = 0; i < cases.length; i++) {
77
89
  const myCase = cases[i];
78
- const actual = renderer.render(myCase.txt, myCase.data);
90
+ const actual = renderer.render(myCase.txt, myCase.data, myCase.skipUndefined === true);
79
91
  //console.log(actual);
80
92
  if (actual !== myCase.exp) {
81
93
  throw Error(`expected:\n${myCase.exp}\nactual:\n${actual}`);
@@ -276,11 +276,12 @@ class FlowChartExec {
276
276
  return this.loadFlowChartInternal(paths, true);
277
277
  }
278
278
 
279
- complementFlowChart(paths) {
280
- return this.loadFlowChartInternal(paths, false);
279
+ complementFlowChart(paths, indexPath) {
280
+ console.log("complementFlowChart...");
281
+ return this.loadFlowChartInternal(paths, false, indexPath);
281
282
  }
282
283
 
283
- loadFlowChartInternal(paths, initialize) {
284
+ loadFlowChartInternal(paths, initialize, indexPath) {
284
285
  if (initialize) {
285
286
  this.flowchart = {
286
287
  shapes: [],
@@ -295,6 +296,12 @@ class FlowChartExec {
295
296
  };
296
297
  const llaves = Object.keys(paths);
297
298
  llaves.forEach((prefix) => {
299
+ // Get index Number from prefix if matches the pattern
300
+ const prefixGroupsNumber = /_([\d]+)$/.exec(prefix);
301
+ let index = null;
302
+ if (prefixGroupsNumber) {
303
+ index = parseInt(prefixGroupsNumber[1]);
304
+ }
298
305
  const path = paths[prefix];
299
306
  const xmlFlowText = fs.readFileSync(path, 'utf8');
300
307
  const parser = new XMLParser(options);
@@ -303,6 +310,15 @@ class FlowChartExec {
303
310
  this.interpretColor(localFlowChart.shapes);
304
311
  //
305
312
  const check = this.checkTypeOfChart(localFlowChart);
313
+ if (typeof indexPath == "string" && !isNaN(index)) {
314
+ // Write metadata of node
315
+ const placeMetadata = (node) => {
316
+ node.indexPath = indexPath;
317
+ SimpleObj.recreate(node, `meta.${indexPath}`, index);
318
+ }
319
+ localFlowChart.shapes.forEach(placeMetadata);
320
+ localFlowChart.arrows.forEach(placeMetadata);
321
+ }
306
322
  this.flowchart.shapes.push(...localFlowChart.shapes);
307
323
  this.flowchart.arrows.push(...localFlowChart.arrows);
308
324
  this.flowchart.prefixes.push(prefix);
@@ -313,8 +329,6 @@ class FlowChartExec {
313
329
  }
314
330
  });
315
331
 
316
- //console.log(JSON.stringify(this.flowchart.flux, null, 4));
317
-
318
332
  // Se indexan los nodos por id
319
333
  const BLACK_LIST = ["flux"];
320
334
  this.flowchart.shapes.forEach((node) => {
@@ -359,7 +373,7 @@ class FlowChartExec {
359
373
  return instance;
360
374
  }
361
375
 
362
- interpretArguments(argTxt) {
376
+ interpretArguments(argTxt, node) {
363
377
  //console.log(`Interpret ${argTxt}`);
364
378
  const tokens = argTxt.split(";");
365
379
  const args = [];
@@ -367,7 +381,13 @@ class FlowChartExec {
367
381
  const token = tokens[i];
368
382
  // Interpret with template
369
383
  if (token.trim().length > 0) {
370
- const rendered = this.renderer.render(token, this.data);
384
+ let rendered = token;
385
+ if (node.meta) {
386
+ // Render skiping meta
387
+ const skipUndefined = true;
388
+ rendered = this.renderer.render(rendered, node.meta, skipUndefined);
389
+ }
390
+ rendered = this.renderer.render(rendered, this.data);
371
391
  let value;
372
392
  try {
373
393
  value = eval(rendered);
@@ -388,7 +408,7 @@ class FlowChartExec {
388
408
  }
389
409
  let index = 0;
390
410
  const answered = await MyUtilities.replaceAsync2(txt, /(([a-z]+)«([^»]*)»)/ig, async (match, g1, commandName, argsTxt) => {
391
- const args = this.interpretArguments(argsTxt);
411
+ const args = this.interpretArguments(argsTxt, node);
392
412
  let instance = this.createInstance(commandName, id, txt, args);
393
413
  index++;
394
414
  const response1 = `${commandName}${JSON.stringify(args)}`;
@@ -416,7 +436,7 @@ class FlowChartExec {
416
436
  }
417
437
  let index = 0;
418
438
  const answered = await MyUtilities.replaceAsync2(txt, /(([a-z]+)«([^»]*)»)/ig, async (match, g1, commandName, argsTxt) => {
419
- const args = this.interpretArguments(argsTxt);
439
+ const args = this.interpretArguments(argsTxt, node);
420
440
  let instance = null;
421
441
  if (create || !this.instances[id][`${index}`]) {
422
442
  instance = this.createInstance(commandName, id, txt, args);
@@ -20,25 +20,38 @@ class StepMultiple extends StepBasic {
20
20
  }
21
21
  }
22
22
  async execFirst() {
23
+ // console.log(`StepMultiple ${JSON.stringify(this.args)}`);
23
24
  // Reads the list to iterate
24
25
  const room = this.context.getRoom();
25
26
  this.list = SimpleObj.getValue(this.context.data, this.args[1], []);
26
- const WORKSPACE = SimpleObj.getValue(this.context.data, "env.WORKSPACE", "./");
27
- //console.log(`WORKSPACE=${WORKSPACE}`);
28
- //console.log(`My list ${JSON.stringify(this.list)}`);
29
- // Loads the draw.io file
27
+ const WORKSPACE = SimpleObj.getValue(this.context.data, "env.WORKSPACE", ".");
30
28
  const flowChartTemplateName = this.args[0];
31
- const extraConfiguration = {
32
- Print: '${WORKSPACE}/flowcharts/multiple/flow01N.drawio',
33
- };
34
- this.completePaths(extraConfiguration, WORKSPACE);
29
+ const indexPath = this.args[2];
35
30
  // Force update the model
36
31
  const superContext = this.context.getSuperContext();
37
32
  const roomData = superContext.getRoomLiveTupleModel(room);
38
- roomData.model.flowchart = this.context.complementFlowChart(extraConfiguration);
39
- let changes = roomData.builder.trackDifferences(roomData.model, [], null, ["flowchart"]);
40
- roomData.model = roomData.builder.affect(changes);
41
- superContext.emitToRoom(room, "flowChartModified");
33
+ const routeMultiple = SimpleObj.getValue(roomData, `model.multiples.${flowChartTemplateName}`, null);
34
+ //console.log(`flowChartTemplateName=${flowChartTemplateName} routeMultiple=${routeMultiple}`)
35
+ if (typeof indexPath !== "string") {
36
+ console.log(`In StepMultiple, indexPath is not a string ${JSON.stringify(this.args)}`);
37
+ return;
38
+ }
39
+ if (routeMultiple) {
40
+ const extraConfiguration = {};
41
+
42
+ for (let i = 0; i < this.list.length; i++) {
43
+ extraConfiguration[`${flowChartTemplateName}_${i}`] = routeMultiple;
44
+ }
45
+ this.completePaths(extraConfiguration, WORKSPACE);
46
+
47
+ roomData.model.flowchart = this.context.complementFlowChart(extraConfiguration, indexPath);
48
+ let changes = roomData.builder.trackDifferences(roomData.model, [], null, ["flowchart"]);
49
+ roomData.model = roomData.builder.affect(changes);
50
+ superContext.emitToRoom(room, "flowChartModified");
51
+ } else {
52
+ console.log(`In StepMultiple, can't find model.multiples.${flowChartTemplateName}`);
53
+ //console.log(JSON.stringify(roomData, null, 4));
54
+ }
42
55
  }
43
56
 
44
57
  async canContinue() {