Skip to content

Commit

Permalink
Cli options and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayvadher committed Jul 18, 2024
1 parent ae9b6bd commit 7a25dc7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
# cuid2
Next generation guids. Secure, collision-resistant ids optimized for horizontal scaling and performance.

Next generation guids.
Secure, collision-resistant ids optimized for horizontal scaling and
performance.

Original author and reference: https://github.com/paralleldrive/cuid2

# package

```shell
go get github.com/akshayvadher/cuid2
```

## usage

```go
package main

import (
"fmt"
"github.com/akshayvadher/cuid2"
)

func main() {
id := cuid2.CreateId()
fmt.Println(id)
// to create default length id
// us1hfvvf2uyzmh031bav6skw

idWithLength := cuid2.CreateIdOf(10)
// zev57ezp7c
}
````

###

# cli

```shell
go install github.com/akshayvadher/cuid2/cmd/cuid2@latest
```

## usage

```shell
cuid2
# wldu51x7wn6baulkeq49qfm7
cuid2 -n 5
# generates 5 ids
# s7dseq8y5ti85c02eptzia1p
# wz4rk8nj39dpyd01gddsp9rz
# kwezj4wa69d6ta7jxg3b6lnz
# pzuju2hk01xpev6ixnnnsqba
# enxpfer2u7c00xa24li0jghc
cuid2 -len 10
# generates id with length 10
# h8crfzyp6q
cuid2 -n 3 -len 11
# generates 3 ids with length 11
# ijlk68norem
# redx1s0adbb
# mk5zg8dxgi1
```
13 changes: 12 additions & 1 deletion cmd/cuid2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ func main() {
Value: 1,
Usage: "Numbers of ids to generate",
},
&cli.IntFlag{
Name: "len",
Value: 24,
Usage: "Length of the Id (between 2 and 36)",
Action: func(ctx *cli.Context, v int) error {
if v > 36 || v < 2 {
return fmt.Errorf("len %v should be between 2 and 36", v)
}
return nil
},
},
},
Action: func(cCtx *cli.Context) error {
for range cCtx.Int("n") {
fmt.Println(cuid2.CreateId())
fmt.Println(cuid2.CreateIdOf(cCtx.Int("len")))
}
return nil
},
Expand Down
4 changes: 4 additions & 0 deletions cuid2.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func CreateId() string {
return defaultInit()
}

func CreateIdOf(len int) string {
return Init(DefaultRandom, DefaultCounter, len, DefaultFingerprint)()
}

func IsCuid(id string) bool {
minLength := 2
maxLength := bigLength
Expand Down
16 changes: 15 additions & 1 deletion cuid2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestConfigurability(t *testing.T) {
}
}

func TestLength(t *testing.T) {
func TestLengthWithFullCustomization(t *testing.T) {
minLen := 2
maxLen := 32
for tt := minLen; tt <= maxLen; tt++ {
Expand All @@ -116,3 +116,17 @@ func TestLength(t *testing.T) {
})
}
}

func TestLengthOnlyCustomization(t *testing.T) {
minLen := 2
maxLen := 32
for tt := minLen; tt <= maxLen; tt++ {
t.Run(fmt.Sprintf("with length %d", tt), func(t *testing.T) {
id := CreateIdOf(tt)
fmt.Printf("Created id %q\n", id)
if len(id) != tt {
t.Errorf("Expected id of len %d. Got %d\n", tt, len(id))
}
})
}
}

0 comments on commit 7a25dc7

Please sign in to comment.