@inseefr/lunatic 3.4.11-rc.0 → 3.5.0-rc.2

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.
Files changed (111) hide show
  1. package/components/Loop/Loop.js +7 -27
  2. package/components/Loop/Loop.js.map +1 -1
  3. package/components/RosterForLoop/RosterForLoop.js +8 -37
  4. package/components/RosterForLoop/RosterForLoop.js.map +1 -1
  5. package/components/Sequence/Sequence.d.ts +1 -1
  6. package/components/Subsequence/Subsequence.d.ts +1 -1
  7. package/components/library.d.ts +2 -2
  8. package/esm/components/Loop/Loop.js +7 -27
  9. package/esm/components/Loop/Loop.js.map +1 -1
  10. package/esm/components/RosterForLoop/RosterForLoop.js +8 -34
  11. package/esm/components/RosterForLoop/RosterForLoop.js.map +1 -1
  12. package/esm/components/Sequence/Sequence.d.ts +1 -1
  13. package/esm/components/Subsequence/Subsequence.d.ts +1 -1
  14. package/esm/components/library.d.ts +2 -2
  15. package/esm/hooks/useArticulation.d.ts +58 -0
  16. package/esm/hooks/useArticulation.js +81 -0
  17. package/esm/hooks/useArticulation.js.map +1 -0
  18. package/esm/i18n/dictionary.d.ts +0 -8
  19. package/esm/i18n/dictionary.js +0 -5
  20. package/esm/i18n/dictionary.js.map +1 -1
  21. package/esm/i18n/index.d.ts +1 -1
  22. package/esm/index.d.ts +1 -0
  23. package/esm/index.js +1 -0
  24. package/esm/index.js.map +1 -1
  25. package/esm/type.source.js +1 -0
  26. package/esm/type.source.js.map +1 -1
  27. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +7 -23
  28. package/esm/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  29. package/esm/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  30. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  31. package/esm/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  32. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  33. package/esm/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  34. package/esm/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  35. package/esm/use-lunatic/reducer/commons/index.d.ts +1 -0
  36. package/esm/use-lunatic/reducer/commons/index.js +1 -0
  37. package/esm/use-lunatic/reducer/commons/index.js.map +1 -1
  38. package/esm/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  39. package/esm/use-lunatic/reducer/commons/resize-array-variable.js +21 -0
  40. package/esm/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  41. package/esm/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  42. package/esm/use-lunatic/reducer/reducerInitializer.js +31 -24
  43. package/esm/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  44. package/esm/use-lunatic/type.d.ts +0 -1
  45. package/esm/utils/array.d.ts +0 -4
  46. package/esm/utils/array.js +2 -15
  47. package/esm/utils/array.js.map +1 -1
  48. package/esm/utils/array.spec.js +1 -12
  49. package/esm/utils/array.spec.js.map +1 -1
  50. package/hooks/useArticulation.d.ts +58 -0
  51. package/hooks/useArticulation.js +81 -0
  52. package/hooks/useArticulation.js.map +1 -0
  53. package/i18n/dictionary.d.ts +0 -8
  54. package/i18n/dictionary.js +0 -5
  55. package/i18n/dictionary.js.map +1 -1
  56. package/i18n/index.d.ts +1 -1
  57. package/index.d.ts +1 -0
  58. package/index.js +3 -1
  59. package/index.js.map +1 -1
  60. package/package.json +20 -4
  61. package/src/components/Loop/Loop.tsx +12 -42
  62. package/src/components/RosterForLoop/RosterForLoop.tsx +2 -42
  63. package/src/components/RosterForLoop/__snapshots__/RosterForLoop.spec.tsx.snap +0 -18
  64. package/src/hooks/useArticulation.ts +124 -0
  65. package/src/i18n/dictionary.ts +0 -5
  66. package/src/index.ts +1 -0
  67. package/src/stories/behaviour/articulation/articulation.stories.tsx +105 -0
  68. package/src/stories/behaviour/articulation/roundabout.json +348 -0
  69. package/src/stories/behaviour/performance/performance.stories.jsx +1 -1
  70. package/src/stories/pairwise/data.json +1 -1
  71. package/src/stories/pairwise/source.json +1 -3
  72. package/src/stories/utils/{SchemaValidator.jsx → SchemaValidator.tsx} +1 -1
  73. package/src/stories/utils/{orchestrator.jsx → orchestrator.tsx} +13 -9
  74. package/src/stories/utils/{overview.jsx → overview.tsx} +2 -0
  75. package/src/type.source.ts +1 -0
  76. package/src/use-lunatic/__snapshots__/use-lunatic.test.ts.snap +323 -0
  77. package/src/use-lunatic/commons/variables/behaviours/resizing-behaviour.ts +10 -35
  78. package/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts +1 -68
  79. package/src/use-lunatic/commons/variables/lunatic-variables-store.ts +2 -4
  80. package/src/use-lunatic/reducer/commons/index.ts +1 -0
  81. package/src/use-lunatic/reducer/commons/resize-array-variable.ts +28 -0
  82. package/src/use-lunatic/reducer/reducerInitializer.tsx +40 -29
  83. package/src/use-lunatic/type.ts +0 -1
  84. package/src/utils/array.spec.ts +1 -18
  85. package/src/utils/array.ts +3 -21
  86. package/tsconfig.build.tsbuildinfo +1 -1
  87. package/type.source.js +1 -0
  88. package/type.source.js.map +1 -1
  89. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js +6 -22
  90. package/use-lunatic/commons/variables/behaviours/resizing-behaviour.js.map +1 -1
  91. package/use-lunatic/commons/variables/lunatic-variables-store.d.ts +1 -3
  92. package/use-lunatic/commons/variables/lunatic-variables-store.js +1 -1
  93. package/use-lunatic/commons/variables/lunatic-variables-store.js.map +1 -1
  94. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js +1 -66
  95. package/use-lunatic/commons/variables/lunatic-variables-store.spec.js.map +1 -1
  96. package/use-lunatic/props/getComponentTypeProps.d.ts +4 -4
  97. package/use-lunatic/reducer/commons/index.d.ts +1 -0
  98. package/use-lunatic/reducer/commons/index.js +6 -0
  99. package/use-lunatic/reducer/commons/index.js.map +1 -1
  100. package/use-lunatic/reducer/commons/resize-array-variable.d.ts +5 -0
  101. package/use-lunatic/reducer/commons/resize-array-variable.js +23 -0
  102. package/use-lunatic/reducer/commons/resize-array-variable.js.map +1 -0
  103. package/use-lunatic/reducer/reducerInitializer.d.ts +2 -2
  104. package/use-lunatic/reducer/reducerInitializer.js +31 -24
  105. package/use-lunatic/reducer/reducerInitializer.js.map +1 -1
  106. package/use-lunatic/type.d.ts +0 -1
  107. package/utils/array.d.ts +0 -4
  108. package/utils/array.js +2 -16
  109. package/utils/array.js.map +1 -1
  110. package/utils/array.spec.js +0 -11
  111. package/utils/array.spec.js.map +1 -1
@@ -1,3 +1,5 @@
1
+ /* eslint-disable */
2
+ // @ts-nocheck
1
3
  import './custom-lunatic.scss';
2
4
  import './orchestrator.scss';
3
5
 
@@ -7,19 +9,19 @@ import {
7
9
  LunaticComponents,
8
10
  ModalControls,
9
11
  useLunatic,
10
- } from '../..';
11
- import React, { memo, useCallback, useEffect, useMemo, useState } from 'react';
12
+ } from '../../index.js';
13
+ import { memo, useCallback, useEffect, useState } from 'react';
12
14
 
13
- import { Logger } from '../../utils/logger';
14
- import { Overview } from './overview';
15
+ import { Logger } from '../../utils/logger.js';
16
+ import { Overview } from './overview.js';
15
17
  import { SchemaValidator } from './SchemaValidator.jsx';
16
18
 
17
19
  const Input = components.Input;
18
20
 
19
- function DevOptions({ goToPage, getData }) {
21
+ function DevOptions({ goToPage, getData }: { goToPage: any; getData: any }) {
20
22
  const [toPage, setToPage] = useState(1);
21
23
 
22
- function handleChange(_, value) {
24
+ function handleChange(_, value: any) {
23
25
  setToPage(value);
24
26
  }
25
27
 
@@ -33,6 +35,8 @@ function DevOptions({ goToPage, getData }) {
33
35
  </div>
34
36
  <Input
35
37
  id="page-to-jump"
38
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
39
+ // @ts-ignore
36
40
  value={toPage}
37
41
  handleChange={handleChange}
38
42
  min={1}
@@ -89,7 +93,7 @@ function Pager({
89
93
  );
90
94
  }
91
95
 
92
- function onLogChange(response, value, args) {
96
+ function onLogChange(response: any, value: any, args: any) {
93
97
  Logger.log('onChange', { response, value, args });
94
98
  }
95
99
 
@@ -110,7 +114,7 @@ function OrchestratorForStories({
110
114
  missingStrategy = logMissingStrategy,
111
115
  missingShortcut,
112
116
  autoSuggesterLoading,
113
- addExternal,
117
+ // addExternal,
114
118
  preferences,
115
119
  slots,
116
120
  showOverview = false,
@@ -136,7 +140,7 @@ function OrchestratorForStories({
136
140
  pageTag,
137
141
  isFirstPage,
138
142
  isLastPage,
139
- waiting,
143
+ // waiting,
140
144
  overview,
141
145
  compileControls,
142
146
  getData,
@@ -1,3 +1,5 @@
1
+ /* eslint-disable */
2
+ // @ts-nocheck
1
3
  import { Fragment } from 'react';
2
4
  import './overview.scss';
3
5
 
@@ -1,3 +1,4 @@
1
+ /* eslint-disable */
1
2
  /**
2
3
  * This file was automatically generated by json-schema-to-typescript.
3
4
  * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
@@ -1855,6 +1855,329 @@ exports[`use-lunatic() > overview > with loop > should handle initialPage 1`] =
1855
1855
  ]
1856
1856
  `;
1857
1857
 
1858
+ exports[`use-lunatic() > overview > with loop > should handle lastReachedPage 1`] = `
1859
+ [
1860
+ {
1861
+ "children": [
1862
+ {
1863
+ "children": [],
1864
+ "current": false,
1865
+ "description": undefined,
1866
+ "id": "lujqeci5",
1867
+ "label": <MDLabel
1868
+ expression="Votre santé"
1869
+ />,
1870
+ "page": "2",
1871
+ "reached": true,
1872
+ "type": "Subsequence",
1873
+ },
1874
+ {
1875
+ "children": [],
1876
+ "current": false,
1877
+ "description": undefined,
1878
+ "id": "lujqbyzl",
1879
+ "label": <MDLabel
1880
+ expression="Votre logement"
1881
+ />,
1882
+ "page": "3",
1883
+ "reached": true,
1884
+ "type": "Subsequence",
1885
+ },
1886
+ ],
1887
+ "current": true,
1888
+ "description": undefined,
1889
+ "id": "lujqfpva",
1890
+ "label": <MDLabel
1891
+ expression="I - Séquence 1"
1892
+ />,
1893
+ "page": "1",
1894
+ "reached": true,
1895
+ "type": "Sequence",
1896
+ },
1897
+ {
1898
+ "children": [
1899
+ {
1900
+ "children": [],
1901
+ "current": false,
1902
+ "description": undefined,
1903
+ "id": "lulbmyhr",
1904
+ "label": <MDLabel
1905
+ expression="Commentaire sur composition du logement"
1906
+ />,
1907
+ "page": "8",
1908
+ "reached": true,
1909
+ "type": "Subsequence",
1910
+ },
1911
+ ],
1912
+ "current": false,
1913
+ "description": undefined,
1914
+ "id": "lujqrqmp",
1915
+ "label": <MDLabel
1916
+ expression="II - Habitants du logement"
1917
+ />,
1918
+ "page": "5",
1919
+ "reached": true,
1920
+ "type": "Sequence",
1921
+ },
1922
+ {
1923
+ "children": [
1924
+ {
1925
+ "children": [],
1926
+ "current": false,
1927
+ "description": undefined,
1928
+ "id": "lujykwaz",
1929
+ "label": <MDLabel
1930
+ expression="Caractéristiques de Quentin"
1931
+ />,
1932
+ "page": "10.1#1",
1933
+ "reached": true,
1934
+ "type": "Subsequence",
1935
+ },
1936
+ {
1937
+ "children": [],
1938
+ "current": false,
1939
+ "description": undefined,
1940
+ "id": "lujykwaz",
1941
+ "label": <MDLabel
1942
+ expression="Caractéristiques de Luna"
1943
+ />,
1944
+ "page": "10.1#2",
1945
+ "reached": true,
1946
+ "type": "Subsequence",
1947
+ },
1948
+ {
1949
+ "children": [],
1950
+ "current": false,
1951
+ "description": undefined,
1952
+ "id": "lujykwaz",
1953
+ "label": <MDLabel
1954
+ expression="Caractéristiques de Paul"
1955
+ />,
1956
+ "page": "10.1#3",
1957
+ "reached": true,
1958
+ "type": "Subsequence",
1959
+ },
1960
+ {
1961
+ "children": [],
1962
+ "current": false,
1963
+ "description": undefined,
1964
+ "id": "lujyik5q",
1965
+ "label": <MDLabel
1966
+ expression="Autres caractéristiques de Luna : filtrée pour le premier individu"
1967
+ />,
1968
+ "page": "11.1#2",
1969
+ "reached": true,
1970
+ "type": "Subsequence",
1971
+ },
1972
+ {
1973
+ "children": [],
1974
+ "current": false,
1975
+ "description": undefined,
1976
+ "id": "luk0swcz",
1977
+ "label": <MDLabel
1978
+ expression="Encore d'autres caractéristiques de Luna : filtrée pour le premier individu"
1979
+ />,
1980
+ "page": "11.2#2",
1981
+ "reached": true,
1982
+ "type": "Subsequence",
1983
+ },
1984
+ {
1985
+ "children": [],
1986
+ "current": false,
1987
+ "description": undefined,
1988
+ "id": "lujyik5q",
1989
+ "label": <MDLabel
1990
+ expression="Autres caractéristiques de Paul : filtrée pour le premier individu"
1991
+ />,
1992
+ "page": "11.1#3",
1993
+ "reached": false,
1994
+ "type": "Subsequence",
1995
+ },
1996
+ {
1997
+ "children": [],
1998
+ "current": false,
1999
+ "description": undefined,
2000
+ "id": "luk0swcz",
2001
+ "label": <MDLabel
2002
+ expression="Encore d'autres caractéristiques de Paul : filtrée pour le premier individu"
2003
+ />,
2004
+ "page": "11.2#3",
2005
+ "reached": false,
2006
+ "type": "Subsequence",
2007
+ },
2008
+ ],
2009
+ "current": false,
2010
+ "description": undefined,
2011
+ "id": "lujyi4pe",
2012
+ "label": <MDLabel
2013
+ expression="III - Détail des individus"
2014
+ />,
2015
+ "page": "9",
2016
+ "reached": true,
2017
+ "type": "Sequence",
2018
+ },
2019
+ {
2020
+ "children": [
2021
+ {
2022
+ "children": [],
2023
+ "current": false,
2024
+ "description": undefined,
2025
+ "id": "lumfc98o",
2026
+ "label": <MDLabel
2027
+ expression="Belle sous-séquence pour Quentin"
2028
+ />,
2029
+ "page": "12.2#1",
2030
+ "reached": false,
2031
+ "type": "Subsequence",
2032
+ },
2033
+ {
2034
+ "children": [],
2035
+ "current": false,
2036
+ "description": undefined,
2037
+ "id": "lumfe3bj",
2038
+ "label": <MDLabel
2039
+ expression="Autre belle sous-seq"
2040
+ />,
2041
+ "page": "12.3#1",
2042
+ "reached": false,
2043
+ "type": "Subsequence",
2044
+ },
2045
+ ],
2046
+ "current": false,
2047
+ "description": undefined,
2048
+ "id": "luk1ojt5",
2049
+ "label": <MDLabel
2050
+ expression="IV - Belle séquence pour Quentin"
2051
+ />,
2052
+ "page": "12.1#1",
2053
+ "reached": false,
2054
+ "type": "Sequence",
2055
+ },
2056
+ {
2057
+ "children": [],
2058
+ "current": false,
2059
+ "description": undefined,
2060
+ "id": "lulbelgr",
2061
+ "label": <MDLabel
2062
+ expression="V - Autre séquence pour Quentin"
2063
+ />,
2064
+ "page": "12.4#1",
2065
+ "reached": false,
2066
+ "type": "Sequence",
2067
+ },
2068
+ {
2069
+ "children": [
2070
+ {
2071
+ "children": [],
2072
+ "current": false,
2073
+ "description": undefined,
2074
+ "id": "lumfc98o",
2075
+ "label": <MDLabel
2076
+ expression="Belle sous-séquence pour Luna"
2077
+ />,
2078
+ "page": "12.2#2",
2079
+ "reached": false,
2080
+ "type": "Subsequence",
2081
+ },
2082
+ {
2083
+ "children": [],
2084
+ "current": false,
2085
+ "description": undefined,
2086
+ "id": "lumfe3bj",
2087
+ "label": <MDLabel
2088
+ expression="Autre belle sous-seq"
2089
+ />,
2090
+ "page": "12.3#2",
2091
+ "reached": false,
2092
+ "type": "Subsequence",
2093
+ },
2094
+ ],
2095
+ "current": false,
2096
+ "description": undefined,
2097
+ "id": "luk1ojt5",
2098
+ "label": <MDLabel
2099
+ expression="IV - Belle séquence pour Luna"
2100
+ />,
2101
+ "page": "12.1#2",
2102
+ "reached": false,
2103
+ "type": "Sequence",
2104
+ },
2105
+ {
2106
+ "children": [],
2107
+ "current": false,
2108
+ "description": undefined,
2109
+ "id": "lulbelgr",
2110
+ "label": <MDLabel
2111
+ expression="V - Autre séquence pour Luna"
2112
+ />,
2113
+ "page": "12.4#2",
2114
+ "reached": false,
2115
+ "type": "Sequence",
2116
+ },
2117
+ {
2118
+ "children": [
2119
+ {
2120
+ "children": [],
2121
+ "current": false,
2122
+ "description": undefined,
2123
+ "id": "lumfc98o",
2124
+ "label": <MDLabel
2125
+ expression="Belle sous-séquence pour Paul"
2126
+ />,
2127
+ "page": "12.2#3",
2128
+ "reached": false,
2129
+ "type": "Subsequence",
2130
+ },
2131
+ {
2132
+ "children": [],
2133
+ "current": false,
2134
+ "description": undefined,
2135
+ "id": "lumfe3bj",
2136
+ "label": <MDLabel
2137
+ expression="Autre belle sous-seq"
2138
+ />,
2139
+ "page": "12.3#3",
2140
+ "reached": false,
2141
+ "type": "Subsequence",
2142
+ },
2143
+ ],
2144
+ "current": false,
2145
+ "description": undefined,
2146
+ "id": "luk1ojt5",
2147
+ "label": <MDLabel
2148
+ expression="IV - Belle séquence pour Paul"
2149
+ />,
2150
+ "page": "12.1#3",
2151
+ "reached": false,
2152
+ "type": "Sequence",
2153
+ },
2154
+ {
2155
+ "children": [],
2156
+ "current": false,
2157
+ "description": undefined,
2158
+ "id": "lulbelgr",
2159
+ "label": <MDLabel
2160
+ expression="V - Autre séquence pour Paul"
2161
+ />,
2162
+ "page": "12.4#3",
2163
+ "reached": false,
2164
+ "type": "Sequence",
2165
+ },
2166
+ {
2167
+ "children": [],
2168
+ "current": false,
2169
+ "description": undefined,
2170
+ "id": "COMMENT-SEQ",
2171
+ "label": <MDLabel
2172
+ expression="Commentaire"
2173
+ />,
2174
+ "page": "13",
2175
+ "reached": false,
2176
+ "type": "Sequence",
2177
+ },
2178
+ ]
2179
+ `;
2180
+
1858
2181
  exports[`use-lunatic() > overview > with loop > should work with loop 1`] = `
1859
2182
  [
1860
2183
  {
@@ -1,8 +1,9 @@
1
1
  import type { LunaticVariablesStore } from '../lunatic-variables-store';
2
2
  import type { LunaticSource } from '../../../type';
3
3
  import { forceInt } from '../../../../utils/number';
4
+ import { resizeArrayVariable } from '../../../reducer/commons';
4
5
  import { getExpressionAsString } from '../../../../utils/vtl';
5
- import { resizeArray, resizeDownArrayWithIndex } from '../../../../utils/array';
6
+ import { resizeArray } from '../../../../utils/array';
6
7
 
7
8
  /**
8
9
  * Resizing behaviour for the store
@@ -41,20 +42,8 @@ export function resizingBehaviour(
41
42
  const newSize = forceInt(store.run(resizingInfo.size));
42
43
  for (const variableName of resizingInfo.variables) {
43
44
  const value = store.get(variableName);
44
- if (Array.isArray(value) && e.detail.removedIndex) {
45
- store.set(
46
- variableName,
47
- resizeDownArrayWithIndex(value, e.detail.removedIndex),
48
- {
49
- cause: 'resizing',
50
- }
51
- );
52
- }
53
- if (
54
- !e.detail.removedIndex &&
55
- (!Array.isArray(value) || value.length !== newSize)
56
- ) {
57
- store.set(variableName, resizeArray(value, newSize, null), {
45
+ if (!Array.isArray(value) || value.length !== newSize) {
46
+ store.set(variableName, resizeArrayVariable(value, newSize, null), {
58
47
  cause: 'resizing',
59
48
  });
60
49
  }
@@ -72,7 +61,6 @@ function resizePairwise(
72
61
  },
73
62
  args: {
74
63
  iteration?: number[];
75
- removedIndex?: number;
76
64
  }
77
65
  ) {
78
66
  // Handle expression being sent as an array or an object (ensure backward compatibility)
@@ -90,25 +78,12 @@ function resizePairwise(
90
78
  });
91
79
  resizingInfo.linksVariables.forEach((variable) => {
92
80
  const value = store.get(variable, args.iteration);
93
- let resizedValue;
94
- if (args.removedIndex) {
95
- const removedIndex = args.removedIndex;
96
- resizedValue = resizeDownArrayWithIndex(
97
- Array.isArray(value)
98
- ? value.map((i) => resizeDownArrayWithIndex(i, removedIndex))
99
- : [],
100
- removedIndex
101
- );
102
- } else {
103
- resizedValue = resizeArray(
104
- // The value is not an array, force an array
105
- Array.isArray(value)
106
- ? value.map((i) => resizeArray(i, ySize, null))
107
- : [],
108
- xSize,
109
- new Array(ySize).fill(null)
110
- );
111
- }
81
+ const resizedValue = resizeArray(
82
+ // The value is not an array, force an array
83
+ Array.isArray(value) ? value.map((i) => resizeArray(i, ySize, null)) : [],
84
+ xSize,
85
+ new Array(ySize).fill(null)
86
+ );
112
87
  store.set(variable, resizedValue);
113
88
  });
114
89
  }
@@ -281,28 +281,6 @@ describe('lunatic-variables-store', () => {
281
281
  cause: 'resizing',
282
282
  });
283
283
  });
284
-
285
- it('should resize variables with Index', () => {
286
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
287
- variables.set('AGE', [20, 30, 40]);
288
- const spy = vi.fn();
289
- variables.on('change', (e) => spy(e.detail));
290
- resizingBehaviour(variables, {
291
- PRENOM: {
292
- size: 'count(PRENOM)',
293
- variables: ['AGE'],
294
- },
295
- });
296
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
297
- expect((variables.get('PRENOM') as string[]).length).toEqual(2);
298
- expect((variables.get('AGE') as string[]).length).toEqual(2);
299
- expect(spy).toHaveBeenLastCalledWith({
300
- name: 'AGE',
301
- value: [20, 40],
302
- cause: 'resizing',
303
- });
304
- });
305
-
306
284
  it('should resize pairwise with the array syntax', () => {
307
285
  variables.set('PRENOM', []);
308
286
  variables.set('LINKS', [[]]);
@@ -338,29 +316,7 @@ describe('lunatic-variables-store', () => {
338
316
  [null, null, null],
339
317
  ]);
340
318
  });
341
- it('should resize pairwise with the object syntax with index', () => {
342
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
343
- variables.set('LINKS', [
344
- [null, 2, 4],
345
- [1, null, 2],
346
- [3, 2, null],
347
- ]);
348
- resizingBehaviour(variables, {
349
- PRENOM: {
350
- sizeForLinksVariables: {
351
- xAxisSize: 'count(PRENOM)',
352
- yAxisSize: 'count(PRENOM)',
353
- },
354
- linksVariables: ['LINKS'],
355
- },
356
- });
357
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
358
- expect(variables.get('LINKS') as string[][]).toEqual([
359
- [null, 4],
360
- [3, null],
361
- ]);
362
- });
363
- it('should handle both: pairwise and normal resize', () => {
319
+ it('should handle both pairwise and normal resize', () => {
364
320
  variables.set('PRENOM', []);
365
321
  variables.set('NOM', []);
366
322
  variables.set('LINKS', [[]]);
@@ -380,29 +336,6 @@ describe('lunatic-variables-store', () => {
380
336
  ]);
381
337
  expect(variables.get('NOM') as string[]).toEqual([null, null, null]);
382
338
  });
383
- it('should handle both: pairwise and normal resize with index', () => {
384
- variables.set('PRENOM', ['John', 'Jane', 'Marc']);
385
- variables.set('AGE', [40, 30, 20]);
386
- variables.set('LINKS', [
387
- [null, 2, 4],
388
- [1, null, 2],
389
- [3, 2, null],
390
- ]);
391
- resizingBehaviour(variables, {
392
- PRENOM: {
393
- sizeForLinksVariables: ['count(PRENOM)', 'count(PRENOM)'],
394
- linksVariables: ['LINKS'],
395
- size: 'count(PRENOM)',
396
- variables: ['AGE'],
397
- },
398
- });
399
- variables.set('PRENOM', ['John', 'Marc'], { removedIndex: 1 });
400
- expect(variables.get('LINKS') as string[][]).toEqual([
401
- [null, 4],
402
- [3, null],
403
- ]);
404
- expect(variables.get('AGE') as string[]).toEqual([40, 20]);
405
- });
406
339
  });
407
340
 
408
341
  describe('cleaning', () => {
@@ -33,8 +33,6 @@ export type EventArgs = {
33
33
  value: unknown;
34
34
  /** Iteration changed (for array). */
35
35
  iteration?: IterationLevel | undefined;
36
- /** removedIndex: when resize an array directly with only one handleChange (remove one line in tableLoop) */
37
- removedIndex?: number;
38
36
  /** What triggered this change. */
39
37
  cause?: 'resizing' | 'cleaning';
40
38
  /** Extra sent when setting the variable. */
@@ -62,7 +60,7 @@ export class LunaticVariablesStore {
62
60
  if (!source.variables) {
63
61
  return store;
64
62
  }
65
- // Source data (picked from "variables" in the source.json)s
63
+ // Source data (picked from "variables" in the source.json)
66
64
  const sourceValues: Record<string, unknown> = {};
67
65
  // Starting data for the form (merged with data.json or injected data)
68
66
  const initialValues: Record<string, unknown> = {};
@@ -125,7 +123,7 @@ export class LunaticVariablesStore {
125
123
  public set(
126
124
  name: string,
127
125
  value: unknown,
128
- args: Pick<EventArgs['change'], 'iteration' | 'cause' | 'removedIndex'> = {}
126
+ args: Pick<EventArgs['change'], 'iteration' | 'cause'> = {}
129
127
  ): LunaticVariable {
130
128
  if (!this.dictionary.has(name)) {
131
129
  this.dictionary.set(
@@ -1 +1,2 @@
1
+ export { default as resizeArrayVariable } from './resize-array-variable';
1
2
  export * from './validate-condition-filter';
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Cast the variable into an array and adjust the length if necessary
3
+ */
4
+ function resizeArrayVariable<T = unknown>(
5
+ array: unknown,
6
+ length: number,
7
+ defaultValue?: T
8
+ ): T[] {
9
+ if (!Array.isArray(array)) {
10
+ // create the array
11
+ return new Array(length).fill(defaultValue);
12
+ } else if (array.length !== length) {
13
+ // renew array end keep previous values
14
+ return new Array(length).fill(defaultValue).reduce(function (
15
+ step,
16
+ current,
17
+ index
18
+ ) {
19
+ if (index < array.length) {
20
+ return [...step, array[index]];
21
+ }
22
+ return [...step, current];
23
+ }, []);
24
+ }
25
+ return array;
26
+ }
27
+
28
+ export default resizeArrayVariable;