Step by varargs[Value, toPreserves]
This commit is contained in:
parent
3ad3127a40
commit
9c7839d05a
|
@ -1,6 +1,6 @@
|
|||
# Package
|
||||
|
||||
version = "20240101"
|
||||
version = "20240102"
|
||||
author = "Emery Hemingway"
|
||||
description = "data model and serialization format"
|
||||
license = "Unlicense"
|
||||
|
|
|
@ -816,14 +816,7 @@ when isMainModule:
|
|||
var pr = t.toPreservesHook()
|
||||
assert fromPreservesHook(t, pr)
|
||||
|
||||
func step*(pr, idx: Value): Option[Value] =
|
||||
## Step into `pr` by index `idx`.
|
||||
## Works for sequences, records, and dictionaries.
|
||||
runnableExamples:
|
||||
import std/options
|
||||
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
|
||||
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
|
||||
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Value)
|
||||
func step(pr, idx: Value): Option[Value] =
|
||||
if pr.isDictionary:
|
||||
for (k, v) in pr.dict.items:
|
||||
if k == idx:
|
||||
|
@ -836,8 +829,14 @@ func step*(pr, idx: Value): Option[Value] =
|
|||
if i < pr.len:
|
||||
result = some(pr[i])
|
||||
|
||||
func step*(pr: Value; path: varargs[Value]): Option[Value] =
|
||||
## Step into `pr` by indexes at `path`.
|
||||
func step*(pr: Value; path: varargs[Value, toPreserves]): Option[Value] =
|
||||
## Step into `pr` by index `idx`.
|
||||
## Works for sequences, records, and dictionaries.
|
||||
runnableExamples:
|
||||
import std/options
|
||||
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
|
||||
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
|
||||
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Value)
|
||||
result = some(pr)
|
||||
for index in path:
|
||||
if result.isSome:
|
||||
|
|
Loading…
Reference in New Issue