summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorNeelesh Chandola <neelesh.chandola@outlook.com>2021-01-04 22:16:39 +0530
committerGitHub <noreply@github.com>2021-01-04 17:46:39 +0100
commit0d67ad0bf38a69d110558a6eaa525e25cbf90648 (patch)
treed46a4c9273fb765088365e544ebdbc44a1febf4d /lib
parent349574d5745944ed2183b196113026f804393f29 (diff)
downloadNim-0d67ad0bf38a69d110558a6eaa525e25cbf90648.tar.gz
Add backwards index overload for `[]` for JsonNode (#16501)
* Add backwards index overload for `[]` for JsonNode

* Add since

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* Add docs, example, and changelog

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/json.nim13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 063fad8b4..a9d0ed4cb 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -496,6 +496,19 @@ proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
   assert(node.kind == JArray)
   return node.elems[index]
 
+proc `[]`*(node: JsonNode, index: BackwardsIndex): JsonNode {.inline, since: (1, 5, 1).} =
+  ## Gets the node at `array.len-i` in an array through the `^` operator.
+  ##
+  ## i.e. `j[^i]` is a shortcut for `j[j.len-i]`.
+  runnableExamples:
+    let
+      j = parseJson("[1,2,3,4,5]")
+
+    doAssert j[^1].getInt == 5
+    doAssert j[^2].getInt == 4
+
+  `[]`(node, node.len - int(index))
+
 proc hasKey*(node: JsonNode, key: string): bool =
   ## Checks if `key` exists in `node`.
   assert(node.kind == JObject)