Files
insomnia/packages/insomnia-data/common-src/misc.ts
T
Pavlos Koutoglou a3822e8d30 feat(git): name app-managed git folders after their project instead of a bare hex id (#10349)
* 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
2026-08-10 20:40:00 +03:00

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, '');
}