Skip to content

Commit be9e7e3

Browse files
committed
✨ added string::join
1 parent 04dc9c9 commit be9e7e3

4 files changed

Lines changed: 124 additions & 12 deletions

File tree

libraries.d/lib-string

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ function string::getFormattedHeader() {
661661

662662
if ((availableSpaceForLeft - leftPartLength >= 0)); then
663663
if ((middlePartLength > 0)); then
664-
padding="${spaces:0:availableSpaceForLeft - leftPartLength}"
664+
padding="${spaces:0:availableSpaceForLeft-leftPartLength}"
665665
finalString="${leftPart}${paddingStyle}${padding//" "/"${paddingChar}"}${paddingStyleReset}${finalString}"
666666
else
667667
finalString="${leftPart}"
@@ -690,7 +690,7 @@ function string::getFormattedHeader() {
690690
fi
691691

692692
if ((availableSpaceForRight - rightPartLength >= 0)); then
693-
padding="${spaces:0:availableSpaceForRight - rightPartLength}"
693+
padding="${spaces:0:availableSpaceForRight-rightPartLength}"
694694
finalString+="${paddingStyle}${padding//" "/"${paddingChar}"}${paddingStyleReset}${rightPart}"
695695
remainingWidth=$((remainingWidth - rightPartLength))
696696
elif [[ ${noEllipsis} != "true" && availableSpaceForRight -eq 1 ]]; then
@@ -751,11 +751,11 @@ function string::truncateWithEllipsis() {
751751

752752
# shortcut if no invisible characters
753753
if [[ ${inputString_NameRef} != *$'\e'* ]]; then
754-
if (( ${#inputString_NameRef} > maxLength )); then
754+
if ((${#inputString_NameRef} > maxLength)); then
755755
if [[ ${noEllipsis} == "true" ]]; then
756756
inputString_NameRef="${inputString_NameRef:0:maxLength}"
757757
else
758-
inputString_NameRef="${inputString_NameRef:0:maxLength - 1}"
758+
inputString_NameRef="${inputString_NameRef:0:maxLength-1}"
759759
fi
760760
REPLY=0
761761
else
@@ -799,4 +799,47 @@ function string::truncateWithEllipsis() {
799799
done
800800

801801
REPLY=$((maxLength - displayedLength))
802-
}
802+
}
803+
804+
# ## string::join
805+
#
806+
# Join an array of strings into a single string using a separator.
807+
#
808+
# - $1: **array variable name** _as string_:
809+
# The variable name that contains the array to join.
810+
# - ${separator} _as string_:
811+
# The separator character to use.
812+
# (defaults to $'\n')
813+
#
814+
# Returns:
815+
# - ${REPLY}: the joined string
816+
#
817+
# ```bash
818+
# MY_ARRAY=("name" "first_name" "address")
819+
# string::join MY_ARRAY separator=","
820+
# echo "${REPLY}"
821+
# ```
822+
function string::join() {
823+
local -n inputArray_NameRef="${1?"The function ⌜${FUNCNAME:-?}⌝ requires more than $# arguments."}"
824+
local \
825+
separator=$'\n' \
826+
IFS=$' '
827+
shift 1
828+
eval "local a= ${*@Q}"
829+
if ((${#separator} == 1)); then
830+
# optimization for single character separator
831+
IFS="${separator}"
832+
REPLY="${inputArray_NameRef[*]}"
833+
else
834+
REPLY=""
835+
local item
836+
local -i index=0
837+
for item in "${inputArray_NameRef[@]}"; do
838+
if ((index > 0)); then
839+
REPLY+="${separator}"
840+
fi
841+
REPLY+="${item}"
842+
((index++))
843+
done
844+
fi
845+
}

tests.d/lib-string/00.tests.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
source string
55

66
function main() {
7+
test_string::join
78
test_string::truncateWithEllipsis
89
test_string::getFormattedHeader
910
test_string::get KebabCase
@@ -26,6 +27,18 @@ function main() {
2627
test_string::doForEachLine
2728
}
2829

30+
function test_string::join() {
31+
test::title "✅ Testing string::join"
32+
33+
local -a myArray
34+
myArray=(one two 3 four 5)
35+
test::printVars myArray
36+
test::func string::join myArray
37+
test::func string::join myArray separator='@'
38+
test::func string::join myArray separator=''
39+
test::func string::join myArray separator=$'\n - '
40+
}
41+
2942
function test_string::truncateWithEllipsis() {
3043
test::title "✅ Testing string::truncateWithEllipsis"
3144

@@ -38,7 +51,7 @@ function test_string::truncateWithEllipsis() {
3851
test::funcWithInPlaceString string::truncateWithEllipsis 'This is a test string' maxLength=1
3952
test::funcWithInPlaceString string::truncateWithEllipsis 'This is a test string' maxLength=2
4053

41-
for ((i=6; i>=0; i--)); do
54+
for ((i = 6; i >= 0; i--)); do
4255
local myString="${ESC__FG_BRIGHT_CYAN}a${ESC__FG_BRIGHT_CYAN}b${ESC__FG_BRIGHT_CYAN}c${ESC__FG_BRIGHT_CYAN}d${ESC__FG_BRIGHT_CYAN}e"
4356
string::truncateWithEllipsis myString maxLength="${i}"
4457
echo "REPLY=${REPLY}${myString}"
@@ -60,13 +73,13 @@ function test_string::getFormattedHeader() {
6073
test_printFormattedHeader "left||" width=50
6174
test_printFormattedHeader "||right" width=50
6275

63-
for ((i=20; i>=0; i--)); do
76+
for ((i = 20; i >= 0; i--)); do
6477
test_printFormattedHeader "${ESC__FG_BRIGHT_CYAN}le${ESC__FG_BRIGHT_CYAN}ft|${ESC__FG_BRIGHT_CYAN}middle|ri${ESC__FG_BRIGHT_CYAN}gh${ESC__FG_BRIGHT_CYAN}t" width="${i}"
6578
done
66-
for ((i=20; i>=0; i--)); do
79+
for ((i = 20; i >= 0; i--)); do
6780
test_printFormattedHeader "left|middle|right" width="${i}" paddingStyle=$'\e[1;34m' paddingStyleReset=$'\e[0m'
6881
done
69-
for ((i=20; i>=0; i--)); do
82+
for ((i = 20; i >= 0; i--)); do
7083
test_printFormattedHeader "left|middle|right" width="${i}" noEllipsis=true
7184
done
7285

@@ -250,7 +263,7 @@ function test_string::wrapWords() {
250263
function test_string::wrapCharacters() {
251264
test::title "✅ Testing string::wrapCharacters"
252265

253-
test::markdown "Wrapping characters at column 20 with padding of 3 on all lines"
266+
test::markdown "Wrapping characters at column 20 with padding of 3 on all lines"
254267
test::func string::wrapCharacters MULTI_LINES_TEXT width=20 newLinePadString=" " firstLineWidth=17
255268

256269
test::markdown "Wrapping characters at 20, no other options"
@@ -347,4 +360,4 @@ MULTI_LINES_TEXT3="1 line one
347360
3 line three
348361
4 line four"
349362

350-
main
363+
main

tests.d/lib-string/results.approved.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,62 @@
22

33
## Test script 00.tests
44

5+
### ✅ Testing string::join
6+
7+
```text
8+
myArray=(
9+
[0]='one'
10+
[1]='two'
11+
[2]='3'
12+
[3]='four'
13+
[4]='5'
14+
)
15+
```
16+
17+
`string::join myArray`
18+
19+
Returned variables:
20+
21+
```text
22+
REPLY='one
23+
two
24+
3
25+
four
26+
5'
27+
```
28+
29+
`string::join myArray separator=@`
30+
31+
Returned variables:
32+
33+
```text
34+
REPLY='one@two@3@four@5'
35+
```
36+
37+
`string::join myArray separator=`
38+
39+
Returned code: `1`
40+
41+
Returned variables:
42+
43+
```text
44+
REPLY='onetwo3four5'
45+
```
46+
47+
`string::join myArray $'separator=\n - '`
48+
49+
Returned code: `1`
50+
51+
Returned variables:
52+
53+
```text
54+
REPLY='one
55+
- two
56+
- 3
57+
- four
58+
- 5'
59+
```
60+
561
### ✅ Testing string::truncateWithEllipsis
662

763
```text

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.36.5
1+
0.36.9

0 commit comments

Comments
 (0)