From 0587a9404aeab2be2f3dfcdb8a092a13f0af7b23 Mon Sep 17 00:00:00 2001 From: desktop Date: Thu, 4 Jan 2024 22:03:10 +0300 Subject: [PATCH] Parse : Fix parse splitting. --- .config/bash/module/Parse.sh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.config/bash/module/Parse.sh b/.config/bash/module/Parse.sh index b7763b3..98b5a54 100644 --- a/.config/bash/module/Parse.sh +++ b/.config/bash/module/Parse.sh @@ -88,8 +88,7 @@ function parse_snake_uppercase() { # Parse data keeping only alphanumeric characters. # Usage: parse_alnum function parse_alnum() { - echo "${*}" | \ - sed -e "s/[^[:alnum:]]//g" + echo "${*}" | sed -e "s/[^[:alnum:]]//g" } # Parse integers from mixed string. @@ -114,8 +113,8 @@ function parse_uppercase() { # Usage: parse_titlecase 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 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") local is_first=true for part in ${parts[@]}; do @@ -125,7 +124,7 @@ function parse_titlecase() { continue fi - if [[ "${minors[@]}" =~ "${part,,}" ]]; then + if [[ "${minors[@]}" =~ $(echo ${part} | sed -e "s/[${_PARSE_SPLIT_CHARS}]//g") ]]; then echo -n "${part}" else echo -n "${part^}" @@ -146,7 +145,7 @@ function parse_sentencecase() { # Usage: parse_startcase function parse_startcase() { local IFS=$'\n' - local parts=$(_get_parts ${*}) + local parts=$(_parse_split ${*}) for part in ${parts[@]}; do echo -n "${part^}" @@ -159,8 +158,14 @@ function parse_json() { echo "${*}" | jq } +# Split string by separators. +# Usage: _parse_split +function _parse_split() { + 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" +} + # Get name parts. # Usage: _get_parts function _get_parts() { - echo "${*}" | sed -e "s/[A-Z]/\n&/g" -e "s/[0-9]\+/\n&/g" -e "s/[${_PARSE_SPLIT_CHARS}]/&\n/g" | sed -e "/^$/d" + _parse_split "${*}" | sed -e "s/[${_PARSE_SPLIT_CHARS}]//g" | sed -e "/^$/d" }