lib/utilities: rewrite _bash-it-component-pluralize
Fix up and rename `_bash-it-pluralize-component()` to `_bash-it-component-pluralize()`, and add matching function `_bash-it-component-singularize()`.pull/1999/head
parent
c3eaa606de
commit
4f700dfb3b
|
|
@ -79,7 +79,8 @@ function _bash-it-egrep() {
|
||||||
|
|
||||||
function _bash-it-component-help() {
|
function _bash-it-component-help() {
|
||||||
local component file func
|
local component file func
|
||||||
component="$(_bash-it-pluralize-component "${1}")"
|
_bash-it-component-pluralize "${1}"
|
||||||
|
component="${_bash_it_component_pluralized_name?}"
|
||||||
file="$(_bash-it-component-cache-file "${component}")"
|
file="$(_bash-it-component-cache-file "${component}")"
|
||||||
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
|
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
|
||||||
func="_bash-it-${component}"
|
func="_bash-it-${component}"
|
||||||
|
|
@ -90,19 +91,36 @@ function _bash-it-component-help() {
|
||||||
|
|
||||||
function _bash-it-component-cache-file() {
|
function _bash-it-component-cache-file() {
|
||||||
local component file
|
local component file
|
||||||
component="$(_bash-it-pluralize-component "${1?${FUNCNAME[0]}: component name required}")"
|
_bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}"
|
||||||
|
component="${_bash_it_component_pluralized_name?}"
|
||||||
file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}"
|
file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}"
|
||||||
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
|
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
|
||||||
printf '%s' "${file}"
|
printf '%s' "${file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-pluralize-component() {
|
function _bash-it-component-singularize() {
|
||||||
local component="${1}"
|
[[ -n "${2:-}" ]] && local -n _bash_it_component_singularized_name="${2?}"
|
||||||
local -i len=$((${#component} - 1))
|
local component="${1?${FUNCNAME[0]}: component name required}"
|
||||||
|
local -i len="$((${#component} - 2))"
|
||||||
|
if [[ "${component:${len}:2}" == 'ns' ]]; then
|
||||||
|
component="${component%s}"
|
||||||
|
elif [[ "${component}" == "aliases" ]]; then
|
||||||
|
component="${component%es}"
|
||||||
|
fi
|
||||||
|
printf -v _bash_it_component_singularized_name '%s' "${component}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function _bash-it-component-pluralize() {
|
||||||
|
[[ -n "${2:-}" ]] && local -n _bash_it_component_pluralized_name="${2?}"
|
||||||
|
local component="${1?${FUNCNAME[0]}: component name required}"
|
||||||
|
local -i len="$((${#component} - 1))"
|
||||||
# pluralize component name for consistency
|
# pluralize component name for consistency
|
||||||
[[ "${component:${len}:1}" != 's' ]] && component="${component}s"
|
if [[ "${component:${len}:1}" != 's' ]]; then
|
||||||
[[ "${component}" == "alias" ]] && component="aliases"
|
component="${component}s"
|
||||||
printf '%s' "${component}"
|
elif [[ "${component}" == "alias" ]]; then
|
||||||
|
component="${component}es"
|
||||||
|
fi
|
||||||
|
printf -v _bash_it_component_pluralized_name '%s' "${component}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-clean-component-cache() {
|
function _bash-it-clean-component-cache() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue