Skip to content

Add a better version of join. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,34 @@ array_filter() {
#
# Join array elements with a string.
#
# $1: String separator
# $@: Array elements
# $1: String default, used in case array to join is empty
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the principle of do one thing and do it well, can you remove the "default"?

I feel this is conflating two responsibilities into one function (join should just join, nothing more, nothing less).

# $2: String separator, has to be a single element
Copy link
Owner

@dansimau dansimau Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're making array_join better, why don't we improve it to allow any join string, rather than just a single character?

I can imagine this is useful, e.g. say you want to create a human-readable CSV, so you want to join by ", " (comma-space).

# $@: 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name for the ifs variable here would be join_string.

local IFS

default="$1"; shift

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line

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 #
################
Expand Down
64 changes: 64 additions & 0 deletions test-bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,70 @@ 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_no_delimeter() {
local result

result=$(join "def")

[ "$result" == "def" ]
}

test_join_default() {
local result

result=$(join "def" - '' '')

[ "$result" == "def" ]
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the following test for joining an element containing a newline?

test_join_multiline_string_element() {
    local result
    local -a test_input=(
        "foo"
        "bar"
        "omg"$'\n'"baz"
    )

    result=$(join "def" "," "${test_input[@]}")

    [ "$result" == "foo,bar,omg"$'\n'"baz" ]
}

The current code fails this test.

tests() {
local ret=0
local test_log="/tmp/${0##*/}.test.log"
Expand Down