Skip to content

Commit

Permalink
fix(slug_url): remove ending hypen if any
Browse files Browse the repository at this point in the history
  • Loading branch information
rlespinasse authored Jun 15, 2022
1 parent 930d452 commit 4e258f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/slugify-value.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ jobs:
with:
key: ANOTHER_MAX_LENGTH
value: "Never gonna give you up Never gonna let you down"
slug-maxlength: 23
slug-maxlength: 24
- name: Validate // Slugify with another max length
run: |
[[ "${{ env.ANOTHER_MAX_LENGTH }}" == "Never gonna give you up Never gonna let you down" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up-" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up-" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL }}" == "never-gonna-give-you-up" ]]
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL_CS }}" == "Never-gonna-give-you-up" ]]
shell: bash
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Produce some `slug`-ed environment variables based on the input one.
- `<env name>_SLUG_URL` (or `<env name>_SLUG_URL_CS`)

- like `<env name>_SLUG` (or `<env name>_SLUG_CS`) with the `.`, and `_` characters also replaced by `-`
- will not end with `-`

## Usage

Expand Down
16 changes: 10 additions & 6 deletions slugify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ slug() {
# 3d : Remove leading dashes
# 4d : Remove trailing dashes
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
reduce "$output"
reduce "$output" false
}

slug_url() {
Expand All @@ -42,15 +42,19 @@ slug_url() {
# 3d : Remove leading dashes
# 4d : Remove trailing dashes
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
reduce "$output"
reduce "$output" true
}

reduce() {
if [ "${MAX_LENGTH}" == "nolimit" ]; then
echo "$1"
else
cut -c1-"${MAX_LENGTH}" <<<"$1"
reduced_value="$1"
remove_ending_hypen="$2"
if [ "${MAX_LENGTH}" != "nolimit" ]; then
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$1")
fi
if [ "$remove_ending_hypen" == "true" ]; then
reduced_value=$(sed -E 's/-*$//' <<<"$reduced_value")
fi
echo "$reduced_value"
}

SLUG_VALUE=$(slug "$VALUE")
Expand Down

0 comments on commit 4e258f5

Please sign in to comment.