SanitizeIdentifierName sanitizes a name for use as a programming-language identifier by replacing disallowed characters with underscores. Use this function for code identifiers (for example JavaScript and Python variable names). It preserves [a-zA-Z0-9_] plus optional extraAllowed runes and prepend
(name string, extraAllowed func(rune) bool)
| 216 | // For workflow artifact and user-agent identifiers, use workflow.SanitizeArtifactIdentifier |
| 217 | // instead, which produces hyphen-separated lowercase output. |
| 218 | func SanitizeIdentifierName(name string, extraAllowed func(rune) bool) string { |
| 219 | result := strings.Map(func(r rune) rune { |
| 220 | if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' { |
| 221 | return r |
| 222 | } |
| 223 | if extraAllowed != nil && extraAllowed(r) { |
| 224 | return r |
| 225 | } |
| 226 | return '_' |
| 227 | }, name) |
| 228 | |
| 229 | // Ensure it doesn't start with a number |
| 230 | if result != "" && result[0] >= '0' && result[0] <= '9' { |
| 231 | result = "_" + result |
| 232 | } |
| 233 | |
| 234 | return result |
| 235 | } |
| 236 | |
| 237 | // SanitizeParameterName converts a parameter name to a safe JavaScript identifier |
| 238 | // by replacing non-alphanumeric characters with underscores. |
no outgoing calls