Skip to content

Conversation

@rabbbit
Copy link

@rabbbit rabbbit commented Jul 18, 2019

Skips empty arguments.
Handles defaults.
Has tests.

rabbbit added 2 commits July 19, 2019 01:15
Skips empty arguments.
Handles defaults.
Has tests.
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

#
# $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).

# $1: String separator
# $@: Array elements
# $1: String default, used in case array to join is empty
# $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).

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.


[ "$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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants