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

# 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 PascalCase.
# Usage: parse_pascal <STRING>
function parse_pascal() {
local IFS=${IFS}_-
local parts=($(_get_parts ${*}))
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=($(_get_parts ${*}))
local result
for part in "${parts[@]}"; do
local word="${part,,}"
result="${result}_${word}"
done
echo "${result#_}"
}
# Parse to kebab-case.
# Usage: parse_kebab <STRING>
function parse_kebab() {
local IFS=${IFS}_-
local parts=($(_get_parts ${*}))
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=($(_get_parts ${*}))
local result
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=($(_get_parts ${*}))
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"
}
# Get name parts.
# Usage: _get_parts <STRING>
function _get_parts() {
parse_simplify "${*}" | _split_uppercase
}
# Split string by uppercase letters.
# Usage: echo | _split_uppercase
function _split_uppercase() {
sed -e "s/[A-Z]\+/\n&/g"
}