mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-05-02 17:44:52 +00:00
24 lines
515 B
TypeScript
24 lines
515 B
TypeScript
type SanitizeIdentifierOptions = {
|
|
fallback?: string
|
|
}
|
|
|
|
export function sanitizeIdentifier(value: string, options: SanitizeIdentifierOptions = {}): string {
|
|
const fallback = options.fallback ?? 'identifier'
|
|
const trimmed = value.trim()
|
|
const normalized = trimmed
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9_]+/g, '_')
|
|
.replace(/^_+|_+$/g, '')
|
|
.replace(/_+/g, '_')
|
|
|
|
if (!normalized) {
|
|
return fallback
|
|
}
|
|
|
|
if (/^[0-9]/.test(normalized)) {
|
|
return `_${normalized}`
|
|
}
|
|
|
|
return normalized
|
|
}
|