ntup{x,l,r} - tuples that can be nested and without quoting issues, base64 encoded elements
This commit is contained in:
parent
cba3556da8
commit
9c238777e1
2 changed files with 42 additions and 0 deletions
30
src/fun.sh
30
src/fun.sh
|
@ -286,6 +286,36 @@ tupr() {
|
|||
tupx 1- "$@" | last
|
||||
}
|
||||
|
||||
ntup() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
local arg
|
||||
read arg
|
||||
ntup $arg
|
||||
else
|
||||
list "$@" | map lambda x . 'echo "$x" | base64 --wrap=0 ; echo' | join , '(' ')'
|
||||
fi
|
||||
}
|
||||
|
||||
ntupx() {
|
||||
if [[ $# -eq 1 ]]; then
|
||||
local arg
|
||||
read arg
|
||||
ntupx "$1" "$arg"
|
||||
else
|
||||
local n=$1
|
||||
shift
|
||||
echo "$@" | stripl '(' | stripr ')' | cut -d',' -f${n} | tr , '\n' | map lambda x . 'echo "$x" | base64 -d'
|
||||
fi
|
||||
}
|
||||
|
||||
ntupl() {
|
||||
ntupx 1 "$@"
|
||||
}
|
||||
|
||||
ntupr() {
|
||||
ntupx 1- "$@" | last
|
||||
}
|
||||
|
||||
zip() {
|
||||
local list=$*
|
||||
cat - | while read x; do
|
||||
|
|
|
@ -69,4 +69,16 @@ testTupr() {
|
|||
assertEquals '5' "$(tup 5 | tupr)"
|
||||
}
|
||||
|
||||
testNTup() {
|
||||
assertEquals '(KFlRbz0sWWdvPSkK,Ywo=)' "$(ntup $(ntup a b) c)"
|
||||
assertEquals '(YQo=,Ygo=)' "$(ntupl '(KFlRbz0sWWdvPSkK,Ywo=)')"
|
||||
assertEquals 'a' "$(ntupl '(YQo=,Ygo=)')"
|
||||
assertEquals 'b' "$(ntupr '(YQo=,Ygo=)')"
|
||||
assertEquals 'c' "$(ntupr '(KFlRbz0sWWdvPSkK,Ywo=)')"
|
||||
assertEquals 'a' "$(ntup $(ntup a b) c | ntupx 1 | ntupx 1)"
|
||||
assertEquals 'b' "$(ntup $(ntup a b) c | ntupx 1 | ntupx 2)"
|
||||
assertEquals 'c' "$(ntup $(ntup a b) c | ntupx 2)"
|
||||
assertEquals 'a b' "$(ntup $(ntup a b) c | ntupx 1 | ntupx 1,2 | unlist)"
|
||||
}
|
||||
|
||||
. ./shunit2-init.sh
|
||||
|
|
Loading…
Reference in a new issue