Added new function to array and updated generate_readme.sh

This commit is contained in:
labbots
2020-06-07 04:33:53 +01:00
parent f4ebe6afcb
commit 8fb2ac4974
4 changed files with 52 additions and 13 deletions

View File

@@ -13,6 +13,7 @@ Bash library which provides utility functions and helpers for functional program
- [Array](#array)
- [array::contains()](#arraycontains)
- [array::dedupe()](#arraydedupe)
- [array::is_empty()](#arrayis_empty)
- [array::join()](#arrayjoin)
- [array::reverse()](#arrayreverse)
- [array::random_element()](#arrayrandom_element)
@@ -63,7 +64,7 @@ array::contains "c" ${array[@]}
### array::dedupe()
Remove duplicate items from the array
Remove duplicate items from the array.
#### Example
@@ -87,6 +88,26 @@ a b c
- Deduplicated array.
### array::is_empty()
Check if a given array is empty.
#### Example
```bash
array=("a" "b" "c" "d")
array::is_empty "${array[@]}"
```
#### Arguments
- **$1** (array): Array to be checked.
#### Exit codes
- **0**: If the given array is empty.
- **2**: If the given array is not empty.
### array::join()
Join array elements with a string.

View File

@@ -117,8 +117,8 @@ _setup_tempfile() {
_generate_shdoc() {
declare file
file="$(realpath $1)"
./shdoc.awk < "${file}" >> "$2"
file="$(realpath "${1}")"
./bashdoc.awk < "${file}" >> "$2"
}
_insert_shdoc_to_file() {
@@ -132,7 +132,7 @@ _insert_shdoc_to_file() {
sed -i "1s/^/${info_shdoc}\n/" "${shdoc_tmp_file}"
if grep --color=always -Pzl "(?s)${start_shdoc}.*\n.*${end_shdoc}" $source_markdown &> /dev/null; then
if grep --color=always -Pzl "(?s)${start_shdoc}.*\n.*${end_shdoc}" "${source_markdown}" &> /dev/null; then
# src https://stackoverflow.com/questions/2699666/replace-delimited-block-of-text-in-file-with-the-contents-of-another-file
sed -i -ne "/${start_shdoc}/ {p; r ${shdoc_tmp_file}" -e ":a; n; /${end_shdoc}/ {p; b}; ba}; p" "${source_markdown}"
@@ -179,13 +179,13 @@ _generate_toc() {
temp_output=$output"$level- [$title](#$anchor)\n"
counter=1
while true; do
nlines="$(echo -e $temp_output | wc -l)"
duplines="$(echo -e $temp_output | sort | uniq | wc -l)"
if [ $nlines = $duplines ]; then
nlines="$(echo -e "${temp_output}" | wc -l)"
duplines="$(echo -e "${temp_output}" | sort | uniq | wc -l)"
if [ "${nlines}" = "${duplines}" ]; then
break
fi
temp_output=$output"$level- [$title](#$anchor-$counter)\n"
counter=$(($counter + 1))
counter=$((counter + 1))
done
output="$temp_output"
@@ -195,8 +195,7 @@ _generate_toc() {
done <<< "$(grep -E "^#{${MINLEVEL},${MAXLEVEL}} " "${1}" | tr -d '\r' | sed "s/^#\{$((${MINLEVEL} - 1))\}//g")"
# when in toc we have two `--` quit one
output="$(echo "$output" | sed 's/--*/-/g')"
output="${output//--*/-}"
echo "$output"
}
@@ -247,8 +246,8 @@ _process_toc() {
sed '/```/,/```/d' "${source_markdown}" > "${toc_temp_file}"
declare level=$MINLEVEL
while [[ $(grep -E "^#{$level} " "${toc_temp_file}" | wc -l) -le 1 ]]; do
level=$(($level + 1))
while [[ $(grep -Ec "^#{$level} " "${toc_temp_file}") -le 1 ]]; do
level=$((level + 1))
done
MINLEVEL=${level}

View File

@@ -29,7 +29,7 @@ array::contains() {
return 1
}
# @description Remove duplicate items from the array
# @description Remove duplicate items from the array.
#
# @example
# array=("a" "b" "a" "c")
@@ -54,6 +54,25 @@ array::dedupe() {
printf '%s\n' "${arr_unique[@]}"
}
# @description Check if a given array is empty.
#
# @example
# array=("a" "b" "c" "d")
# array::is_empty "${array[@]}"
#
# @arg $1 array Array to be checked.
#
# @exitcode 0 If the given array is empty.
# @exitcode 2 If the given array is not empty.
array::is_empty() {
declare -a array
local array=("$@")
if [ ${#array[@]} -eq 0 ]; then
return 0
else
return 1
fi
}
# @description Join array elements with a string.
#
# @example