MCPcopy Index your code
hub / github.com/github/gh-aw / SanitizeIdentifierName

Function SanitizeIdentifierName

pkg/stringutil/sanitize.go:218–235  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

216// For workflow artifact and user-agent identifiers, use workflow.SanitizeArtifactIdentifier
217// instead, which produces hyphen-separated lowercase output.
218func 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.

Calls

no outgoing calls