dgilland / pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.
http://pydash.readthedocs.io
MIT License
1.32k stars 93 forks source link

Negative Indexes Support #181

Closed bl4ckst0ne closed 1 year ago

bl4ckst0ne commented 1 year ago

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.

coveralls commented 1 year ago

Coverage Status

Coverage: 100.0%. Remained the same when pulling 6928a766b259ed2e4b763976fb01db6903631d13 on bl4ckst0ne:feature/negative-indexes into f2740b55e59defb87ed6f1faec48b1c3b15565e3 on dgilland:develop.

dgilland commented 1 year ago

Thanks for working on this!