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>
This commit is contained in:
parent
349574d574
commit
0d67ad0bf3
2 changed files with 15 additions and 0 deletions
|
|
@ -89,6 +89,8 @@
|
||||||
- Added `posix_utils.osReleaseFile` to get system identification from `os-release` file on Linux and the BSDs.
|
- Added `posix_utils.osReleaseFile` to get system identification from `os-release` file on Linux and the BSDs.
|
||||||
https://www.freedesktop.org/software/systemd/man/os-release.html
|
https://www.freedesktop.org/software/systemd/man/os-release.html
|
||||||
|
|
||||||
|
- Added `BackwardsIndex` overload for `JsonNode`.
|
||||||
|
|
||||||
|
|
||||||
## Language changes
|
## Language changes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -496,6 +496,19 @@ proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
|
||||||
assert(node.kind == JArray)
|
assert(node.kind == JArray)
|
||||||
return node.elems[index]
|
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 =
|
proc hasKey*(node: JsonNode, key: string): bool =
|
||||||
## Checks if `key` exists in `node`.
|
## Checks if `key` exists in `node`.
|
||||||
assert(node.kind == JObject)
|
assert(node.kind == JObject)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue