25 lines
770 B
JavaScript
25 lines
770 B
JavaScript
/**
|
|
* Input sanitization utilities - pure functions for cleaning user input.
|
|
*/
|
|
|
|
import { MAX_IDENTIFIER_LENGTH, MAX_STRING_LENGTH } from './constants.js'
|
|
|
|
const IDENTIFIER_REGEX = /^\w+$/
|
|
|
|
export function sanitizeString(str, maxLength = MAX_STRING_LENGTH) {
|
|
if (typeof str !== 'string') return ''
|
|
const trimmed = str.trim()
|
|
return trimmed.length > maxLength ? trimmed.slice(0, maxLength) : trimmed
|
|
}
|
|
|
|
export function sanitizeIdentifier(str) {
|
|
if (typeof str !== 'string') return ''
|
|
const trimmed = str.trim()
|
|
if (trimmed.length === 0 || trimmed.length > MAX_IDENTIFIER_LENGTH) return ''
|
|
return IDENTIFIER_REGEX.test(trimmed) ? trimmed : ''
|
|
}
|
|
|
|
export function sanitizeLabel(str, maxLength = MAX_STRING_LENGTH) {
|
|
return sanitizeString(str, maxLength)
|
|
}
|