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:
parent
880353c0ad
commit
58080525a1
1 changed files with 18 additions and 0 deletions
|
|
@ -531,6 +531,24 @@ proc `[]`*(node: JsonNode, index: BackwardsIndex): JsonNode {.inline, since: (1,
|
||||||
|
|
||||||
`[]`(node, node.len - int(index))
|
`[]`(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 =
|
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