From 9d9daad724360e40e804f901f34a56daae4dd71d Mon Sep 17 00:00:00 2001 From: Bruno Marques Date: Fri, 11 Mar 2022 10:16:18 -0300 Subject: [PATCH] Allow negative segment indexes on Split --- README.rst | 2 +- jsonpath_ng/ext/string.py | 2 +- tests/test_jsonpath_rw_ext.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index ce9a19b..917c009 100644 --- a/README.rst +++ b/README.rst @@ -195,7 +195,7 @@ Extensions | sub | - $.field.`sub(/foo\\\\+(.*)/, \\\\1)` | +--------------+----------------------------------------------+ | split | - $.field.`split(+, 2, -1)` | -| | - $.field.`split(sep, segement, maxsplit)` | +| | - $.field.`split(sep, segment, maxsplit)` | +--------------+----------------------------------------------+ | sorted | - $.objects.`sorted` | | | - $.objects[\\some_field] | diff --git a/jsonpath_ng/ext/string.py b/jsonpath_ng/ext/string.py index 80ed890..5f2746e 100644 --- a/jsonpath_ng/ext/string.py +++ b/jsonpath_ng/ext/string.py @@ -16,7 +16,7 @@ SUB = re.compile("sub\(/(.*)/,\s+(.*)\)") -SPLIT = re.compile("split\((.),\s+(\d+),\s+(\d+|-1)\)") +SPLIT = re.compile("split\((.),\s+(-?\d+),\s+(\d+|-1)\)") STR = re.compile("str\(\)") diff --git a/tests/test_jsonpath_rw_ext.py b/tests/test_jsonpath_rw_ext.py index 163e1be..f1e52d4 100644 --- a/tests/test_jsonpath_rw_ext.py +++ b/tests/test_jsonpath_rw_ext.py @@ -291,6 +291,16 @@ class Testjsonpath_ng_ext(testscenarios.WithScenarios, data={'payload': "foo-bar-cat-bow"}, target=["cat-bow"] )), + ('split3', dict( + string='payload.`split(-, -2, -1)`', + data={'payload': "foo-bar-cat-bow"}, + target=["cat"] + )), + ('split4', dict( + string='payload.`split(-, -2, 2)`', + data={'payload': "foo-bar-cat-bow"}, + target=["bar"] + )), ('bug-#2-correct', dict( string='foo[?(@.baz==1)]',