diff --git a/.config/bash/module/Name.sh b/.config/bash/module/Name.sh index dff9885..3b6cfa8 100644 --- a/.config/bash/module/Name.sh +++ b/.config/bash/module/Name.sh @@ -61,7 +61,7 @@ function name_simple() { [[ "${ext#.}" = "${name}" ]] && ext="" # Get new name. - local new_name=$(parse_simplify "${name}")${ext,,} + local new_name=$(echo "${name}" | parse_simplify)${ext,,} # check if same name. [[ "${target}" = "${new_name}" ]] && return 0 diff --git a/.config/bash/module/Parse.sh b/.config/bash/module/Parse.sh index b8faa53..0057633 100644 --- a/.config/bash/module/Parse.sh +++ b/.config/bash/module/Parse.sh @@ -1,19 +1,21 @@ +_PARSE_ALLOWED_CHARS="_-" +_PARSE_SPLIT_CHARS="\.\ _-" + # Parse data and output simplified format. # Usage: parse_simplify function parse_simplify() { - echo "${*}" | \ - sed -e "s/ /_/g" -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/_$//" + 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/_$//" } # Parse to PascalCase. # Usage: parse_pascal function parse_pascal() { - local IFS=${IFS}_- local parts=($(_get_parts ${*})) local result @@ -29,7 +31,6 @@ function parse_pascal() { # Parse to snake_case. # Usage: parse_snake function parse_snake() { - local IFS=${IFS}_- local parts=($(_get_parts ${*})) local result @@ -44,7 +45,6 @@ function parse_snake() { # Parse to kebab-case. # Usage: parse_kebab function parse_kebab() { - local IFS=${IFS}_- local parts=($(_get_parts ${*})) local result @@ -59,7 +59,6 @@ function parse_kebab() { # Parse to camelCase. # Usage: parse_camel function parse_camel() { - local IFS=${IFS}_- local parts=($(_get_parts ${*})) local result @@ -75,7 +74,6 @@ function parse_camel() { # Parse to SNAKE_CASE_UPPERCASE. # Usage: parse_snake_uppercase function parse_snake_uppercase() { - local IFS=${IFS}_- local parts=($(_get_parts ${*})) local result @@ -97,11 +95,5 @@ function parse_alnum() { # Get name parts. # Usage: _get_parts 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" + parse_simplify "${*}" | sed -e "s/[A-Z]/\n&/g" -e "s/[${_PARSE_SPLIT_CHARS}]/\n/g" | sed -e "1d" -e "/^$/d" }