Skip to content

Commit

Permalink
refactor extra methods out
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 17, 2024
1 parent 1c21a9e commit 6e259a0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 79 deletions.
30 changes: 5 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fmt.Println("Generated default public ID:", id)
3. Generate a long public ID (12 characters):
```go
longID, _ := publicid.NewLong()
longID, _ := publicid.New(publicid.Long())
fmt.Println("Generated long public ID:", longID)
// Output: Generated long public ID: 7Zt3xY9pQr5W
```
Expand Down Expand Up @@ -104,7 +104,7 @@ if err != nil {
```go
longID := "7Zt3xY9pQr5W"
err := publicid.ValidateLong(longID)
err := publicid.Validate(longID, publicid.Long())
if err != nil {
fmt.Println("Invalid long ID:", err)
} else {
Expand All @@ -129,9 +129,7 @@ Package publicid generates and validates NanoID strings designed to be publicly
- [Constants](<#constants>)
- [func New\(opts ...Option\) \(string, error\)](<#New>)
- [func NewLong\(opts ...Option\) \(string, error\)](<#NewLong>)
- [func Validate\(id string\) error](<#Validate>)
- [func ValidateLong\(id string\) error](<#ValidateLong>)
- [func Validate\(id string, opts ...Option\) error](<#Validate>)
- [type Option](<#Option>)
- [func Alphabet\(a string\) Option](<#Alphabet>)
- [func Attempts\(n int\) Option](<#Attempts>)
Expand Down Expand Up @@ -170,33 +168,15 @@ func New(opts ...Option) (string, error)
New generates a unique nanoID with a length of 8 characters and the given options.
<a name="NewLong"></a>
## func [NewLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L70>)
```go
func NewLong(opts ...Option) (string, error)
```
NewLong generates a unique nanoID with a length of 12 characters and the given options.
<a name="Validate"></a>
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L94>)
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L91>)
```go
func Validate(id string) error
func Validate(id string, opts ...Option) error
```
Validate checks if a given field name's public ID value is valid according to the constraints defined by package publicid.

<a name="ValidateLong"></a>
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L98>)

```go
func ValidateLong(id string) error
```

validateLong checks if a given field name's public ID value is valid according to the constraints defined by package publicid.
<a name="Option"></a>
## type [Option](<https://github.com/agentstation/publicid/blob/master/publicid.go#L31>)

Expand Down
19 changes: 8 additions & 11 deletions publicid.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ func Alphabet(a string) Option {
// New generates a unique nanoID with a length of 8 characters and the given options.
func New(opts ...Option) (string, error) { return generateID(DefaultIDLength, opts...) }

// NewLong generates a unique nanoID with a length of 12 characters and the given options.
func NewLong(opts ...Option) (string, error) { return generateID(LongIDLength, opts...) }

// generateID is a helper function to generate IDs with the given length and options.
func generateID(len int, opts ...Option) (string, error) {
// set default configuration values
Expand All @@ -91,24 +88,24 @@ func generateID(len int, opts ...Option) (string, error) {

// Validate checks if a given field name's public ID value is valid according to
// the constraints defined by package publicid.
func Validate(id string) error { return validate(id, DefaultIDLength) }

// validateLong checks if a given field name's public ID value is valid according to
// the constraints defined by package publicid.
func ValidateLong(id string) error { return validate(id, LongIDLength) }
func Validate(id string, opts ...Option) error { return validate(id, opts...) }

// isValidChar checks if a given character is a valid public ID character.
func isValidChar(c rune) bool {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
}

// validate checks if a given public ID value is valid.
func validate(id string, expectedLen int) error {
func validate(id string, opts ...Option) error {
if id == "" { // if the ID is empty, it's not valid
return fmt.Errorf("public ID is empty")
}
if len(id) != expectedLen { // if the ID is not the expected length, it's not valid
return fmt.Errorf("public ID has length %d, want %d", len(id), expectedLen)
cfg := &config{length: DefaultIDLength}
for _, opt := range opts {
opt(cfg)
}
if len(id) != cfg.length { // if the ID is not the expected length, it's not valid
return fmt.Errorf("public ID has length %d, want %d", len(id), cfg.length)
}
for _, char := range id {
if !isValidChar(char) { // if the ID contains an invalid character, it's not valid
Expand Down
8 changes: 4 additions & 4 deletions publicid_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func BenchmarkNewWithAttempts(b *testing.B) {

func BenchmarkLong(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := publicid.NewLong()
_, err := publicid.New(publicid.Long())
if err != nil {
b.Fatal(err)
}
Expand All @@ -35,7 +35,7 @@ func BenchmarkLong(b *testing.B) {

func BenchmarkLongWithAttempts(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := publicid.NewLong(publicid.Attempts(5))
_, err := publicid.New(publicid.Long(), publicid.Attempts(5))
if err != nil {
b.Fatal(err)
}
Expand All @@ -57,13 +57,13 @@ func BenchmarkValidate(b *testing.B) {
}

func BenchmarkValidateLong(b *testing.B) {
id, err := publicid.NewLong()
id, err := publicid.New(publicid.Long())
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := publicid.ValidateLong(id)
err := publicid.Validate(id, publicid.Long())
if err != nil {
b.Fatal(err)
}
Expand Down
73 changes: 34 additions & 39 deletions publicid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,72 +35,67 @@ func TestNewWithAttempts(t *testing.T) {
}

func TestLong(t *testing.T) {
id, err := NewLong()
id, err := New(Long())
if err != nil {
t.Errorf("Long() returned an error: %v", err)
t.Errorf("New(Long()) returned an error: %v", err)
}
if len(id) != 12 {
t.Errorf("Long() returned id with length %d, want 12", len(id))
if len(id) != LongIDLength {
t.Errorf("New(Long()) returned id with length %d, want %d", len(id), LongIDLength)
}
if err := ValidateLong(id); err != nil {
t.Errorf("Long() returned invalid id: %v", err)
if err := Validate(id, Long()); err != nil {
t.Errorf("New(Long()) returned invalid id: %v", err)
}
}

func TestLongWithAttempts(t *testing.T) {
id, err := NewLong(Attempts(5))
id, err := New(Long(), Attempts(5))
if err != nil {
t.Errorf("Long(Attempts(5)) returned an error: %v", err)
t.Errorf("New(Long(), Attempts(5)) returned an error: %v", err)
}
if len(id) != 12 {
t.Errorf("Long(Attempts(5)) returned id with length %d, want 12", len(id))
if len(id) != LongIDLength {
t.Errorf("New(Long(), Attempts(5)) returned id with length %d, want %d", len(id), LongIDLength)
}
if err := ValidateLong(id); err != nil {
t.Errorf("Long(Attempts(5)) returned invalid id: %v", err)
if err := Validate(id, Long()); err != nil {
t.Errorf("New(Long(), Attempts(5)) returned invalid id: %v", err)
}
}

func TestValidate(t *testing.T) {
testCases := []struct {
name string
id string
wantError bool
}{
{"Valid ID", "abCD1234", false},
{"Empty ID", "", true},
{"Short ID", "abc123", true},
{"Long ID", "abcDEF123456", true},
{"Invalid char", "abCD12_4", true},
func TestNewWithCustomLength(t *testing.T) {
customLength := 10
id, err := New(Len(customLength))
if err != nil {
t.Errorf("New(Len(%d)) returned an error: %v", customLength, err)
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := Validate(tc.id)
if (err != nil) != tc.wantError {
t.Errorf("Validate() error = %v, wantError %v", err, tc.wantError)
}
})
if len(id) != customLength {
t.Errorf("New(Len(%d)) returned id with length %d, want %d", customLength, len(id), customLength)
}
if err := Validate(id, Len(customLength)); err != nil {
t.Errorf("New(Len(%d)) returned invalid id: %v", customLength, err)
}
}

func TestValidateLong(t *testing.T) {
func TestValidate(t *testing.T) {
testCases := []struct {
name string
id string
opts []Option
wantError bool
}{
{"Valid ID", "abCD1234EFGH", false},
{"Empty ID", "", true},
{"Short ID", "abcDEF123", true},
{"Long ID", "abcDEF123456789", true},
{"Invalid char", "abCD1234EF_H", true},
{"Valid Default ID", "abCD1234", nil, false},
{"Valid Long ID", "abCD1234EFGH", []Option{Long()}, false},
{"Valid Custom Length ID", "abCD123456", []Option{Len(10)}, false},
{"Empty ID", "", nil, true},
{"Short ID", "abc123", nil, true},
{"Long ID", "abcDEF123456", nil, true},
{"Invalid char", "abCD12_4", nil, true},
{"Invalid Long ID", "abCD1234EF", []Option{Long()}, true},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateLong(tc.id)
err := Validate(tc.id, tc.opts...)
if (err != nil) != tc.wantError {
t.Errorf("ValidateLong() error = %v, wantError %v", err, tc.wantError)
t.Errorf("Validate() error = %v, wantError %v", err, tc.wantError)
}
})
}
Expand Down

0 comments on commit 6e259a0

Please sign in to comment.