(
name: string,
options: Partial<{ disableEmptyFallback: boolean }> = {}
)
| 13 | } |
| 14 | |
| 15 | export function sanitizePythonVariableName( |
| 16 | name: string, |
| 17 | options: Partial<{ disableEmptyFallback: boolean }> = {} |
| 18 | ): string { |
| 19 | let sanitizedVariableName = name |
| 20 | // Convert whitespace to underscores |
| 21 | .replace(/\s+/g, '_') |
| 22 | // Remove invalid characters |
| 23 | .replace(/[^0-9a-zA-Z_]/g, '') |
| 24 | // Remove invalid leading characters |
| 25 | .replace(/^[^a-zA-Z_]+/g, '') |
| 26 | |
| 27 | // Set a default value |
| 28 | if (sanitizedVariableName === '' && !options.disableEmptyFallback) { |
| 29 | sanitizedVariableName = 'input_1' // We don't want to call it just `input` to avoid name clashes |
| 30 | } |
| 31 | |
| 32 | return sanitizedVariableName |
| 33 | } |
no outgoing calls
no test coverage detected