summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-26 13:43:58 -0500
committerroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-26 13:43:58 -0500
commit95ef1b369941cbf0c7c86634d3b2e4aa44a94141 (patch)
tree17d0fed084120f35416795cd5127d262e3a8a0d6
parentc1260ca34d9b41d617a4398759443a1df1abfab7 (diff)
cleaner evals
-rw-r--r--wordpress/export-json.bash54
1 files changed, 27 insertions, 27 deletions
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()
98 while [ $# -ge 2 ] 98 while [ $# -ge 2 ]
99 do 99 do
100 $1 "$2" || return 100 $1 "$2" || return
101 # This just removes $2 from the arglist: 101 set -- "$1" "${@:3}"
102 eval 'shift 2; set -- '"${1@Q}"' "$@"' || return
103 done 102 done
104} 103}
105 104
106# IDEA: bash module w/ some simple FORTH style
107# point-free ops on arglist. To eliminate the
108# evals. Of course, there is not much use for
109# point-free code like this, it only makes
110# sense if you care about the code providing a
111# pristine environment for the callback -- which
112# only matters for highly general library style
113# code meant to be reused in unknown future
114# environments.
115
116export_to_env0() 105export_to_env0()
117{ 106{
118 while [ $# -gt 0 ] 107 for_each arg1_to_env0 "$@"
119 do
120 arg1_to_env0 "$1" || return
121 shift
122 done
123} 108}
124 109
125to_JSON_all() 110to_JSON_all()
@@ -139,8 +124,8 @@ jq_zip2()
139 cat <<'END' 124 cat <<'END'
140$ARGS.positional | 125$ARGS.positional |
141[ 126[
142 .[ : length/2 ], 127 .[ length/2 : ],
143 .[ length/2 : ] 128 .[ : length/2 ]
144] | 129] |
145transpose | 130transpose |
146map ({ (.[0]): .[1] }) | 131map ({ (.[0]): .[1] }) |
@@ -150,21 +135,30 @@ END
150 135
151json_encode_associative_array() 136json_encode_associative_array()
152{ 137{
153 eval 'set -- --arg k "'"$1"'" --args \ 138 eval \
154 "${!'"$1"'[@]}" \ 139 'set -- "$1" \
155 "${'"$1"'[@]}"' && 140 "${'"$1"'[@]}" \
156 if [ $# -gt 4 ] 141 "${!'"$1"'[@]}";
142 ' &&
143 if [ $# -ge 3 ]
157 then 144 then
158 jq -n "{ (\$k): $(jq_zip2) }" "$@" 145 jq -n "{ (\$k): $(jq_zip2) }" \
146 --arg k "$1" \
147 --args "${@:2}"
159 else 148 else
160 jq -n '{ ($k): {} }' "$@" 149 jq -n '{ ($k): {} }' --arg k "$1"
161 fi 150 fi
162} 151}
163 152
164json_encode_indexed_array() 153json_encode_indexed_array()
165{ 154{
166 eval 'set -- '"${1@Q}"' --args "${'"$1"'[@]}"' && 155 eval \
167 jq -n '{ ($k): $ARGS.positional }' --arg k "$@" 156 'set -- "$1" \
157 "${'"$1"'[@]}" \
158 ' &&
159 jq -n '{ ($k): $ARGS.positional }' \
160 --arg k "$1" \
161 --args "${@:2}"
168} 162}
169 163
170# This uses the more complex implementation that, 164# This uses the more complex implementation that,
@@ -313,3 +307,9 @@ runtest()
313# hackers 307# hackers
314# 308#
315# atomized castaways of aging social bodies 309# atomized castaways of aging social bodies
310# reviving ancient code bodies encoding ancient
311# knowns into own bodies endomorphing endorphins
312# of discovery over arrows of time chance and
313# finger dance into machine flow that won't halt
314# or falter to a byzantine emperors structures
315# span relativities messages