Skip to content

Commit

Permalink
no need for empty parenthesis when using Long option
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Oct 2, 2024
1 parent 55297ab commit d63a3ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ Package publicid generates and validates NanoID strings designed to be publicly
- [func Alphabet\(a string\) Option](<#Alphabet>)
- [func Attempts\(n int\) Option](<#Attempts>)
- [func Len\(len int\) Option](<#Len>)
- [func Long\(\) Option](<#Long>)
## Constants
Expand All @@ -160,7 +159,7 @@ const (
```
<a name="New"></a>
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L67>)
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L65>)
```go
func New(opts ...Option) (string, error)
Expand All @@ -169,7 +168,7 @@ func New(opts ...Option) (string, error)
New generates a unique nanoID with a length of 8 characters and the given options.
<a name="Validate"></a>
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L91>)
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L89>)
```go
func Validate(id string, opts ...Option) error
Expand All @@ -186,8 +185,14 @@ Option is a function type for configuring ID generation.
type Option func(*config)
```

<a name="Long"></a>Long returns an Option to set the length of the ID to be generated to 12.

```go
var Long Option = Len(LongIDLength)
```

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

```go
func Alphabet(a string) Option
Expand All @@ -213,15 +218,6 @@ func Len(len int) Option

Len returns an Option to set the length of the ID to be generated.

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

```go
func Long() Option
```

Long returns an Option to set the length of the ID to be generated to 12.

Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)


Expand Down
4 changes: 1 addition & 3 deletions publicid.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ func Len(len int) Option {
}

// Long returns an Option to set the length of the ID to be generated to 12.
func Long() Option {
return Len(LongIDLength)
}
var Long Option = Len(LongIDLength)

// Alphabet returns an Option to set the alphabet to be used for ID generation.
func Alphabet(a string) Option {
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.New(publicid.Long())
_, 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.New(publicid.Long(), 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.New(publicid.Long())
id, err := publicid.New(publicid.Long)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := publicid.Validate(id, publicid.Long())
err := publicid.Validate(id, publicid.Long)
if err != nil {
b.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions publicid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ func TestNewWithAttempts(t *testing.T) {
}

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

func TestNewLongWithAttempts(t *testing.T) {
id, err := New(Long(), Attempts(5))
id, err := New(Long, Attempts(5))
if err != nil {
t.Errorf("New(Long(), Attempts(5)) returned an error: %v", err)
}
if len(id) != LongIDLength {
t.Errorf("New(Long(), Attempts(5)) returned id with length %d, want %d", len(id), LongIDLength)
}
if err := Validate(id, Long()); err != nil {
if err := Validate(id, Long); err != nil {
t.Errorf("New(Long(), Attempts(5)) returned invalid id: %v", err)
}
}
Expand All @@ -83,13 +83,13 @@ func TestValidate(t *testing.T) {
wantError bool
}{
{"Valid Default ID", "abCD1234", nil, false},
{"Valid Long ID", "abCD1234EFGH", []Option{Long()}, 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},
{"Invalid Long ID", "abCD1234EF", []Option{Long}, true},
}

for _, tc := range testCases {
Expand Down

0 comments on commit d63a3ad

Please sign in to comment.