This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unsafe database characters for db add using slugs
Signed-off-by: Jason McCallister <jason@craftcms.com>
- Loading branch information
1 parent
769ff59
commit f34066f
Showing
4 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package slug | ||
|
||
import "strings" | ||
|
||
// Generate takes a string and will trim, remove spaces, and remove special characters | ||
func Generate(s string) string { | ||
sl := s | ||
|
||
// trim the string | ||
sl = strings.TrimSpace(sl) | ||
|
||
// remove spaces | ||
if strings.Contains(sl, " ") { | ||
sl = strings.ReplaceAll(sl, " ", "_") | ||
} | ||
|
||
return sl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package slug | ||
|
||
import "testing" | ||
|
||
func TestGenerate(t *testing.T) { | ||
type args struct { | ||
s string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "removes spaces from strings", | ||
args: args{s: "this database"}, | ||
want: "this_database", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Generate(tt.args.s); got != tt.want { | ||
t.Errorf("Generate() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |