mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-08 14:10:08 -04:00
* feat(git): implement folderSlug for Git repositories and add slugify utility * fix(project): wait for modal backdrop to hide before clicking next button * feat(git): enhance folderSlug validation and path resolution for Git repositories * fix(git): improve fallback path resolution for git repositories to ensure safety * fix(project): handle unsaved changes confirmation when closing project modal * fix(project): handle race condition in unsaved changes confirmation dialog when closing project modal
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
/**
|
|
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
|
|
* @param prefix
|
|
* @returns {string}
|
|
*/
|
|
export function generateId(prefix?: string) {
|
|
const id = uuidv4().replace(/-/g, '');
|
|
|
|
if (prefix) {
|
|
return `${prefix}_${id}`;
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Turns arbitrary text into a filesystem-safe slug: lowercased, accents
|
|
* stripped, runs of non-alphanumeric characters collapsed to a single `-`,
|
|
* leading/trailing `-` trimmed, and truncated to `maxLength`.
|
|
*
|
|
* Returns `''` when nothing usable remains (e.g. all-emoji or all-punctuation
|
|
* input) — callers should treat that as "no slug available".
|
|
*/
|
|
export function slugify(input: string, maxLength = 40) {
|
|
const slug = input
|
|
.normalize('NFKD')
|
|
.replace(/[\u0300-\u036F]/g, '') // strip accents (combining diacritical marks)
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '');
|
|
|
|
if (slug.length <= maxLength) {
|
|
return slug;
|
|
}
|
|
return slug.slice(0, maxLength).replace(/-+$/g, '');
|
|
}
|