Hey,
I've added support for negative indexes. Let me know if there are any problems with that. 😉
Feature Overview
Currently, negative integer indexes are not supported. It might be useful for obtaining one of the last elements when the list length is unknown. For example, when parsing a payload from the network / user input. In my case, it was a sorted list from the network that I wanted the second item from the end.
A basic usage might look like this: for {'a': [1,2,3]}, the path a.[-1] should return 3.
Implementation
To support negative indexes, to_path should parse [negative index] as a number. Thus, we need to alter just the regex.
The change is pretty small: just add -? before \d+. That way the minus sign is optional, so both positive and negative numbers will be matched.
Tests
I've added cases with negative indexes for to_path function as well as get function.
Coverage: 100.0%. Remained the same when pulling 6928a766b259ed2e4b763976fb01db6903631d13 on bl4ckst0ne:feature/negative-indexes into f2740b55e59defb87ed6f1faec48b1c3b15565e3 on dgilland:develop.
Hey, I've added support for negative indexes. Let me know if there are any problems with that. 😉
Feature Overview
Currently, negative integer indexes are not supported. It might be useful for obtaining one of the last elements when the list length is unknown. For example, when parsing a payload from the network / user input. In my case, it was a sorted list from the network that I wanted the second item from the end.
A basic usage might look like this: for
{'a': [1,2,3]}
, the patha.[-1]
should return 3.Implementation
To support negative indexes,
to_path
should parse[negative index]
as a number. Thus, we need to alter just the regex. The change is pretty small: just add-?
before\d+
. That way the minus sign is optional, so both positive and negative numbers will be matched.Tests
I've added cases with negative indexes for
to_path
function as well asget
function.