New implementation of tup/tupx/tupr

This commit is contained in:
Sławomir Śledź 2018-03-10 14:12:05 +01:00
parent b5a3c9eea0
commit 9502f7a8a2
2 changed files with 82 additions and 4 deletions

View file

@ -188,9 +188,19 @@ filter() {
done done
} }
stripl() {
local arg=$1
cat - | map lambda l . 'ret ${l##'$arg'}'
}
stripr() {
local arg=$1
cat - | map lambda l . 'ret ${l%%'$arg'}'
}
strip() { strip() {
local arg=$1 local arg=$1
cat - | map lambda l . 'ret ${l##'$arg'}' | map lambda l . 'ret ${l%%'$arg'}' cat - | stripl "$arg" | stripr "$arg"
} }
buff() { buff() {
@ -210,7 +220,7 @@ buff() {
} }
tup() { tup() {
list "$@" | join , '(' ')' list "$@" | map lambda x . 'echo ${x/,/u002c}' | join , '(' ')'
} }
tupx() { tupx() {
@ -221,7 +231,7 @@ tupx() {
else else
local n=$1 local n=$1
shift shift
list "$@" | strip '\(' | strip '\)' | unlist | cut -d',' -f${n} echo "$@" | stripl '(' | stripr ')' | cut -d',' -f${n} | tr ',' '\n' | map lambda x . 'echo ${x/u002c/,}'
fi fi
} }
@ -230,7 +240,7 @@ tupl() {
} }
tupr() { tupr() {
tupx 2 "$@" tupx 1- "$@" | last
} }
zip() { zip() {

68
test/tup_test.sh Executable file
View file

@ -0,0 +1,68 @@
#! /bin/bash
testTupIfEmpty() {
assertEquals '()' $(tup)
}
testTupIfOneElement() {
assertEquals '(1)' $(tup 1)
assertEquals '(")' $(tup '"')
assertEquals "(')" $(tup "'")
assertEquals "(u002c)" $(tup ",")
assertEquals "(()" $(tup "(")
assertEquals "())" $(tup ")")
}
testTupHappyPath() {
assertEquals '(1,2,3,4,5)' $(tup 1 2 3 4 5)
assertEquals '(a-1,b-2,c-3)' $(tup 'a-1' 'b-2' 'c-3')
assertEquals '(a b,c d e,f)' "$(tup 'a b' 'c d e' 'f')"
}
testTupxHappyPath() {
assertEquals '4' $(tup 4 5 1 4 | tupx 1)
assertEquals '5' $(tup 4 5 1 4 | tupx 2)
assertEquals '1' $(tup 4 5 1 4 | tupx 3)
assertEquals '4' $(tup 4 5 1 4 | tupx 4)
}
testTupxIfEmpty() {
assertEquals '' "$(tup | tupx 1)"
assertEquals '' "$(tup | tupx 5)"
}
testTupxIfZeroIndex() {
assertEquals '' "$(tup 1 3 | tupx 0 2>/dev/null)"
}
testTupxIfSpecialChars() {
assertEquals ',' "$(tup ',' | tupx 1)"
assertEquals '(' "$(tup '(' | tupx 1)"
assertEquals ')' "$(tup ')' | tupx 1)"
assertEquals '()' "$(tup '()' | tupx 1)"
assertEquals '(' "$(tup '(' ')' | tupx 1)"
assertEquals '(' "$(tup '(' '(' | tupx 1)"
assertEquals ')' "$(tup ')' ')' | tupx 1)"
assertEquals ',' "$(tup 'u002c' | tupx 1)"
}
testTupxRange() {
assertEquals '4 5' "$(tup 4 5 1 4 | tupx 1-2 | unlist)"
assertEquals '4 4' "$(tup 4 5 1 4 | tupx 1,4 | unlist)"
assertEquals '4 5 4' "$(tup 4 5 1 4 | tupx 1,2,4 | unlist)"
}
testTupl() {
assertEquals '4' "$(tup 4 5 | tupl)"
assertEquals '4' "$(tup 4 5 6 | tupl)"
assertEquals '6' "$(tup 6 | tupl)"
}
testTupr() {
assertEquals '5' "$(tup 4 5 | tupr)"
assertEquals '5' "$(tup 1 4 5 | tupr)"
assertEquals '5' "$(tup 5 | tupr)"
}
. ./shunit2-init.sh