Created
November 20, 2021 05:13
-
-
Save tomdavidson/33e22d36faad9fb9cc886b724acaf5c2 to your computer and use it in GitHub Desktop.
Lowercased, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with '-'. No leading or trailing '-'. Useful with URLs, host names, domain names, and stack names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
shopt -s extglob | |
# Lowercased, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with '-'. | |
# No leading or trailing '-'. Useful with URLs, host names, domain names, and stack names. | |
slugify() { | |
: "${1:?'Usage: slugify NAME'}" | |
local next="$1" | |
next="${next,,}" # lowercase | |
next="${next//+([^a-z0-9])/-}" # replace non-alphanumeric wiht '-' | |
next="${next#-}" # remove - from the start | |
next="${next%-}" # remove - from the end | |
next="${next:0:63}" # only first 64 char | |
echo "$next" | |
} | |
[[ "${BASH_SOURCE[0]}" == "$0" ]] && "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment