mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-06-14 10:31:21 +00:00
fix: remove trailing hyphens if any after cut
This commit is contained in:
committed by
Romain Lespinasse
parent
596b602d8a
commit
19f9c84ac6
@@ -32,8 +32,9 @@ This GitHub Action will expose the slug/short values of [some GitHub environment
|
|||||||
|
|
||||||
- put the variable content in lower case
|
- put the variable content in lower case
|
||||||
- replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
- replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||||
- remove leading and trailing `-` character
|
- remove leading `-` characters
|
||||||
- limit the string size to 63 characters
|
- limit the string size to 63 characters
|
||||||
|
- remove trailing `-` characters
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Others <b>Slug-ish</b> commands are available</summary>
|
<summary>Others <b>Slug-ish</b> commands are available</summary>
|
||||||
|
|||||||
@@ -52,22 +52,22 @@ test('slug: trailing', () => {
|
|||||||
|
|
||||||
test('slug: a very long string', () => {
|
test('slug: a very long string', () => {
|
||||||
test_slug(
|
test_slug(
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-feature-very-very-very-very-very-very-very-long-more'
|
'has-an-awesome-feature-very-very-very-very-very-very-very-long'
|
||||||
)
|
)
|
||||||
test_slug_cs(
|
test_slug_cs(
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-more'
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('slug: short string after trailing', () => {
|
test('slug: short string after trailing', () => {
|
||||||
test_slug(
|
test_slug(
|
||||||
'-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters-',
|
'-has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters-',
|
||||||
'an-awesome-feature-very-very-very-very-very-very-very-long-more'
|
'has-an-awesome-feature-very-very-very-very-very-very-very-long'
|
||||||
)
|
)
|
||||||
test_slug_cs(
|
test_slug_cs(
|
||||||
'-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters-',
|
'-has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters-',
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-more'
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -57,12 +57,12 @@ test('slug_ref: test refs inside string', () => {
|
|||||||
|
|
||||||
test('slug_ref: a very long name', () => {
|
test('slug_ref: a very long name', () => {
|
||||||
test_slugref(
|
test_slugref(
|
||||||
'refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'refs/heads/has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-feature-very-very-very-very-very-very-very-long-more'
|
'has-an-awesome-feature-very-very-very-very-very-very-very-long'
|
||||||
)
|
)
|
||||||
test_slugref_cs(
|
test_slugref_cs(
|
||||||
'refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'refs/heads/has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-more'
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -47,11 +47,11 @@ test('slug_url: trailing', () => {
|
|||||||
|
|
||||||
test('slug_url: a very long string', () => {
|
test('slug_url: a very long string', () => {
|
||||||
test_slugurl(
|
test_slugurl(
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-feature-very-very-very-very-very-very-very-long-more'
|
'has-an-awesome-feature-very-very-very-very-very-very-very-long'
|
||||||
)
|
)
|
||||||
test_slugurl_cs(
|
test_slugurl_cs(
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-more'
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -47,12 +47,12 @@ test('slug_url_ref: a reference with upper case letters', () => {
|
|||||||
|
|
||||||
test('slug_url_ref: a very long name', () => {
|
test('slug_url_ref: a very long name', () => {
|
||||||
test_slugurlref(
|
test_slugurlref(
|
||||||
'refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'refs/heads/has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-feature-very-very-very-very-very-very-very-long-more'
|
'has-an-awesome-feature-very-very-very-very-very-very-very-long'
|
||||||
)
|
)
|
||||||
test_slugurlref_cs(
|
test_slugurlref_cs(
|
||||||
'refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
'refs/heads/has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters',
|
||||||
'an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-more'
|
'has-an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
41
src/slug.ts
41
src/slug.ts
@@ -3,23 +3,27 @@ const MAX_SLUG_STRING_SIZE = 63
|
|||||||
/**
|
/**
|
||||||
* slug_cs will take envVar and then :
|
* slug_cs will take envVar and then :
|
||||||
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slug_cs(envVar: string): string {
|
export function slug_cs(envVar: string): string {
|
||||||
return trailHyphen(replaceAnyNonAlphanumericCharacter(envVar)).substring(
|
return removeTrailingHyphens(
|
||||||
|
removeLeadingHyphens(replaceAnyNonAlphanumericCharacter(envVar)).substring(
|
||||||
0,
|
0,
|
||||||
MAX_SLUG_STRING_SIZE
|
MAX_SLUG_STRING_SIZE
|
||||||
)
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* slug will take envVar and then :
|
* slug will take envVar and then :
|
||||||
* - put the variable content in lower case
|
* - put the variable content in lower case
|
||||||
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slug(envVar: string): string {
|
export function slug(envVar: string): string {
|
||||||
@@ -30,8 +34,9 @@ export function slug(envVar: string): string {
|
|||||||
* slugref_cs will take envVar and then :
|
* slugref_cs will take envVar and then :
|
||||||
* - remove refs/(heads|tags|pull)/
|
* - remove refs/(heads|tags|pull)/
|
||||||
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugref_cs(envVar: string): string {
|
export function slugref_cs(envVar: string): string {
|
||||||
@@ -43,8 +48,9 @@ export function slugref_cs(envVar: string): string {
|
|||||||
* - remove refs/(heads|tags|pull)/
|
* - remove refs/(heads|tags|pull)/
|
||||||
* - put the variable content in lower case
|
* - put the variable content in lower case
|
||||||
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
* - replace any character by `-` except `0-9`, `a-z`, `.`, and `_`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugref(envVar: string): string {
|
export function slugref(envVar: string): string {
|
||||||
@@ -54,8 +60,9 @@ export function slugref(envVar: string): string {
|
|||||||
/**
|
/**
|
||||||
* slugurl_cs will take envVar and then :
|
* slugurl_cs will take envVar and then :
|
||||||
* - replace any character by `-` except `0-9`, `a-z`
|
* - replace any character by `-` except `0-9`, `a-z`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugurl_cs(envVar: string): string {
|
export function slugurl_cs(envVar: string): string {
|
||||||
@@ -66,24 +73,26 @@ export function slugurl_cs(envVar: string): string {
|
|||||||
* slugurl will take envVar and then :
|
* slugurl will take envVar and then :
|
||||||
* - put the variable content in lower case
|
* - put the variable content in lower case
|
||||||
* - replace any character by `-` except `0-9`, `a-z`
|
* - replace any character by `-` except `0-9`, `a-z`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugurl(envVar: string): string {
|
export function slugurl(envVar: string): string {
|
||||||
return slug(replaceAnyNonUrlCharactersWithHyphen(envVar))
|
return slugurl_cs(envVar.toLowerCase())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* slugurlref_cs will take envVar and then :
|
* slugurlref_cs will take envVar and then :
|
||||||
* - remove refs/(heads|tags|pull)/
|
* - remove refs/(heads|tags|pull)/
|
||||||
* - replace any character by `-` except `0-9`, `a-z`
|
* - replace any character by `-` except `0-9`, `a-z`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugurlref_cs(envVar: string): string {
|
export function slugurlref_cs(envVar: string): string {
|
||||||
return slugurl_cs(slugref_cs(envVar))
|
return slugurl_cs(removeRef(envVar))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,16 +100,20 @@ export function slugurlref_cs(envVar: string): string {
|
|||||||
* - remove refs/(heads|tags|pull)/
|
* - remove refs/(heads|tags|pull)/
|
||||||
* - put the variable content in lower case
|
* - put the variable content in lower case
|
||||||
* - replace any character by `-` except `0-9`, `a-z`
|
* - replace any character by `-` except `0-9`, `a-z`
|
||||||
* - remove leading and trailing `-` character
|
* - remove leading `-` characters
|
||||||
* - limit the string size to 63 characters
|
* - limit the string size to 63 characters
|
||||||
|
* - remove trailing `-` characters
|
||||||
* @param envVar to be slugged
|
* @param envVar to be slugged
|
||||||
*/
|
*/
|
||||||
export function slugurlref(envVar: string): string {
|
export function slugurlref(envVar: string): string {
|
||||||
return slugurl(slugref(envVar))
|
return slugurlref_cs(envVar.toLowerCase())
|
||||||
}
|
}
|
||||||
|
|
||||||
function trailHyphen(envVar: string): string {
|
function removeLeadingHyphens(envVar: string): string {
|
||||||
return envVar.replace(RegExp('^-*', 'g'), '').replace(RegExp('-*$', 'g'), '')
|
return envVar.replace(RegExp('^-*', 'g'), '')
|
||||||
|
}
|
||||||
|
function removeTrailingHyphens(envVar: string): string {
|
||||||
|
return envVar.replace(RegExp('-*$', 'g'), '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceAnyNonAlphanumericCharacter(envVar: string): string {
|
function replaceAnyNonAlphanumericCharacter(envVar: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user