From e1fea2d149a7ceabb80e903ce2c5f0f4e797242e Mon Sep 17 00:00:00 2001 From: Sergey Solodyagin Date: Fri, 29 Dec 2023 11:02:41 +0300 Subject: [PATCH] add files --- README.md | 12 +++++++++- go.mod | 11 +++++++++ go.sum | 10 ++++++++ sanitize.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ sanitize_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 sanitize.go create mode 100644 sanitize_test.go diff --git a/README.md b/README.md index 12b5e6f..4dcba81 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -# sanitize \ No newline at end of file +# sanitize + +## Installation + +```bash +go get -u github.com/andoma-go/sanitize +``` + +## Documentation + +View the generated [documentation](https://pkg.go.dev/github.com/andoma-go/sanitize) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..feaa2a5 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/andoma-go/sanitize + +go 1.21.4 + +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fa4b6e6 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/sanitize.go b/sanitize.go new file mode 100644 index 0000000..b6dd1a9 --- /dev/null +++ b/sanitize.go @@ -0,0 +1,60 @@ +package sanitize + +// Integer returns only numbers +func Integer(input string) string { + runes := []rune{} + n := 0 + for _, r := range input { + if !(r == 45) && !(r >= 48 && r <= 57) { // regexp.MustCompile(`[^-\d]`).ReplaceAllString(input, "$1") + continue + } + switch r { + case 45: // "-" + if n > 0 { + continue + } + default: // Digits + n++ + } + runes = append(runes, r) + } + if n > 0 { + return string(runes) + } + return "" +} + +// Float returns sanitized floating-point numbers +func Float(input string) string { + runes := []rune{} + dot := false + n := 0 + for i, r := range input { + if !(r >= 44 && r <= 46) && !(r >= 48 && r <= 57) { // regexp.MustCompile(`[^-\.,\d]`).ReplaceAllString(input, "$1") + continue + } + switch r { + case 45: // "-" + if n > 0 { + continue + } + case 44: // "," + fallthrough + case 46: // "." + if dot || i == len(input)-1 { + continue + } + dot = true + if n == 0 { + runes = append(runes, 48) // "0" + } + default: // Digits + n++ + } + runes = append(runes, r) + } + if n > 0 { + return string(runes) + } + return "" +} diff --git a/sanitize_test.go b/sanitize_test.go new file mode 100644 index 0000000..38c6101 --- /dev/null +++ b/sanitize_test.go @@ -0,0 +1,61 @@ +package sanitize + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestInteger tests the integer sanitize method +func TestInteger(t *testing.T) { + t.Parallel() + + var tests = []struct { + input string + expected string + }{ + {"45sDa8f$sd541zfa", "458541"}, + {"a-bc12.3def987asdf--", "-123987"}, + {"z,2134-59", "213459"}, + {".,-", ""}, + } + + for _, test := range tests { + output := Integer(test.input) + assert.Equal(t, test.expected, output) + } +} + +// BenchmarkInteger benchmarks the integer method +func BenchmarkInteger(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Integer("a-bc12.3def987asdf--") + } +} + +// TestFloat tests the float sanitize method +func TestFloat(t *testing.T) { + t.Parallel() + + var tests = []struct { + input string + expected string + }{ + {"45sDa8f$sd541zfa", "458541"}, + {"a-bc12.3def987asdf--", "-12.3987"}, + {"z,2134-59", "0,213459"}, + {".,-", ""}, + } + + for _, test := range tests { + output := Float(test.input) + assert.Equal(t, test.expected, output) + } +} + +// BenchmarkFloat benchmarks the float method +func BenchmarkFloat(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Float("a-bc12.3def987asdf--") + } +}