mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-22 00:41:28 +00:00
add [1..2] for JArray (#18525)
* add [1..2] for JArray * fix BackwardsIndex to int * fix for BackwardsIndex * fix for assert node kind check * fix variable name * Update lib/pure/json.nim * fix for when x.a is BackwardsIndex Co-authored-by: itsumura-h <dumblepy@mail.com> Co-authored-by: Dominik Picheta <dominikpicheta@googlemail.com>
This commit is contained in:
@@ -531,6 +531,24 @@ proc `[]`*(node: JsonNode, index: BackwardsIndex): JsonNode {.inline, since: (1,
|
||||
|
||||
`[]`(node, node.len - int(index))
|
||||
|
||||
proc `[]`*[U, V](a: JsonNode, x: HSlice[U, V]): JsonNode =
|
||||
## Slice operation for JArray.
|
||||
##
|
||||
## Returns the inclusive range `[a[x.a], a[x.b]]`:
|
||||
runnableExamples:
|
||||
import json
|
||||
let arr = %[0,1,2,3,4,5]
|
||||
doAssert arr[2..4] == %[2,3,4]
|
||||
doAssert arr[2..^2] == %[2,3,4]
|
||||
doAssert arr[^4..^2] == %[2,3,4]
|
||||
|
||||
assert(a.kind == JArray)
|
||||
result = newJArray()
|
||||
let xa = (when x.a is BackwardsIndex: a.len - int(x.a) else: int(x.a))
|
||||
let L = (when x.b is BackwardsIndex: a.len - int(x.b) else: int(x.b)) - xa + 1
|
||||
for i in 0..<L:
|
||||
result.add(a[i + xa])
|
||||
|
||||
proc hasKey*(node: JsonNode, key: string): bool =
|
||||
## Checks if `key` exists in `node`.
|
||||
assert(node.kind == JObject)
|
||||
|
||||
Reference in New Issue
Block a user