Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Created November 20, 2021 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomdavidson/33e22d36faad9fb9cc886b724acaf5c2 to your computer and use it in GitHub Desktop.
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.
#!/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