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

172 lines
3.7 KiB
Bash
Raw Normal View History

2023-12-30 20:16:32 +03:00
_PARSE_ALLOWED_CHARS="_-"
_PARSE_SPLIT_CHARS="\.\ _-"
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-30 20:16:32 +03:00
echo "${*}" | sed \
-e "s/[${_PARSE_SPLIT_CHARS}]/_/g" \
-e "s/[^[:alnum:]${_PARSE_ALLOWED_CHARS}]//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() {
2024-01-04 20:59:16 +03:00
local parts=($(_get_parts $(parse_simplify "${*}")))
2023-12-05 21:50:45 +03:00
local result
2023-11-30 04:53:57 +03:00
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() {
2024-01-04 20:59:16 +03:00
local parts=($(_get_parts $(parse_simplify "${*}")))
2023-12-17 20:08:31 +03:00
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() {
2024-01-04 20:59:16 +03:00
local parts=($(_get_parts $(parse_simplify "${*}")))
2023-12-17 21:35:02 +03:00
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() {
2024-01-04 20:59:16 +03:00
local parts=($(_get_parts $(parse_simplify "${*}")))
2023-12-17 21:35:02 +03:00
local result
for part in "${parts[@]}"; do
local word="${part,,}"
word="${word^}"
result="${result}${word}"
done
echo "${result,}"
}
# Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.**
2023-12-17 21:35:02 +03:00
# Usage: parse_snake_uppercase <STRING>
function parse_snake_uppercase() {
2024-01-04 20:59:16 +03:00
local parts=($(_get_parts $(parse_simplify "${*}")))
2023-12-17 21:35:02 +03:00
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() {
2024-01-04 22:03:10 +03:00
echo "${*}" | sed -e "s/[^[:alnum:]]//g"
2023-10-30 03:49:10 +03:00
}
2024-01-03 18:50:59 +03:00
# Parse integers from mixed string.
2024-01-04 20:59:16 +03:00
# Usage: parse_ints <STRING>
2024-01-03 18:50:59 +03:00
function parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
}
2024-01-04 20:59:16 +03:00
# Parse string to lowercase.
# Usage: parse_lowercase <STRING>
function parse_lowercase() {
echo "${*,,}"
}
# Parse string to uppercase.
# Usage: parse_uppercase <STRING>
function parse_uppercase() {
echo "${*^^}"
}
# Parse string to title case.
# Usage: parse_titlecase <STRING>
function parse_titlecase() {
local IFS=$'\n'
2024-01-04 22:03:10 +03:00
local parts=$(_parse_split ${*})
local minors=("is" "at" "of" "to" "in" "for" "the" "a" "an" "and" "but" "or" "on" "was" "were" "been" "be" "do" "did" "does")
2024-01-04 20:59:16 +03:00
local is_first=true
for part in ${parts[@]}; do
if ${is_first}; then
echo -n "${part^}"
is_first=false
continue
fi
2024-01-04 22:03:10 +03:00
if [[ "${minors[@]}" =~ $(echo ${part} | sed -e "s/[${_PARSE_SPLIT_CHARS}]//g") ]]; then
2024-01-04 20:59:16 +03:00
echo -n "${part}"
else
echo -n "${part^}"
fi
done
echo
}
# Parse string to sentence case.
# Usage: parse_sentencecase <STRING>
function parse_sentencecase() {
local lower="${*,,}"
echo "${lower^}"
}
# Parse string to start case.
# Usage: parse_startcase <STRING>
function parse_startcase() {
local IFS=$'\n'
2024-01-04 22:03:10 +03:00
local parts=$(_parse_split ${*})
2024-01-04 20:59:16 +03:00
for part in ${parts[@]}; do
echo -n "${part^}"
done
}
# Parse string to pretty Json.
# Usage: parse_json <STRING>
function parse_json() {
echo "${*}" | jq
}
2024-01-04 22:03:10 +03:00
# Split string by separators.
# Usage: _parse_split <STRING>
function _parse_split() {
2024-01-04 22:18:38 +03:00
echo "${*}" | sed -e "s/[A-Z]\+/\n&/g" -e "s/[0-9]\+/\n&\n/g" -e "s/[${_PARSE_SPLIT_CHARS}]/&\n/g" | sed -e "/^$/d"
2024-01-04 22:03:10 +03:00
}
# Get name parts.
# Usage: _get_parts <STRING>
function _get_parts() {
2024-01-04 22:03:10 +03:00
_parse_split "${*}" | sed -e "s/[${_PARSE_SPLIT_CHARS}]//g" | sed -e "/^$/d"
}