1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

clean up typescript initialization

This commit is contained in:
checktheroads
2021-04-20 09:34:12 -07:00
parent 0aa8fc2fc2
commit 99f0e31810
26 changed files with 312 additions and 414 deletions

View File

@@ -13,6 +13,22 @@ export function hasError(data: Record<string, unknown>): data is ErrorBase {
return 'error' in data;
}
/**
* Create a slug from any input string.
*
* @param slug Original string.
* @param chars Maximum number of characters.
* @returns Slugified string.
*/
export function slugify(slug: string, chars: number): string {
return slug
.replace(/[^\-\.\w\s]/g, '') // Remove unneeded chars
.replace(/^[\s\.]+|[\s\.]+$/g, '') // Trim leading/trailing spaces
.replace(/[\-\.\s]+/g, '-') // Convert spaces and decimals to hyphens
.toLowerCase() // Convert to lowercase
.substring(0, chars); // Trim to first chars chars
}
/**
* Type guard to determine if a value is not null, undefined, or empty.
*/