Parse : Simplify.

This commit is contained in:
Dmitry Voronin 2023-12-30 20:16:32 +03:00
parent 04ffbfe55a
commit 908c157eff
2 changed files with 12 additions and 20 deletions

View file

@ -61,7 +61,7 @@ function name_simple() {
[[ "${ext#.}" = "${name}" ]] && ext="" [[ "${ext#.}" = "${name}" ]] && ext=""
# Get new name. # Get new name.
local new_name=$(parse_simplify "${name}")${ext,,} local new_name=$(echo "${name}" | parse_simplify)${ext,,}
# check if same name. # check if same name.
[[ "${target}" = "${new_name}" ]] && return 0 [[ "${target}" = "${new_name}" ]] && return 0

View file

@ -1,19 +1,21 @@
_PARSE_ALLOWED_CHARS="_-"
_PARSE_SPLIT_CHARS="\.\ _-"
# Parse data and output simplified format. # Parse data and output simplified format.
# Usage: parse_simplify <STRING> # Usage: parse_simplify <STRING>
function parse_simplify() { function parse_simplify() {
echo "${*}" | \ echo "${*}" | sed \
sed -e "s/ /_/g" -e "s/\./_/g" \ -e "s/[${_PARSE_SPLIT_CHARS}]/_/g" \
-e "s/[^[:alnum:]_-]//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/-_/_/g" \ -e "s/_-/_/g" -e "s/-_/_/g" \
-e "s/_\+/_/g" \ -e "s/_\+/_/g" \
-e "s/^_//" -e "s/_$//" -e "s/^_//" -e "s/_$//"
} }
# Parse to PascalCase. # Parse to PascalCase.
# Usage: parse_pascal <STRING> # Usage: parse_pascal <STRING>
function parse_pascal() { function parse_pascal() {
local IFS=${IFS}_-
local parts=($(_get_parts ${*})) local parts=($(_get_parts ${*}))
local result local result
@ -29,7 +31,6 @@ 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 IFS=${IFS}_-
local parts=($(_get_parts ${*})) local parts=($(_get_parts ${*}))
local result local result
@ -44,7 +45,6 @@ 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 IFS=${IFS}_-
local parts=($(_get_parts ${*})) local parts=($(_get_parts ${*}))
local result local result
@ -59,7 +59,6 @@ function parse_kebab() {
# Parse to camelCase. # Parse to camelCase.
# Usage: parse_camel <STRING> # Usage: parse_camel <STRING>
function parse_camel() { function parse_camel() {
local IFS=${IFS}_-
local parts=($(_get_parts ${*})) local parts=($(_get_parts ${*}))
local result local result
@ -75,7 +74,6 @@ function parse_camel() {
# Parse to SNAKE_CASE_UPPERCASE. # Parse to SNAKE_CASE_UPPERCASE.
# Usage: parse_snake_uppercase <STRING> # Usage: parse_snake_uppercase <STRING>
function parse_snake_uppercase() { function parse_snake_uppercase() {
local IFS=${IFS}_-
local parts=($(_get_parts ${*})) local parts=($(_get_parts ${*}))
local result local result
@ -97,11 +95,5 @@ function parse_alnum() {
# Get name parts. # Get name parts.
# Usage: _get_parts <STRING> # Usage: _get_parts <STRING>
function _get_parts() { function _get_parts() {
parse_simplify "${*}" | _split_uppercase parse_simplify "${*}" | sed -e "s/[A-Z]/\n&/g" -e "s/[${_PARSE_SPLIT_CHARS}]/\n/g" | sed -e "1d" -e "/^$/d"
}
# Split string by uppercase letters.
# Usage: echo | _split_uppercase
function _split_uppercase() {
sed -e "s/[A-Z]/\n&/g"
} }