From 95ef1b369941cbf0c7c86634d3b2e4aa44a94141 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jan 2024 13:43:58 -0500 Subject: cleaner evals --- wordpress/export-json.bash | 54 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'wordpress/export-json.bash') diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash index 5404ceb..f146c19 100644 --- a/wordpress/export-json.bash +++ b/wordpress/export-json.bash @@ -98,28 +98,13 @@ for_each() while [ $# -ge 2 ] do $1 "$2" || return - # This just removes $2 from the arglist: - eval 'shift 2; set -- '"${1@Q}"' "$@"' || return + set -- "$1" "${@:3}" done } -# IDEA: bash module w/ some simple FORTH style -# point-free ops on arglist. To eliminate the -# evals. Of course, there is not much use for -# point-free code like this, it only makes -# sense if you care about the code providing a -# pristine environment for the callback -- which -# only matters for highly general library style -# code meant to be reused in unknown future -# environments. - export_to_env0() { - while [ $# -gt 0 ] - do - arg1_to_env0 "$1" || return - shift - done + for_each arg1_to_env0 "$@" } to_JSON_all() @@ -139,8 +124,8 @@ jq_zip2() cat <<'END' $ARGS.positional | [ - .[ : length/2 ], - .[ length/2 : ] + .[ length/2 : ], + .[ : length/2 ] ] | transpose | map ({ (.[0]): .[1] }) | @@ -150,21 +135,30 @@ END json_encode_associative_array() { - eval 'set -- --arg k "'"$1"'" --args \ - "${!'"$1"'[@]}" \ - "${'"$1"'[@]}"' && - if [ $# -gt 4 ] + eval \ + 'set -- "$1" \ + "${'"$1"'[@]}" \ + "${!'"$1"'[@]}"; + ' && + if [ $# -ge 3 ] then - jq -n "{ (\$k): $(jq_zip2) }" "$@" + jq -n "{ (\$k): $(jq_zip2) }" \ + --arg k "$1" \ + --args "${@:2}" else - jq -n '{ ($k): {} }' "$@" + jq -n '{ ($k): {} }' --arg k "$1" fi } json_encode_indexed_array() { - eval 'set -- '"${1@Q}"' --args "${'"$1"'[@]}"' && - jq -n '{ ($k): $ARGS.positional }' --arg k "$@" + eval \ + 'set -- "$1" \ + "${'"$1"'[@]}" \ + ' && + jq -n '{ ($k): $ARGS.positional }' \ + --arg k "$1" \ + --args "${@:2}" } # This uses the more complex implementation that, @@ -313,3 +307,9 @@ runtest() # hackers # # atomized castaways of aging social bodies +# reviving ancient code bodies encoding ancient +# knowns into own bodies endomorphing endorphins +# of discovery over arrows of time chance and +# finger dance into machine flow that won't halt +# or falter to a byzantine emperors structures +# span relativities messages -- cgit v1.2.3