@inseefr/lunatic 2.7.6 → 2.7.7

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.
@@ -31,9 +31,10 @@ export declare class LunaticVariablesStore {
31
31
  /**
32
32
  * Register calculated variable
33
33
  */
34
- setCalculated(name: string, expression: string, { dependencies, iterationDepth, }?: {
34
+ setCalculated(name: string, expression: string, { dependencies, iterationDepth, shapeFrom, }?: {
35
35
  dependencies?: string[];
36
36
  iterationDepth?: number;
37
+ shapeFrom?: string;
37
38
  }): LunaticVariable;
38
39
  /**
39
40
  * Run a VTL expression
@@ -60,12 +61,14 @@ declare class LunaticVariable {
60
61
  private readonly expression?;
61
62
  private readonly dictionary?;
62
63
  private readonly iterationDepth?;
64
+ private readonly shapeFrom?;
63
65
  private readonly name?;
64
66
  constructor(args?: {
65
67
  expression?: string;
66
68
  dependencies?: string[];
67
69
  dictionary?: Map<string, LunaticVariable>;
68
70
  iterationDepth?: number;
71
+ shapeFrom?: string;
69
72
  name?: string;
70
73
  });
71
74
  getValue(iteration?: IterationLevel): unknown;
@@ -84,13 +84,15 @@ var LunaticVariablesStore = exports.LunaticVariablesStore = /*#__PURE__*/functio
84
84
  value: function setCalculated(name, expression) {
85
85
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
86
86
  dependencies = _ref.dependencies,
87
- iterationDepth = _ref.iterationDepth;
87
+ iterationDepth = _ref.iterationDepth,
88
+ shapeFrom = _ref.shapeFrom;
88
89
  if (this.dictionary.has(name)) {
89
90
  return this.dictionary.get(name);
90
91
  }
91
92
  var variable = new LunaticVariable({
92
93
  expression: expression,
93
94
  dictionary: this.dictionary,
95
+ shapeFrom: shapeFrom,
94
96
  dependencies: dependencies,
95
97
  iterationDepth: iterationDepth,
96
98
  name: name
@@ -160,7 +162,8 @@ var LunaticVariablesStore = exports.LunaticVariablesStore = /*#__PURE__*/functio
160
162
  case 'CALCULATED':
161
163
  store.setCalculated(variable.name, variable.expression.value, {
162
164
  dependencies: variable.bindingDependencies,
163
- iterationDepth: getIterationDepth(variable.name)
165
+ iterationDepth: getIterationDepth(variable.name),
166
+ shapeFrom: variable.shapeFrom
164
167
  });
165
168
  break;
166
169
  case 'COLLECTED':
@@ -216,6 +219,8 @@ var LunaticVariable = /*#__PURE__*/function () {
216
219
  this.dictionary = void 0;
217
220
  // Specific iteration depth to get value from dependencies (used for yAxis for instance)
218
221
  this.iterationDepth = void 0;
222
+ // For calculated variable, shape is copied from another variable
223
+ this.shapeFrom = void 0;
219
224
  // Keep a record of variable name (optional, used for debug)
220
225
  this.name = void 0;
221
226
  if (args.expression && !args.dictionary) {
@@ -225,15 +230,34 @@ var LunaticVariable = /*#__PURE__*/function () {
225
230
  this.dictionary = args.dictionary;
226
231
  this.dependencies = args.dependencies;
227
232
  this.iterationDepth = args.iterationDepth;
233
+ this.shapeFrom = args.shapeFrom;
228
234
  this.name = (_args$name = args.name) !== null && _args$name !== void 0 ? _args$name : args.expression;
229
235
  }
230
236
  _createClass(LunaticVariable, [{
231
237
  key: "getValue",
232
238
  value: function getValue(iteration) {
239
+ var _this$dictionary,
240
+ _this = this,
241
+ _iteration;
233
242
  // The variable is not calculated
234
243
  if (!this.expression) {
235
244
  return this.getSavedValue(iteration);
236
245
  }
246
+ var shapeFromValue = this.shapeFrom ? (_this$dictionary = this.dictionary) === null || _this$dictionary === void 0 || (_this$dictionary = _this$dictionary.get(this.shapeFrom)) === null || _this$dictionary === void 0 ? void 0 : _this$dictionary.getValue() : null;
247
+ // If we want the root value of a calculated array, loop using the shapeFrom value
248
+ if (!iteration && Array.isArray(shapeFromValue)) {
249
+ return shapeFromValue.map(function (_, k) {
250
+ return _this.getValue([k]);
251
+ });
252
+ }
253
+
254
+ // For calculated variable, ignore iteration if shapeFrom exists and is not an array
255
+ if (
256
+ // We have a calculated variable (not a simple expression)
257
+ this.name !== this.expression && !Array.isArray(shapeFromValue)) {
258
+ iteration = undefined;
259
+ }
260
+
237
261
  // Calculate bindings first to refresh "updatedAt" on calculated dependencies
238
262
  var bindings = this.getDependenciesValues(iteration);
239
263
  if (!this.isOutdated(iteration)) {
@@ -248,7 +272,7 @@ var LunaticVariable = /*#__PURE__*/function () {
248
272
  } catch (e) {
249
273
  throw new Error("Cannot interpret expression \"".concat(this.expression, "\" with bindings ").concat(JSON.stringify(bindings), ", error : ").concat(e.toString()));
250
274
  }
251
- this.calculatedAt.set(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'), performance.now());
275
+ this.calculatedAt.set((_iteration = iteration) === null || _iteration === void 0 ? void 0 : _iteration.join('.'), performance.now());
252
276
  this.calculatedAt.set(undefined, performance.now());
253
277
  return this.getSavedValue(iteration);
254
278
  }
@@ -282,13 +306,13 @@ var LunaticVariable = /*#__PURE__*/function () {
282
306
  }, {
283
307
  key: "setValueForArray",
284
308
  value: function setValueForArray(value) {
285
- var _this = this;
309
+ var _this2 = this;
286
310
  var savedValue = this.getSavedValue();
287
311
  var oldSize = Array.isArray(savedValue) ? savedValue.length : -1;
288
312
  var newSize = value.length;
289
313
  // Update every item of the array and look if we changed one item
290
314
  var oneValueChanged = (0, _array.times)(Math.max(oldSize, newSize), function (k) {
291
- return _this.setValue(value[k], [k]);
315
+ return _this2.setValue(value[k], [k]);
292
316
  }).find(function (v) {
293
317
  return v;
294
318
  }) !== undefined;
@@ -334,21 +358,21 @@ var LunaticVariable = /*#__PURE__*/function () {
334
358
  }, {
335
359
  key: "getDependenciesValues",
336
360
  value: function getDependenciesValues(iteration) {
337
- var _this2 = this;
361
+ var _this3 = this;
338
362
  try {
339
363
  return Object.fromEntries(this.getDependencies().map(function (dep) {
340
- var _this2$dictionary, _this2$dictionary$get;
364
+ var _this3$dictionary, _this3$dictionary$get;
341
365
  if (dep === iterationVariableName && iteration) {
342
- return [dep, iteration[0]];
366
+ return [dep, iteration[0] + 1];
343
367
  }
344
- var dependencyIteration = (0, _number.isNumber)(_this2.iterationDepth) && Array.isArray(iteration) ? [iteration[_this2.iterationDepth]] : iteration;
368
+ var dependencyIteration = (0, _number.isNumber)(_this3.iterationDepth) && Array.isArray(iteration) ? [iteration[_this3.iterationDepth]] : iteration;
345
369
 
346
370
  // The variable is not registered in the variable dictionary
347
371
  // Happens when calculating unquoted VTL expression
348
- if (!_this2.dictionary || !((_this2$dictionary = _this2.dictionary) !== null && _this2$dictionary !== void 0 && _this2$dictionary.has(dep))) {
349
- throw new Error("Unknown variable \"".concat(dep, "\" in expression ").concat(_this2.expression));
372
+ if (!_this3.dictionary || !((_this3$dictionary = _this3.dictionary) !== null && _this3$dictionary !== void 0 && _this3$dictionary.has(dep))) {
373
+ throw new Error("Unknown variable \"".concat(dep, "\" in expression ").concat(_this3.expression));
350
374
  }
351
- return [dep, (_this2$dictionary$get = _this2.dictionary.get(dep)) === null || _this2$dictionary$get === void 0 ? void 0 : _this2$dictionary$get.getValue(dependencyIteration)];
375
+ return [dep, (_this3$dictionary$get = _this3.dictionary.get(dep)) === null || _this3$dictionary$get === void 0 ? void 0 : _this3$dictionary$get.getValue(dependencyIteration)];
352
376
  }));
353
377
  } catch (e) {
354
378
  if (e instanceof RangeError) {
@@ -360,11 +384,11 @@ var LunaticVariable = /*#__PURE__*/function () {
360
384
  }, {
361
385
  key: "isOutdated",
362
386
  value: function isOutdated(iteration) {
363
- var _this3 = this,
387
+ var _this4 = this,
364
388
  _this$calculatedAt$ge;
365
389
  var dependenciesUpdatedAt = Math.max.apply(Math, [0].concat(_toConsumableArray(this.getDependencies().map(function (dep) {
366
- var _this3$dictionary$get, _this3$dictionary;
367
- return (_this3$dictionary$get = (_this3$dictionary = _this3.dictionary) === null || _this3$dictionary === void 0 || (_this3$dictionary = _this3$dictionary.get(dep)) === null || _this3$dictionary === void 0 ? void 0 : _this3$dictionary.updatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this3$dictionary$get !== void 0 ? _this3$dictionary$get : 0;
390
+ var _this4$dictionary$get, _this4$dictionary;
391
+ return (_this4$dictionary$get = (_this4$dictionary = _this4.dictionary) === null || _this4$dictionary === void 0 || (_this4$dictionary = _this4$dictionary.get(dep)) === null || _this4$dictionary === void 0 ? void 0 : _this4$dictionary.updatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this4$dictionary$get !== void 0 ? _this4$dictionary$get : 0;
368
392
  }))));
369
393
  return dependenciesUpdatedAt > ((_this$calculatedAt$ge = this.calculatedAt.get(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'))) !== null && _this$calculatedAt$ge !== void 0 ? _this$calculatedAt$ge : -1);
370
394
  }
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
 
3
3
  var _vitest = require("vitest");
4
- var _lunaticVariablesStore = require("./lunatic-variables-store");
5
- var _resizingBehaviour = require("./behaviours/resizing-behaviour");
6
4
  var _cleaningBehaviour = require("./behaviours/cleaning-behaviour");
7
5
  var _missingBehaviour = require("./behaviours/missing-behaviour");
6
+ var _resizingBehaviour = require("./behaviours/resizing-behaviour");
7
+ var _lunaticVariablesStore = require("./lunatic-variables-store");
8
8
  (0, _vitest.describe)('lunatic-variables-store', function () {
9
9
  var variables;
10
10
  (0, _vitest.beforeEach)(function () {
@@ -152,7 +152,8 @@ var _missingBehaviour = require("./behaviours/missing-behaviour");
152
152
  variables.set('FIRSTNAME', ['John', 'Jane']);
153
153
  variables.set('LASTNAME', ['Doe', 'Dae']);
154
154
  variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || LASTNAME', {
155
- dependencies: ['FIRSTNAME', 'LASTNAME']
155
+ dependencies: ['FIRSTNAME', 'LASTNAME'],
156
+ shapeFrom: 'FIRSTNAME'
156
157
  });
157
158
  (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John Doe');
158
159
  (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane Dae');
@@ -185,9 +186,18 @@ var _missingBehaviour = require("./behaviours/missing-behaviour");
185
186
  });
186
187
  (0, _vitest.it)('should handle global iteration variable', function () {
187
188
  variables.set('FIRSTNAME', ['John', 'Jane']);
188
- variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)');
189
- (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John 0');
190
- (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane 1');
189
+ variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)', {
190
+ shapeFrom: 'FIRSTNAME'
191
+ });
192
+ (0, _vitest.expect)(variables.get('FULLNAME', [0])).toEqual('John 1');
193
+ (0, _vitest.expect)(variables.get('FULLNAME', [1])).toEqual('Jane 2');
194
+ });
195
+ (0, _vitest.it)('should handle shapeFrom correctly', function () {
196
+ variables.set('FIRSTNAME', ['John', 'Jane']);
197
+ variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)', {
198
+ shapeFrom: 'FIRSTNAME'
199
+ });
200
+ (0, _vitest.expect)(variables.get('FULLNAME')).toEqual(['John 1', 'Jane 2']);
191
201
  });
192
202
  });
193
203
  (0, _vitest.describe)('resizing', function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inseefr/lunatic",
3
- "version": "2.7.6",
3
+ "version": "2.7.7",
4
4
  "workersVersion": "0.3.0",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {