[nushell]
change
commit 1d30c3e7aa97ce3931487fd9ea185972b385daae
author Alpha Chen <alpha@kejadlen.dev>
date
parent 676cce2c
diff --git a/jj-lfs-test/a.jpg b/jj-lfs-test/a.jpg
new file mode 100644
index 0000000..e69de29
diff --git a/nushell/README.md b/nushell/README.md
new file mode 100644
index 0000000..c585de6
--- /dev/null
+++ b/nushell/README.md
@@ -0,0 +1,100 @@
+Testing out using nushell as a swiss army knife for data manipulation by
+translating these jq exercises to nushell:
+
+https://github.com/qmacro/level-up-your-json-fu-with-jq/blob/main/talknotes.md
+
+```
+pbpaste | from json
+
+pbpaste | save environment.json
+
+# jq '.availableEnvironments|length' environments.json
+open environment.json | get availableEnvironments | length
+
+# jq -r '.[].name' offerings.json
+open offerings.json | get name | to text
+
+# jq 'map({(.name): .roleReferences|length})|add' collections.json
+open collections.json | reduce -f {} {|it,acc| $acc | merge {$it.name: ($it.roleReferences | length)}}
+
+# jq '.datacenters[]|select(.iaasProvider == "AZURE").displayName' regions.json
+open regions.json | get datacenters | where iaasProvider == AZURE | get displayName
+
+# jiq
+echo '' | fzf-tmux -p 80% --preview 'nu -c "open regions.json | $(echo {q})"'
+
+# cat threestrings.dat | jq '.'
+open threestrings.dat | lines | each {|| from json}
+
+# cat threestrings.dat | jq 'length'
+open threestrings.dat | lines | each {|| from json | size | get chars}
+
+# cat threestrings.dat | jq -s '.'
+open threestrings.dat | lines
+
+# cat threestrings.dat | jq -s 'length'
+open threestrings.dat | lines | length
+
+# cat threestrings.dat | jq 'select(startswith("s"))|ascii_upcase'
+open threestrings.dat | lines | each {|| from json} | where {|| str starts-with s} | str upcase
+
+# cat threestrings.dat | jq -r -s '@csv'
+open threestrings.dat | lines | each {|| from json} | wrap x | transpose -i | to csv -n
+
+# cat threestrings.dat | jq -s '{words:., chars:map(length)|add}'
+open threestrings.dat | lines | each {|| from json} | {words: $in, chars: ($in | size | get chars | math sum)}
+
+# jq '.sessions[].speaker' btpcon.json
+open btpcon.json | get sessions.speaker
+
+# jq '.sessions | last | [.title, .starttime[:2]]' btpcon.json
+open btpcon.json | get sessions | last | select title starttime | update starttime {|x| $x.starttime | str substring 0..2}
+
+# def ampm:
+#   if .starttime[:2] | tonumber < 12
+#   then "morning"
+#   else "afternoon"
+#   end
+# ;
+def ampm [session] {
+  if ($session.starttime | str substring 0..2 | into int ) < 12 {
+    "morning"
+  } else {
+    "afternoon"
+  }
+}
+
+# jq 'import "lib" as lib; .sessions | map(.when = lib::ampm)' btpcon.json
+open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}}
+
+# cat btpcon.json \
+# | jq '
+#     import "lib" as lib;
+#     .sessions
+#     | map(.when = lib::ampm)
+#     | group_by(.when)
+#     | reverse
+#     | map({
+#         (first.when): map(.title)
+#       })
+#   '
+
+# this one returns the same structure as the jq snippet (single-element array)
+open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | update cells {|| get title}
+# to unwrap the array
+open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | update cells {|| get title} | get 0
+# an alternative?
+open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | transpose key value | each {|| select key value.title } | transpose -r | into record
+
+# cat btpcon.json \
+# | jq '
+#   import "lib" as lib;
+#   .sessions[]
+#   | select(.type=="talk" and lib::ampm=="afternoon")
+#   | .speaker
+#   | split(" ")
+#   | [last, first]
+#   | join(", ")
+#   '
+open btpcon.json | get sessions | where {|x| ($x.type == talk) and ((ampm $x) == afternoon)} | get speaker | each {|| split words | reverse | str join ", "}
+```