diff options
author | itsumura-h <39766805+itsumura-h@users.noreply.github.com> | 2021-07-21 22:03:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-21 15:03:48 +0200 |
commit | 58080525a1dcad23de6ad0cf9ca6988f103de350 (patch) | |
tree | f56d4f302cf8f461fbe70ab04beadd61c86b015d | |
parent | 880353c0ad9300f2543c1b16548b4f2ab6784183 (diff) | |
download | Nim-58080525a1dcad23de6ad0cf9ca6988f103de350.tar.gz |
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>
-rw-r--r-- | lib/pure/json.nim | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index d5bbb86b8..85c3393b2 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -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) |