Parse : Add common English parsers.

This commit is contained in:
Dmitry Voronin 2024-01-04 20:59:16 +03:00
parent d559059241
commit 7a972f9a53
2 changed files with 75 additions and 7 deletions

View file

@ -16,7 +16,7 @@ function parse_simplify() {
# Parse to PascalCase. # Parse to PascalCase.
# Usage: parse_pascal <STRING> # Usage: parse_pascal <STRING>
function parse_pascal() { function parse_pascal() {
local parts=($(_get_parts ${*})) local parts=($(_get_parts $(parse_simplify "${*}")))
local result local result
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
@ -31,7 +31,7 @@ function parse_pascal() {
# Parse to snake_case. # Parse to snake_case.
# Usage: parse_snake <STRING> # Usage: parse_snake <STRING>
function parse_snake() { function parse_snake() {
local parts=($(_get_parts ${*})) local parts=($(_get_parts $(parse_simplify "${*}")))
local result local result
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
@ -45,7 +45,7 @@ function parse_snake() {
# Parse to kebab-case. # Parse to kebab-case.
# Usage: parse_kebab <STRING> # Usage: parse_kebab <STRING>
function parse_kebab() { function parse_kebab() {
local parts=($(_get_parts ${*})) local parts=($(_get_parts $(parse_simplify "${*}")))
local result local result
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
@ -59,7 +59,7 @@ function parse_kebab() {
# Parse to camelCase. # Parse to camelCase.
# Usage: parse_camel <STRING> # Usage: parse_camel <STRING>
function parse_camel() { function parse_camel() {
local parts=($(_get_parts ${*})) local parts=($(_get_parts $(parse_simplify "${*}")))
local result local result
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
@ -74,7 +74,7 @@ function parse_camel() {
# Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.** # Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.**
# Usage: parse_snake_uppercase <STRING> # Usage: parse_snake_uppercase <STRING>
function parse_snake_uppercase() { function parse_snake_uppercase() {
local parts=($(_get_parts ${*})) local parts=($(_get_parts $(parse_simplify "${*}")))
local result local result
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
@ -93,12 +93,74 @@ function parse_alnum() {
} }
# Parse integers from mixed string. # Parse integers from mixed string.
# Usage: parse_ints <STRING>
function parse_ints() { function parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g' echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
} }
# 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'
local parts=$(_get_parts ${*})
local minors=("is" "at" "of" "to" "in" "for" "the" "a" "an" "and" "but" "or" "on" "was" "were" "been" "be" "do" "did" "does" "")
local is_first=true
for part in ${parts[@]}; do
if ${is_first}; then
echo -n "${part^}"
is_first=false
continue
fi
if [[ "${minors[@]}" =~ "${part,,}" ]]; then
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'
local parts=$(_get_parts ${*})
for part in ${parts[@]}; do
echo -n "${part^}"
done
}
# Parse string to pretty Json.
# Usage: parse_json <STRING>
function parse_json() {
echo "${*}" | jq
}
# Get name parts. # Get name parts.
# Usage: _get_parts <STRING> # Usage: _get_parts <STRING>
function _get_parts() { function _get_parts() {
parse_simplify "${*}" | sed -e "s/[A-Z]/\n&/g" -e "s/[${_PARSE_SPLIT_CHARS}]/\n/g" | sed -e "/^$/d" echo "${*}" | sed -e "s/[A-Z]/\n&/g" -e "s/[${_PARSE_SPLIT_CHARS}]/&\n/g" | sed -e "/^$/d"
} }

View file

@ -273,7 +273,13 @@ Command|Description
`parse_camel <STRING>`|Parse to camelCase. `parse_camel <STRING>`|Parse to camelCase.
`parse_snake_uppercase <STRING>`|Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.** `parse_snake_uppercase <STRING>`|Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.**
`parse_alnum <STRING>`|Parse data keeping only alphanumeric characters. `parse_alnum <STRING>`|Parse data keeping only alphanumeric characters.
`parse_ints`|Parse integers from mixed string. `parse_ints <STRING>`|Parse integers from mixed string.
`parse_lowercase <STRING>`|Parse string to lowercase.
`parse_uppercase <STRING>`|Parse string to uppercase.
`parse_titlecase <STRING>`|Parse string to title case.
`parse_sentencecase <STRING>`|Parse string to sentence case.
`parse_startcase <STRING>`|Parse string to start case.
`parse_json <STRING>`|Parse string to pretty Json.
## Permissions. ## Permissions.