Skip to content

Commit

Permalink
Fixed negative list indices
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 28, 2023
1 parent b70dee6 commit f1c6a14
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/nodes/ListAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ test.each([

test.each([
['[1 2 3][2]', '2'],
['[1 2 3][5]', '2'],
['[1 2 3][0]', 'ø'],
['[1 2 3][-1]', '3'],
['[1 2 3][-3]', '1'],
['[1 2 3][-4]', 'ø'],
['[1 2 3][-4]', '3'],
])('Expect %s to be %s', (code, value) => {
expect(evaluateCode(code)?.toString()).toBe(value);
});
2 changes: 1 addition & 1 deletion src/values/ListValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class ListValue extends SimpleValue {
num === 0
? undefined
: this.values.at(
num > 0 ? (num - 1) % this.values.length : num
(num > 0 ? num - 1 : num) % this.values.length
);
return value === undefined ? new NoneValue(this.creator) : value;
}
Expand Down

0 comments on commit f1c6a14

Please sign in to comment.