Step by varargs[Value, toPreserves]
This commit is contained in:
parent
a83c9ad3a4
commit
a52e84dd70
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "20240101"
|
version = "20240102"
|
||||||
author = "Emery Hemingway"
|
author = "Emery Hemingway"
|
||||||
description = "data model and serialization format"
|
description = "data model and serialization format"
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
|
|
|
@ -816,14 +816,7 @@ when isMainModule:
|
||||||
var pr = t.toPreservesHook()
|
var pr = t.toPreservesHook()
|
||||||
assert fromPreservesHook(t, pr)
|
assert fromPreservesHook(t, pr)
|
||||||
|
|
||||||
func step*(pr, idx: Value): Option[Value] =
|
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)
|
|
||||||
if pr.isDictionary:
|
if pr.isDictionary:
|
||||||
for (k, v) in pr.dict.items:
|
for (k, v) in pr.dict.items:
|
||||||
if k == idx:
|
if k == idx:
|
||||||
|
@ -836,8 +829,14 @@ func step*(pr, idx: Value): Option[Value] =
|
||||||
if i < pr.len:
|
if i < pr.len:
|
||||||
result = some(pr[i])
|
result = some(pr[i])
|
||||||
|
|
||||||
func step*(pr: Value; path: varargs[Value]): Option[Value] =
|
func step*(pr: Value; path: varargs[Value, toPreserves]): Option[Value] =
|
||||||
## Step into `pr` by indexes at `path`.
|
## 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)
|
result = some(pr)
|
||||||
for index in path:
|
for index in path:
|
||||||
if result.isSome:
|
if result.isSome:
|
||||||
|
|
Loading…
Reference in New Issue