Skip to content

Commit

Permalink
fix: correct list indexing with variables
Browse files Browse the repository at this point in the history
Closes #59
  • Loading branch information
renovate[bot] authored and nikku committed Feb 7, 2024
1 parent 29f65ec commit b1684ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
getType,
isDuration,
isDateTime,
isType
isType,
isNumber
} from './types';

import {
Expand Down Expand Up @@ -704,6 +705,20 @@ function evalNode(node: SyntaxNodeRef, input: string, args: any[]) {
return null;
}

// a[variable=number]
if (typeof filterFn.type === 'undefined') {
try {
const value = filterFn(context);

if (isNumber(value)) {
filterFn.type = 'number';
}
} catch (err) {

// ignore
}
}

// a[1]
if (filterFn.type === 'number') {
const idx = filterFn(context);
Expand Down
15 changes: 14 additions & 1 deletion test/interpreter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,20 @@ describe('interpreter', function() {

expr('a[1]', null);

expr('[][a]', [], { a: 1 });
expr('[][a]', null, { a: 1 });

expr('[][a]', [], { a: '1' });

expr('["A", "B", "C"][i]', 'A', { i: 1 });
expr('["A", "B", "C"][i]', 'C', { i: -1 });

expr('["A", "B", "C"][a + b]', 'B', { a: 1, b: 1 });
expr('["A", "B", "C"][a - b]', 'B', { a: 3, b: 1 });

expr('["A", "B", "C"][a()]', 'B', {
a() { return 2; }
});

});


Expand Down

0 comments on commit b1684ef

Please sign in to comment.