63 lines
No EOL
1.4 KiB
Bash
63 lines
No EOL
1.4 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
function string::trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}"
|
|
var="${var%"${var##*[![:space:]]}"}"
|
|
printf "%s" "$var"
|
|
}
|
|
|
|
function string::lowercase() {
|
|
printf "%s" "$1" | tr '[:upper:]' '[:lower:]'
|
|
}
|
|
|
|
function string::uppercase() {
|
|
printf "%s" "$1" | tr '[:lower:]' '[:upper:]'
|
|
}
|
|
|
|
function string::replace() {
|
|
local str="$1" from="$2" to="$3"
|
|
echo "${str//$from/$to}"
|
|
}
|
|
|
|
function string::strip_prefix() {
|
|
local str="$1" prefix="$2"
|
|
echo "${str#"$prefix"}"
|
|
}
|
|
|
|
function string::strip_suffix() {
|
|
local str="$1" suffix="$2"
|
|
echo "${str%"$suffix"}"
|
|
}
|
|
|
|
function string::slugify() {
|
|
echo "$1" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-'
|
|
}
|
|
|
|
function string::pad_right() {
|
|
local str="$1" width="$2"
|
|
printf "%-${width}s" "$str"
|
|
}
|
|
|
|
function string::pad_left() {
|
|
local str="$1" width="$2"
|
|
printf "%${width}s" "$str"
|
|
}
|
|
|
|
function string::repeat() {
|
|
local char="$1" count="$2"
|
|
printf "%${count}s" | tr " " "$char"
|
|
}
|
|
|
|
function string::split() {
|
|
local str="$1" delimiter="$2"
|
|
local -n result_ref="$3" # nameref — caller passes array name
|
|
|
|
IFS="$delimiter" read -ra result_ref <<< "$str"
|
|
}
|
|
|
|
function string::is_empty() { [[ -z "$1" ]]; }
|
|
function string::is_not_empty() { [[ -n "$1" ]]; }
|
|
function string::starts_with() { [[ "$1" == "$2"* ]]; }
|
|
function string::ends_with() { [[ "$1" == *"$2" ]]; }
|
|
function string::contains() { [[ "$1" == *"$2"* ]]; } |