@inseefr/lunatic 2.7.7 → 2.7.8

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.
@@ -18,6 +18,7 @@ function cleaningBehaviour(store, cleaning) {
18
18
  var cleaningMap = new Map(Object.entries(cleaning));
19
19
  store.on('change', function (e) {
20
20
  var cleaningInfo = cleaningMap.get(e.detail.name);
21
+ var iteration = e.detail.iteration;
21
22
 
22
23
  // The variable does not have cleaning
23
24
  if (!cleaningInfo) {
@@ -25,15 +26,14 @@ function cleaningBehaviour(store, cleaning) {
25
26
  }
26
27
  for (var variableName in cleaningInfo) {
27
28
  try {
28
- var _initialValues$variab;
29
29
  var skipCleaning = store.run(cleaningInfo[variableName], {
30
- iteration: e.detail.iteration
30
+ iteration: iteration
31
31
  });
32
32
  if (skipCleaning) {
33
33
  continue;
34
34
  }
35
- store.set(variableName, (_initialValues$variab = initialValues[variableName]) !== null && _initialValues$variab !== void 0 ? _initialValues$variab : null, {
36
- iteration: e.detail.iteration
35
+ store.set(variableName, getValueAtIteration(initialValues[variableName], iteration), {
36
+ iteration: iteration
37
37
  });
38
38
  } catch (e) {
39
39
  // If we have an error, skip this cleaning
@@ -41,4 +41,13 @@ function cleaningBehaviour(store, cleaning) {
41
41
  }
42
42
  }
43
43
  });
44
+ }
45
+ function getValueAtIteration(value, iteration) {
46
+ if (!iteration || iteration.length === 0) {
47
+ return value !== null && value !== void 0 ? value : null;
48
+ }
49
+ if (!Array.isArray(value)) {
50
+ return null;
51
+ }
52
+ return getValueAtIteration(value[iteration[0]], iteration.slice(1));
44
53
  }
@@ -255,6 +255,21 @@ var _lunaticVariablesStore = require("./lunatic-variables-store");
255
255
  });
256
256
  (0, _vitest.expect)(variables.get('PRENOM')).toEqual(['John', null, 'Marc']);
257
257
  });
258
+ (0, _vitest.it)('should clean variables with initial value at specific iteration', function () {
259
+ variables.set('PRENOM', ['John', 'Jane', 'Marc']);
260
+ variables.set('READY', [true, true, true]);
261
+ (0, _cleaningBehaviour.cleaningBehaviour)(variables, {
262
+ READY: {
263
+ PRENOM: 'READY'
264
+ }
265
+ }, {
266
+ PRENOM: [null]
267
+ });
268
+ variables.set('READY', false, {
269
+ iteration: [1]
270
+ });
271
+ (0, _vitest.expect)(variables.get('PRENOM')).toEqual(['John', null, 'Marc']);
272
+ });
258
273
  });
259
274
  (0, _vitest.describe)('missing', function () {
260
275
  (0, _vitest.beforeEach)(function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inseefr/lunatic",
3
- "version": "2.7.7",
3
+ "version": "2.7.8",
4
4
  "workersVersion": "0.3.0",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {