Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib-candidate] Generalized jq recurse() implementation #796

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib-candidate/std-rfc/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export module str/
# commands
export use fs.nu *
export use set-env.nu *
export use recurse.nu *
23 changes: 23 additions & 0 deletions stdlib-candidate/std-rfc/recurse.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export def main [
--filter: closure
]: any -> list<any> {
mut todo = [$in]
mut done = []
while ($todo | length) > 0 {
# pop
let value = $todo | last
$todo = ($todo | drop)
# save
if $filter == null or ($value | do $filter $value) {
$done ++= [$value]
}
# push
$todo ++= (match ($value | describe --detailed | get type) {
table => ($value | values | flatten)
record => ($value | values)
list => $value
_ => continue
} | reverse)
}
$done
}
1 change: 1 addition & 0 deletions stdlib-candidate/tests/mod.nu
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export module fs.nu
export module record.nu
export module recurse.nu
export module str_xpend.nu
32 changes: 32 additions & 0 deletions stdlib-candidate/tests/recurse.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std assert
use ../std-rfc recurse

const fixture = [
0
{ a: 1 }
{
b: [
2
[
[c d];
[3 4]
]
]
}
]

export def test [] {
assert equal ($fixture | recurse) [
$fixture # [0, {a: 1}, {b: [2, [[c, d]; [3, 4]]]}]
$fixture.0 # 0
$fixture.1 # {a: 1}
$fixture.1.a # 1
$fixture.2 # {b: [2, [[c, d]; [3, 4]]]}
$fixture.2.b # [2, [[c, d]; [3, 4]]]
$fixture.2.b.0 # 2
$fixture.2.b.1 # [c, d]; [3, 4]]
$fixture.2.b.1.0 # {c: 3, d: 4}
$fixture.2.b.1.0.c # 3
$fixture.2.b.1.0.d # 4
]
}