fix: extended field names

This commit is contained in:
Fabio Bozzo
2024-11-29 19:32:31 +01:00
parent dff52f80c4
commit a25bfbaf45
2 changed files with 59 additions and 1 deletions

View File

@@ -11,7 +11,21 @@ import (
var ( var (
indexRegex = regexp.MustCompile(`^-?\d+$`) indexRegex = regexp.MustCompile(`^-?\d+$`)
sliceRegex = regexp.MustCompile(`^((\-?\d+:\-?\d*)|(\-?\d*:\-?\d+))$`) sliceRegex = regexp.MustCompile(`^((\-?\d+:\-?\d*)|(\-?\d*:\-?\d+))$`)
fieldRegex = regexp.MustCompile(`^\.[a-zA-Z_-]*?$`)
// According to ECMAScript 2024, identifiers can include:
// - Unicode letters
// - $, _
// - Unicode combining marks
// - Unicode digits
// - Unicode connector punctuation
// Additional characters allowed for compatibility:
// - hyphen (-)
// \p{L} - any kind of letter from any language
// \p{Mn}\p{Mc} - combining marks and spacing combining marks
// \p{Nd} - decimal numbers
// \p{Pc} - connector punctuation (like underscore)
// \p{Sm}\p{So} - math symbols and other symbols
fieldRegex = regexp.MustCompile(`^\.[a-zA-Z_\p{L}][a-zA-Z$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Sm}\p{So}-]*?$`)
) )
func Parse(str string) (Selector, error) { func Parse(str string) (Selector, error) {

View File

@@ -572,4 +572,48 @@ func TestParse(t *testing.T) {
_, err := Parse(".[foo]") _, err := Parse(".[foo]")
require.Error(t, err) require.Error(t, err)
}) })
t.Run("extended field names", func(t *testing.T) {
validFields := []string{
".basic",
".user_name",
".user-name",
".userName$special",
".αβγ", // Greek letters
".użytkownik", // Polish characters
".用户", // Chinese characters
".사용자", // Korean characters
"._private",
".number123",
".camelCase",
".snake_case",
".kebab-case",
".mixed_kebab-case",
".with$dollar",
".MIXED_Case_123",
".unicode⌘",
}
for _, field := range validFields {
sel, err := Parse(field)
require.NoError(t, err, "field: %s", field)
require.NotNil(t, sel)
}
invalidFields := []string{
".123number", // Can't start with digit
".@special", // @ not allowed
".space name", // No spaces
".#hashtag", // No #
".name!", // No !
".{brackets}", // No brackets
".name/with/slashes", // No slashes
}
for _, field := range invalidFields {
sel, err := Parse(field)
require.Error(t, err, "field: %s", field)
require.Nil(t, sel)
}
})
} }