Skip to content

Commit

Permalink
added linter skip comments and removed the Presentator specific infle…
Browse files Browse the repository at this point in the history
…ctor.Usernamify
  • Loading branch information
ganigeorgiev committed Jul 11, 2022
1 parent ed74166 commit 52c288d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 58 deletions.
2 changes: 1 addition & 1 deletion forms/record_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (form *RecordUpsert) Submit() error {
}

// delete old files (if any)
if err := form.processFilesToDelete(); err != nil {
if err := form.processFilesToDelete(); err != nil { //nolint:staticcheck
// for now fail silently to avoid reupload when `form.Submit()`
// is called manually (aka. not from an api request)...
}
Expand Down
5 changes: 3 additions & 2 deletions tools/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func (h *Hook[T]) Reset() {
// - any non-nil error is returned in one of the handlers
func (h *Hook[T]) Trigger(data T, oneOffHandlers ...Handler[T]) error {
h.mux.Lock()
handlers := append(h.handlers, oneOffHandlers...)
h.mux.Unlock() // unlock is not deferred to avoid deadlocks when Trigger is called recursive in the handlers
handlers := append(h.handlers, oneOffHandlers...) //nolint:gocritic
// unlock is not deferred to avoid deadlocks when Trigger is called recursive by the handlers
h.mux.Unlock()

for _, fn := range handlers {
err := fn(data)
Expand Down
32 changes: 0 additions & 32 deletions tools/inflector/inflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

var columnifyRemoveRegex = regexp.MustCompile(`[^\w\.\*\-\_\@\#]+`)
var snakecaseSplitRegex = regexp.MustCompile(`[\W_]+`)
var usernamifySplitRegex = regexp.MustCompile(`\W+`)

// UcFirst converts the first character of a string into uppercase.
func UcFirst(str string) string {
Expand Down Expand Up @@ -85,34 +84,3 @@ func Snakecase(str string) string {

return strings.ToLower(result.String())
}

// Usernamify generates a properly formatted username from the provided string.
// Returns "unknown" if `str` is empty or contains only non word characters.
//
// ```go
// Usernamify("John Doe, hello") // "john.doe.hello"
// ```
func Usernamify(str string) string {
// split at any non word character
words := usernamifySplitRegex.Split(strings.ToLower(str), -1)

// concatenate any non empty word with a dot
var result strings.Builder
for _, word := range words {
if word == "" {
continue
}

if result.Len() > 0 {
result.WriteString(".")
}

result.WriteString(word)
}

if result.Len() == 0 {
return "unknown"
}

return result.String()
}
23 changes: 0 additions & 23 deletions tools/inflector/inflector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,3 @@ func TestSnakecase(t *testing.T) {
}
}
}

func TestUsernamify(t *testing.T) {
scenarios := []struct {
val string
expected string
}{
{"", "unknown"},
{" ", "unknown"},
{"!@#$%^", "unknown"},
{"...", "unknown"},
{"_", "_"}, // underscore is valid word character
{"John Doe", "john.doe"},
{"John_Doe", "john_doe"},
{".a!b@c#d$e%123. ", "a.b.c.d.e.123"},
{"Hello, world", "hello.world"},
}

for i, scenario := range scenarios {
if result := inflector.Usernamify(scenario.val); result != scenario.expected {
t.Errorf("(%d) Expected %q, got %q", i, scenario.expected, result)
}
}
}

0 comments on commit 52c288d

Please sign in to comment.