Skip to content

Commit

Permalink
slug string (#62)
Browse files Browse the repository at this point in the history
* slug string

* update linter
  • Loading branch information
JamesReate committed Apr 16, 2024
1 parent cb7d3b6 commit 061a558
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
41 changes: 38 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,48 @@ linters:
- prealloc
- revive
- goimports
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- zerologlint

issues:
fix: true

linters-settings:
gci:
# DEPRECATED: use `sections` and `prefix(github.com/org/project)` instead.
local-prefixes: github.com/org/project
# Section configuration to compare against.
# Section names are case-insensitive and may contain parameters in ().
# The default order of sections is `standard > default > custom > blank > dot`,
# If `custom-order` is `true`, it follows the order of `sections` option.
# Default: ["standard", "default"]
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/DIMO-Network/shared) # Custom section: groups all imports with the specified Prefix.
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
# Skip generated files.
# Default: true
skip-generated: true
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: false
tagliatelle:
# Check the struck tag name case.
case:
# Use the struct field name to check the name of the struct tag.
# Default: false
use-field-name: true
rules:
# Any struct tag type can be used.
# Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`.
json: camel
yaml: upperSnake
24 changes: 24 additions & 0 deletions slug_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shared

import (
"strings"
"unicode"

"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)

func SlugString(term string) string {
lowerCase := cases.Lower(language.English, cases.NoLower)
lowerTerm := lowerCase.String(term)

t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
cleaned, _, _ := transform.String(t, lowerTerm)
cleaned = strings.ReplaceAll(cleaned, " ", "-")
cleaned = strings.ReplaceAll(cleaned, "_", "-")

return cleaned
}
48 changes: 48 additions & 0 deletions slug_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package shared

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSlugString(t *testing.T) {
tests := []struct {
term string
want string
}{
{
term: "Mercedes-Benz",
want: "mercedes-benz",
},
{
term: "AC",
want: "ac",
},
{
term: "AM General",
want: "am-general",
},
{
term: "Alfa Romeo",
want: "alfa-romeo",
},
{
term: "Citroën",
want: "citroen",
},
{
term: "SsangYong",
want: "ssangyong",
},
{
term: "Land Rover",
want: "land-rover",
},
}
for _, tt := range tests {
t.Run(tt.term, func(t *testing.T) {
assert.Equalf(t, tt.want, SlugString(tt.term), "SlugString(%v)", tt.term)
})
}
}

0 comments on commit 061a558

Please sign in to comment.