mirror of
https://github.com/cubixle/email-checker.git
synced 2026-04-24 18:34:42 +01:00
added isGeneric method and tests
This commit is contained in:
16
.gitignore
vendored
16
.gitignore
vendored
@@ -1,14 +1,2 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
.DS_Store
|
||||
vendor
|
||||
17
README.md
17
README.md
@@ -1,2 +1,19 @@
|
||||
# email-checker
|
||||
Email checker is package with a collection of different methods of checking email addresses.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/lukerodham/email-checker"
|
||||
|
||||
func main() {
|
||||
is, err := checker.IsGeneric("example@gmail.com")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if is {
|
||||
// do something?
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
30
checker.go
Normal file
30
checker.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package checker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsValid preform a simple validation check to see if the email is correctly formatted.
|
||||
// An error is returned if not.
|
||||
func IsValid(email string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsGeneric will check if the email address is provided by a free services provider.
|
||||
// As an example will return true if the email is provided by gmail.com
|
||||
func IsGeneric(email string) (bool, error) {
|
||||
parts := strings.Split(email, "@")
|
||||
|
||||
if len(parts) <= 1 {
|
||||
return false, fmt.Errorf("given an invalid email")
|
||||
}
|
||||
|
||||
for _, d := range domains {
|
||||
if d == parts[1] {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
53
checker_test.go
Normal file
53
checker_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package checker_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lukerodham/email-checker"
|
||||
)
|
||||
|
||||
func TestIsGeneric(t *testing.T) {
|
||||
tests := []struct {
|
||||
Email string
|
||||
IsGeneric bool
|
||||
ShouldError bool
|
||||
}{
|
||||
{
|
||||
Email: "example@gmail.com",
|
||||
IsGeneric: true,
|
||||
ShouldError: false,
|
||||
},
|
||||
{
|
||||
Email: "example@hotmail.com",
|
||||
IsGeneric: true,
|
||||
ShouldError: false,
|
||||
},
|
||||
{
|
||||
Email: "example@yahoo.com",
|
||||
IsGeneric: true,
|
||||
ShouldError: false,
|
||||
},
|
||||
{
|
||||
Email: "example@google.com",
|
||||
IsGeneric: false,
|
||||
ShouldError: false,
|
||||
},
|
||||
{
|
||||
Email: "yahoo.com",
|
||||
IsGeneric: false,
|
||||
ShouldError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
is, err := checker.IsGeneric(test.Email)
|
||||
|
||||
if err != nil && test.ShouldError == false {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if test.IsGeneric != is {
|
||||
t.Errorf("expect %v to equal %v", test.IsGeneric, is)
|
||||
}
|
||||
}
|
||||
}
|
||||
6107
domains.go
Normal file
6107
domains.go
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user