Skip to content

Commit

Permalink
fix: GLOBAL_ITERATION_INDEX support for variable
Browse files Browse the repository at this point in the history
Fix #724
Fix #725
  • Loading branch information
Grafikart committed Oct 18, 2023
1 parent 2c8c08d commit 64a814e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ describe('lunatic-variables-store', () => {
variables.run('FIRSTNAME || " " || LASTNAME', { iteration: [1] })
).toEqual('Jane Doe');
});
it('should handle global iteration variable', () => {
variables.set('FIRSTNAME', ['John', 'Jane']);
variables.setCalculated(
'FULLNAME',
'FIRSTNAME || " " || cast(GLOBAL_ITERATION_INDEX, string)'
);
expect(variables.get('FULLNAME', [0])).toEqual('John 0');
expect(variables.get('FULLNAME', [1])).toEqual('Jane 1');
});
});

describe('resizing', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/use-lunatic/commons/variables/lunatic-variables-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { isNumber } from '../../../utils/number';

// Interpret counter, used for testing purpose
let interpretCount = 0;
// Special variable that will take the current iteration value
const iterationVariableName = 'GLOBAL_ITERATION_INDEX';

type IterationLevel = number[];
type EventArgs = {
Expand Down Expand Up @@ -295,6 +297,9 @@ class LunaticVariable {
try {
return Object.fromEntries(
this.getDependencies().map((dep) => {
if (dep === iterationVariableName && iteration) {
return [dep, iteration[0]];
}
const dependencyIteration =
isNumber(this.iterationDepth) && Array.isArray(iteration)
? [iteration[this.iterationDepth]]
Expand Down

0 comments on commit 64a814e

Please sign in to comment.