50 lines
995 B
Bash
50 lines
995 B
Bash
# Parse data and output simplified format.
|
|
# Usage: parse_simplify <STRING>
|
|
function parse_simplify() {
|
|
echo "${*}" | \
|
|
sed -e "s/ /_/g" \
|
|
-e "s/[^[:alnum:]_-]//g" \
|
|
-e "s/_\+/_/g" -e "s/-\+/-/g" \
|
|
-e "s/_-/_/g" -e "s/-_/_/g" \
|
|
-e "s/_\+/_/g" \
|
|
-e "s/^_//" -e "s/_$//"
|
|
}
|
|
|
|
# Parse to CamelCase.
|
|
# Usage: parse_camel <STRING>
|
|
function parse_camel() {
|
|
local IFS=${IFS}_-
|
|
local parts=($(parse_simplify ${1}))
|
|
local result
|
|
|
|
for part in "${parts[@]}"; do
|
|
local word="${part,,}"
|
|
word="${word^}"
|
|
result="${result}${word}"
|
|
done
|
|
|
|
echo "${result}"
|
|
}
|
|
|
|
# Parse to snake_case.
|
|
# Usage: parse_snake <STRING>
|
|
function parse_snake() {
|
|
local IFS=${IFS}_-
|
|
local parts=($(parse_simplify ${1}))
|
|
local result
|
|
|
|
for part in "${parts[@]}"; do
|
|
local word="${part,,}"
|
|
result="${result}_${word}"
|
|
done
|
|
|
|
echo "${result#_}"
|
|
}
|
|
|
|
# Parse data keeping only alphanumeric characters.
|
|
# Usage: parse_alnum <STRING>
|
|
function parse_alnum() {
|
|
echo "${*}" | \
|
|
sed -e "s/[^[:alnum:]]//g"
|
|
}
|