add files
This commit is contained in:
@@ -1 +1,11 @@
|
||||
# sanitize
|
||||
# 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)
|
||||
|
||||
@@ -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
|
||||
)
|
||||
@@ -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=
|
||||
+60
@@ -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 ""
|
||||
}
|
||||
@@ -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--")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user