-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
# $2: String separator, has to be a single element | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're making 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better name for the |
||
local IFS | ||
|
||
default="$1"; shift | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 # | ||
################ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" ] | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
There was a problem hiding this comment.
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).