This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Parse.sh

108 lines
2 KiB
Bash
Raw Normal View History

2023-12-07 04:02:47 +03:00
# Parse data and output simplified format.
# Usage: parse_simplify <STRING>
2023-12-07 01:44:42 +03:00
function parse_simplify() {
2023-12-05 21:50:45 +03:00
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/_$//"
2023-10-30 03:49:10 +03:00
}
2023-12-17 21:35:02 +03:00
# Parse to PascalCase.
# Usage: parse_pascal <STRING>
function parse_pascal() {
2023-12-05 21:50:45 +03:00
local IFS=${IFS}_-
local parts=($(parse_simplify ${1}))
local result
2023-11-30 04:53:57 +03:00
2023-12-17 23:46:41 +03:00
# If already in Pascal.
if [[ "${#parts[@]}" = 1 ]]; then
echo "${parts[*]^}"
return 0
fi
2023-12-05 21:50:45 +03:00
for part in "${parts[@]}"; do
local word="${part,,}"
word="${word^}"
2023-12-05 21:50:45 +03:00
result="${result}${word}"
done
2023-12-05 21:50:45 +03:00
echo "${result}"
2023-11-30 04:53:57 +03:00
}
2023-12-17 20:08:31 +03:00
# 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#_}"
}
2023-12-17 21:35:02 +03:00
# Parse to kebab-case.
# Usage: parse_kebab <STRING>
function parse_kebab() {
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 to camelCase.
# Usage: parse_camel <STRING>
function parse_camel() {
local IFS=${IFS}_-
local parts=($(parse_simplify ${1}))
local result
2023-12-17 23:46:41 +03:00
# If already in camel.
if [[ "${#parts[@]}" = 1 ]]; then
echo "${parts[*],}"
return 0
fi
2023-12-17 21:35:02 +03:00
for part in "${parts[@]}"; do
local word="${part,,}"
word="${word^}"
result="${result}${word}"
done
echo "${result,}"
}
# Parse to SNAKE_CASE_UPPERCASE.
# Usage: parse_snake_uppercase <STRING>
function parse_snake_uppercase() {
local IFS=${IFS}_-
local parts=($(parse_simplify ${1}))
local result
for part in "${parts[@]}"; do
local word="${part^^}"
result="${result}_${word}"
done
echo "${result#_}"
}
2023-12-07 04:02:47 +03:00
# Parse data keeping only alphanumeric characters.
# Usage: parse_alnum <STRING>
2023-12-07 01:44:42 +03:00
function parse_alnum() {
2023-12-05 21:50:45 +03:00
echo "${*}" | \
sed -e "s/[^[:alnum:]]//g"
2023-10-30 03:49:10 +03:00
}