-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__string_functions.sh
executable file
·47 lines (44 loc) · 2.33 KB
/
__string_functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __camelcase_split
# DESCRIPTION: Convert 'CamelCased' strings to 'Camel Cased'
# USAGE: __camelcase_split "SomeStringWithCamelCase"
# RETURNS: String with camelcase separate arguments
#----------------------------------------------------------------------------------------------------------------------
__camelcase_split() {
# shellcheck disable=SC2001
echo "$*" | sed -e 's/\([^[:upper:][:punct:]]\)\([[:upper:]]\)/\1 \2/g'
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __join_array_by_char
# DESCRIPTION: Join an array of strings by some character
# USAGE: export SOMEARRAY=("some" "array" "with" "items")
# __join_array_by_char ";" "${SOMEARRAY[@]}"
# RETURNS: One string with all array elements joined by the character specified
#----------------------------------------------------------------------------------------------------------------------
__join_array_by_char() {
if [ $# -ge 3 ]; then
local IFS="${1}"
fi
shift
echo "$*"
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __strip_duplicates
# DESCRIPTION: Strip duplicate strings
# USAGE: __strip_duplicates "some some string string with duplicates"
# RETURNS: String without duplicates (i.e. "some string with duplicates")
#----------------------------------------------------------------------------------------------------------------------
__strip_duplicates() {
echo "$*" | tr -s '[:space:]' '\n' | awk '!x[$0]++'
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __unquote_string
# DESCRIPTION: Strip single or double quotes from the provided string
# USAGE: __unquote_string ""Some" "string""
# RETURNS: Message without quotes in string (Some string)
#----------------------------------------------------------------------------------------------------------------------
__unquote_string() {
# shellcheck disable=SC2001
echo "$*" | sed -e "s/^\([\"\']\)\(.*\)\1\$/\2/g"
}