From c8867dcb77aa43c7199912c713a29b950cad4eaf Mon Sep 17 00:00:00 2001 From: Pawel Krolikowski Date: Fri, 19 Jul 2019 01:15:44 +0200 Subject: [PATCH 1/2] Add a better version of join. Skips empty arguments. Handles defaults. Has tests. --- bash.sh | 30 +++++++++++++++++++++++----- test-bash.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/bash.sh b/bash.sh index 907b628..efa446c 100755 --- a/bash.sh +++ b/bash.sh @@ -53,14 +53,34 @@ array_filter() { # # Join array elements with a string. # -# $1: String separator -# $@: Array elements +# $1: String default, used in case array is empty +# $2: String separator, has to be a single element +# $@: Array elements, empty elements will be skipped # -array_join() { - local sep=$1; shift - IFS=$sep eval 'echo "$*"' + +join() { + local -a args + local default + local ifs + local IFS + + default="$1"; shift + + ifs="$1"; shift + + [ "${#ifs}" -gt 1 ] && exit 1 + + # read into array, ifs is still a space + read -r -a args <<< "$*" + + IFS="$ifs" + # args will be unbound if no more arguments were passed in + # check for that, use default if necessary + # otherwise dump the string with overwritten ifs + [ -z "${args-}" ] && echo "$default" || echo "${args[*]}" } + ################ # Benchmarking # ################ diff --git a/test-bash.sh b/test-bash.sh index dfe5633..05d5737 100755 --- a/test-bash.sh +++ b/test-bash.sh @@ -44,6 +44,62 @@ test_resolve_symlinks() { [ "$(resolve_symlinks /tmp/1/2/foo)" == "/tmp/foo" ] } +test_join_standard() { + local result + + result=$(join "def" - 1 2 3) + + [ "$result" == "1-2-3" ] +} + +test_join_one() { + local result + + result=$(join "def" - 1) + + [ "$result" == "1" ] +} + +test_join_spaces() { + local result + + result=$(join "def" - 1 '' '' 3) + + [ "$result" == "1-3" ] +} + +test_join_more_spaces() { + local result + + result=$(join "def" - 1 3) + + [ "$result" == "1-3" ] +} + +test_join_other_delimeter() { + local result + + result=$(join "def" "b" 1 3) + + [ "$result" == "1b3" ] +} + +test_join_invalid_delimeter() { + local result + + result=$(join "def" "bb" 1 3) + + [ "$?" == 1 ] +} + +test_join_default() { + local result + + result=$(join "def" - '' '') + + [ "$result" == "def" ] +} + tests() { local ret=0 local test_log="/tmp/${0##*/}.test.log" From e496e63992da460ba36afa636a98a36c9cfaf0b7 Mon Sep 17 00:00:00 2001 From: Pawel Krolikowski Date: Fri, 19 Jul 2019 01:19:45 +0200 Subject: [PATCH 2/2] more tests --- bash.sh | 2 +- test-bash.sh | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/bash.sh b/bash.sh index efa446c..3d33bc8 100755 --- a/bash.sh +++ b/bash.sh @@ -53,7 +53,7 @@ array_filter() { # # Join array elements with a string. # -# $1: String default, used in case array is empty +# $1: String default, used in case array to join is empty # $2: String separator, has to be a single element # $@: Array elements, empty elements will be skipped # diff --git a/test-bash.sh b/test-bash.sh index 05d5737..b29301d 100755 --- a/test-bash.sh +++ b/test-bash.sh @@ -92,6 +92,14 @@ test_join_invalid_delimeter() { [ "$?" == 1 ] } +test_join_no_delimeter() { + local result + + result=$(join "def") + + [ "$result" == "def" ] +} + test_join_default() { local result