mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-05-21 22:21:46 +00:00
BREAKING CHANGE: The action implementation move from container action to node.js action Co-authored-by: Romain Lespinasse <romain.lespinasse@gmail.com>
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
const MAX_SLUG_STRING_SIZE = 63
|
|
const SHORT_SHA_SIZE = 8
|
|
|
|
/**
|
|
* slug will take envVar and then :
|
|
* - put the variable content in lower case
|
|
* - replace any character by `-` except `0-9`, `a-z`, and `.`
|
|
* - remove leading and trailing `-` character
|
|
* - limit the string size to 63 characters
|
|
* @param envVar to be slugged
|
|
*/
|
|
export function slug(envVar: string): string {
|
|
return trailHyphen(
|
|
replaceAnyNonAlphanumericCaracter(envVar.toLowerCase())
|
|
).substring(0, MAX_SLUG_STRING_SIZE)
|
|
}
|
|
|
|
/**
|
|
* slug will take envVar and then :
|
|
* - remove refs/(heads|tags)/
|
|
* - put the variable content in lower case
|
|
* - replace any character by `-` except `0-9`, `a-z`, and `.`
|
|
* - remove leading and trailing `-` character
|
|
* - limit the string size to 63 characters
|
|
* @param envVar to be slugged
|
|
*/
|
|
export function slugref(envVar: string): string {
|
|
return slug(removeRef(envVar.toLowerCase()))
|
|
}
|
|
|
|
/**
|
|
* slug will take envVar and then :
|
|
* - put the variable content in lower case
|
|
* - replace any character by `-` except `0-9`, `a-z`
|
|
* - remove leading and trailing `-` character
|
|
* - limit the string size to 63 characters
|
|
* @param envVar to be slugged
|
|
*/
|
|
export function slugurl(envVar: string): string {
|
|
return slug(replaceAnyDotToHyphen(envVar))
|
|
}
|
|
|
|
/**
|
|
* slug will take envVar and then :
|
|
* - remove refs/(heads|tags)/
|
|
* - put the variable content in lower case
|
|
* - replace any character by `-` except `0-9`, `a-z`
|
|
* - remove leading and trailing `-` character
|
|
* - limit the string size to 63 characters
|
|
* @param envVar to be slugged
|
|
*/
|
|
export function slugurlref(envVar: string): string {
|
|
return slugurl(slugref(envVar))
|
|
}
|
|
|
|
/**
|
|
* slug will take envVar and then :
|
|
* - limit the string size to 8 characters
|
|
* @param envVar to be slugged
|
|
*/
|
|
export function shortsha(envVar: string): string {
|
|
return envVar.substring(0, SHORT_SHA_SIZE)
|
|
}
|
|
|
|
function trailHyphen(envVar: string): string {
|
|
return envVar.replace(RegExp('^-*', 'g'), '').replace(RegExp('-*$', 'g'), '')
|
|
}
|
|
|
|
function replaceAnyNonAlphanumericCaracter(envVar: string): string {
|
|
return envVar.replace(RegExp('[^a-z0-9.]', 'g'), '-')
|
|
}
|
|
|
|
function replaceAnyDotToHyphen(envVar: string): string {
|
|
return envVar.replace(RegExp('[.]', 'g'), '-')
|
|
}
|
|
|
|
function removeRef(envVar: string): string {
|
|
return envVar.replace(RegExp('^refs/(heads|tags)/'), '')
|
|
}
|