@inseefr/lunatic 2.7.15 → 2.7.16

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.
@@ -76,6 +76,7 @@ declare class LunaticVariable {
76
76
  * Set the value and returns true if the variable is touched
77
77
  */
78
78
  setValue(value: unknown, iteration?: IterationLevel): boolean;
79
+ private updateTimestamps;
79
80
  private setValueForArray;
80
81
  private getSavedValue;
81
82
  private getDependencies;
@@ -11,4 +11,14 @@ export declare function setAtIndex<T>(arr: T, index: number | number[], newValue
11
11
  */
12
12
  export declare function getAtIndex(arr: unknown, indexes: number[]): unknown;
13
13
  export declare function resizeArray<T = unknown>(array: unknown, newLength: number, defaultValue?: T): T[];
14
- export declare function firstItem<T = unknown>(items: T | T[]): T;
14
+ /**
15
+ * Return the first non null/undefined value of an array
16
+ */
17
+ export declare function firstValueItem<T = unknown>(items: T | T[]): T | undefined;
18
+ /**
19
+ * Return a list of partial array
20
+ *
21
+ * Example :
22
+ * - subArray([1, 2, 3]) // [[1], [1, 2], [1, 2, 3]]
23
+ */
24
+ export declare function subArrays<T = unknown>(items: T[] | T): T[][];
@@ -14,7 +14,7 @@ function executeConditionFilter(filter, execute, iteration) {
14
14
  iteration: iteration
15
15
  });
16
16
  // Todo : replace this with a casting system on execute
17
- return Array.isArray(result) ? (0, _array.firstItem)(result) : result;
17
+ return Array.isArray(result) ? (0, _array.firstValueItem)(result) : result;
18
18
  }
19
19
  return undefined;
20
20
  }
@@ -42,7 +42,7 @@ function createCrawl(_ref) {
42
42
  var result = executeExpression(candidate, {
43
43
  iteration: linksIterations !== null && linksIterations !== void 0 ? linksIterations : iteration
44
44
  });
45
- return _objectSpread(_objectSpread({}, object), {}, _defineProperty({}, path, Array.isArray(result) ? (0, _array.firstItem)(result) : result));
45
+ return _objectSpread(_objectSpread({}, object), {}, _defineProperty({}, path, Array.isArray(result) ? (0, _array.firstValueItem)(result) : result));
46
46
  } catch (e) {
47
47
  return _objectSpread(_objectSpread({}, object), {}, _defineProperty({}, path, e instanceof Error ? e.toString() : e));
48
48
  }
@@ -237,8 +237,7 @@ var LunaticVariable = /*#__PURE__*/function () {
237
237
  key: "getValue",
238
238
  value: function getValue(iteration) {
239
239
  var _this$dictionary,
240
- _this = this,
241
- _iteration;
240
+ _this = this;
242
241
  // The variable is not calculated
243
242
  if (!this.expression) {
244
243
  return this.getSavedValue(iteration);
@@ -265,7 +264,7 @@ var LunaticVariable = /*#__PURE__*/function () {
265
264
  if (Object.keys(bindings).length === 0) {
266
265
  iteration = undefined;
267
266
  }
268
- if (!this.isOutdated(iteration)) {
267
+ if (this.shapeFrom && !this.isOutdated(iteration)) {
269
268
  return this.getSavedValue(iteration);
270
269
  }
271
270
  if ((0, _env.isTestEnv)()) {
@@ -277,8 +276,7 @@ var LunaticVariable = /*#__PURE__*/function () {
277
276
  } catch (e) {
278
277
  throw new Error("Cannot interpret expression \"".concat(this.expression, "\" with bindings ").concat(JSON.stringify(bindings), ", error : ").concat(e.toString()));
279
278
  }
280
- this.calculatedAt.set((_iteration = iteration) === null || _iteration === void 0 ? void 0 : _iteration.join('.'), performance.now());
281
- this.calculatedAt.set(undefined, performance.now());
279
+ this.updateTimestamps(iteration, 'calculatedAt');
282
280
  return this.getSavedValue(iteration);
283
281
  }
284
282
 
@@ -300,14 +298,27 @@ var LunaticVariable = /*#__PURE__*/function () {
300
298
  this.value = [];
301
299
  }
302
300
  this.value = !Array.isArray(iteration) ? value : (0, _array.setAtIndex)(this.value, iteration, value);
303
- if (value === undefined) {
304
- this.updatedAt["delete"](iteration === null || iteration === void 0 ? void 0 : iteration.join('.'));
305
- } else {
306
- this.updatedAt.set(iteration === null || iteration === void 0 ? void 0 : iteration.join('.'), performance.now());
307
- }
308
- this.updatedAt.set(undefined, performance.now());
301
+ this.updateTimestamps(iteration, 'updatedAt');
309
302
  return true;
310
303
  }
304
+ }, {
305
+ key: "updateTimestamps",
306
+ value: function updateTimestamps(iteration, key) {
307
+ // Update parent iteration level timestamp
308
+ var _iterator2 = _createForOfIteratorHelper((0, _array.subArrays)(iteration)),
309
+ _step2;
310
+ try {
311
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
312
+ var subIteration = _step2.value;
313
+ this[key].set(subIteration.join('.'), performance.now());
314
+ }
315
+ } catch (err) {
316
+ _iterator2.e(err);
317
+ } finally {
318
+ _iterator2.f();
319
+ }
320
+ this[key].set(undefined, performance.now());
321
+ }
311
322
  }, {
312
323
  key: "setValueForArray",
313
324
  value: function setValueForArray(value) {
@@ -334,20 +345,20 @@ var LunaticVariable = /*#__PURE__*/function () {
334
345
  return this.value;
335
346
  }
336
347
  var current = this.value;
337
- var _iterator2 = _createForOfIteratorHelper(iteration),
338
- _step2;
348
+ var _iterator3 = _createForOfIteratorHelper(iteration),
349
+ _step3;
339
350
  try {
340
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
341
- var index = _step2.value;
351
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
352
+ var index = _step3.value;
342
353
  if (!Array.isArray(current)) {
343
354
  return current;
344
355
  }
345
356
  current = current[index];
346
357
  }
347
358
  } catch (err) {
348
- _iterator2.e(err);
359
+ _iterator3.e(err);
349
360
  } finally {
350
- _iterator2.f();
361
+ _iterator3.f();
351
362
  }
352
363
  return current;
353
364
  }
@@ -37,7 +37,8 @@ var _lunaticVariablesStore = require("./lunatic-variables-store");
37
37
  variables.set('FIRSTNAME', 'John');
38
38
  variables.set('LASTNAME', 'Doe');
39
39
  variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || LASTNAME', {
40
- dependencies: ['FIRSTNAME', 'LASTNAME']
40
+ dependencies: ['FIRSTNAME', 'LASTNAME'],
41
+ shapeFrom: 'FIRSTNAME'
41
42
  });
42
43
  (0, _vitest.expect)(variables.get('FULLNAME')).toEqual('John Doe');
43
44
  (0, _vitest.expect)(variables.interpretCount).toBe(1);
@@ -54,10 +55,12 @@ var _lunaticVariablesStore = require("./lunatic-variables-store");
54
55
  variables.set('LASTNAME', 'Doe');
55
56
  variables.set('AGE', '18');
56
57
  variables.setCalculated('FULLNAME', 'FIRSTNAME || " " || LASTNAME', {
57
- dependencies: ['FIRSTNAME', 'LASTNAME']
58
+ dependencies: ['FIRSTNAME', 'LASTNAME'],
59
+ shapeFrom: 'FIRSTNAME'
58
60
  });
59
61
  variables.setCalculated('LABEL', 'FULLNAME || " is " || AGE', {
60
- dependencies: ['FULLNAME', 'AGE']
62
+ dependencies: ['FULLNAME', 'AGE'],
63
+ shapeFrom: 'FULLNAME'
61
64
  });
62
65
  (0, _vitest.expect)(variables.get('LABEL')).toEqual('John Doe is 18');
63
66
  (0, _vitest.expect)(variables.interpretCount).toBe(2);
@@ -83,7 +86,7 @@ var _lunaticVariablesStore = require("./lunatic-variables-store");
83
86
  variables.set('LASTNAME', 'Doe');
84
87
  (0, _vitest.expect)(variables.run('FIRSTNAME || " " || LASTNAME')).toEqual('John Doe');
85
88
  (0, _vitest.expect)(variables.run('FIRSTNAME || " " || LASTNAME')).toEqual('John Doe');
86
- (0, _vitest.expect)(variables.interpretCount).toBe(1);
89
+ (0, _vitest.expect)(variables.interpretCount).toBe(2);
87
90
  variables.set('FIRSTNAME', 'Jane');
88
91
  (0, _vitest.expect)(variables.run('FIRSTNAME || " " || LASTNAME')).toEqual('Jane Doe');
89
92
  });
@@ -220,7 +223,19 @@ var _lunaticVariablesStore = require("./lunatic-variables-store");
220
223
  iteration: [1]
221
224
  });
222
225
  (0, _vitest.expect)(variables.run('"hello"')).toEqual('hello');
223
- (0, _vitest.expect)(variables.interpretCount).toBe(1);
226
+ (0, _vitest.expect)(variables.interpretCount).toBe(3);
227
+ });
228
+ (0, _vitest.it)('should handle deep refresh', function () {
229
+ variables.set('LIENS', [['17', null], [null, '17']]);
230
+ variables.setCalculated('IS_12', 'if ("12" in LIENS) then 1 else 0', {
231
+ dependencies: ['LIENS'],
232
+ shapeFrom: 'LIENS'
233
+ });
234
+ (0, _vitest.expect)(variables.get('IS_12', [0])).toBe(0);
235
+ variables.set('LIENS', '12', {
236
+ iteration: [0, 0]
237
+ });
238
+ (0, _vitest.expect)(variables.get('IS_12', [0])).toBe(1);
224
239
  });
225
240
  });
226
241
  (0, _vitest.describe)('resizing', function () {
@@ -3,10 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.firstItem = firstItem;
6
+ exports.firstValueItem = firstValueItem;
7
7
  exports.getAtIndex = getAtIndex;
8
8
  exports.resizeArray = resizeArray;
9
9
  exports.setAtIndex = setAtIndex;
10
+ exports.subArrays = subArrays;
10
11
  exports.times = times;
11
12
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
12
13
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -83,23 +84,35 @@ function resizeArray(array, newLength, defaultValue) {
83
84
  return index < array.length ? array[index] : value;
84
85
  }, []);
85
86
  }
86
- function firstItem(items) {
87
+
88
+ /**
89
+ * Return the first non null/undefined value of an array
90
+ */
91
+ function firstValueItem(items) {
87
92
  if (!Array.isArray(items)) {
88
93
  return items;
89
94
  }
90
- var _iterator2 = _createForOfIteratorHelper(items),
91
- _step2;
92
- try {
93
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
94
- var item = _step2.value;
95
- if (item !== undefined) {
96
- return item;
97
- }
98
- }
99
- } catch (err) {
100
- _iterator2.e(err);
101
- } finally {
102
- _iterator2.f();
95
+ return items.find(function (v) {
96
+ return v !== undefined && v !== null;
97
+ });
98
+ }
99
+
100
+ /**
101
+ * Return a list of partial array
102
+ *
103
+ * Example :
104
+ * - subArray([1, 2, 3]) // [[1], [1, 2], [1, 2, 3]]
105
+ */
106
+ function subArrays(items) {
107
+ if (!Array.isArray(items)) {
108
+ return [];
109
+ }
110
+ if (items.length === 1) {
111
+ return [items];
112
+ }
113
+ var arrays = [];
114
+ for (var i = 1; i <= items.length; i++) {
115
+ arrays.push(items.slice(0, i));
103
116
  }
104
- return undefined;
117
+ return arrays;
105
118
  }
@@ -27,4 +27,9 @@ var _array = require("./array");
27
27
  (0, _vitest.expect)((0, _array.setAtIndex)([null], [1, 2], 10)).toEqual([null, [null, null, 10]]);
28
28
  });
29
29
  });
30
+ (0, _vitest.it)('firstValueItem', function () {
31
+ (0, _vitest.expect)((0, _array.firstValueItem)([0, 1, 2])).toBe(0);
32
+ (0, _vitest.expect)((0, _array.firstValueItem)([null, 1, 2])).toBe(1);
33
+ (0, _vitest.expect)((0, _array.firstValueItem)([null, undefined, false])).toBe(false);
34
+ });
30
35
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inseefr/lunatic",
3
- "version": "2.7.15",
3
+ "version": "2.7.16",
4
4
  "workersVersion": "0.3.0",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {