Giter Site home page Giter Site logo

go-nanoid's Introduction

👋 Hey! I am a software engineer at SumUp. I work on Identity & Access Management, observability, and public APIs. We have a team full of wonderful and talented people, so if you are looking for a job or want to chat about any random topic, reach out to me at matous.dzivjak @ sumup.com.

I write in Go and Rust. I use Helix as my driver and also contribute to the development when I find some time.

There's more at my personal website.

go-nanoid's People

Contributors

bayandin avatar dependabot-preview[bot] avatar epk avatar matoous avatar mrozean avatar nyedidikeke avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-nanoid's Issues

Haven't handle edge cases correctly.

This is got into deadlock.

package main

import (
	"fmt"

	"github.com/matoous/go-nanoid"
)

func main() {
	gonanoid.Size(0)
	gonanoid.Alphabet("abcdefghijklmnopqrstuvwxyz")
	id := gonanoid.Generate()
	fmt.Printf("ID: %s", id)
}

And this is got panic.

package main

import (
	"fmt"

	"github.com/matoous/go-nanoid"
)

func main() {
	gonanoid.Alphabet("abcdefghijklmnopqrstuvwxyz")
	gonanoid.Size(0)
	id := gonanoid.Generate()
	fmt.Printf("ID: %s", id)
}

similar error when we are using gonanoid.Alphabet("")

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/lint.yml
  • actions/checkout v2
  • golangci/golangci-lint-action v2
.github/workflows/test.yml
  • actions/setup-go v2
  • actions/checkout v2
  • actions/setup-go v2
  • actions/checkout v2
gomod
go.mod
  • go 1.15
  • github.com/stretchr/testify v1.7.0

  • Check this box to trigger a request for Renovate to run again on this repository

Importing v2.0.0

Hi,
When I go get github.com/matoous/go-nanoid it retrieves the version 1.5.0.

go: downloading github.com/matoous/go-nanoid v1.5.0
go: github.com/matoous/go-nanoid upgrade => v1.5.0

I would like to use the most recent version (aka 2.0.0) but I'm not sure how to download/add it to my project.

Why is the default length 22?

I looked at the primary implementation as well as the implementation for other languages, and the default length for the id is 21. Is there specific reason to make it 22 in this library? In my opinion, it should be consistent with rest of them.

Broken release?

This dependency don't working

require (
	github.com/matoous/go-nanoid v1.4.0
)

go: github.com/matoous/[email protected]: reading github.com/matoous/go-nanoid/go.mod at revision v1.4.0: unknown revision v1.4.0

But working this.

require (
	github.com/matoous/go-nanoid v.1.4.0
)

Which change automatically in my go.mod to:

require (
	github.com/matoous/go-nanoid v1.3.1-0.20200518070138-513938f8e67d
)

Lack of Must / New on API

Hello!

I'm migrating from Google's UUID Package, and there is an specific detail that I find odd: the lack of Must and/or New on the API. These two functions are useful when you're quickly prototyping or when you DO want the entire world to blown up in case of errors.

These two functions have the following signature:

func Must (id string, err error) string // panics if err is not null
func New() string // returns a new id or panics

Is this feature acceptable? If so, I already made the changes locally on my machine and can send you a PR.

Non ASCII ID's

I try to use this library to generate simple human readable ID's on my native language.
Using library according to documentation with non ascii alphabet:
id, err := gonanoid.Generate("абвгдежиклмнпрстуфхшэюя0123456789-", 10)
Gives me byte garbage with wrong length

3ю�ѽ�д0
�Џ�51�б7
���Ѷ0�Ѻ�

Instead normal ID like:

2юбнг4внеэ

Original implementation has no this problem https://zelark.github.io/nano-id-cc/

Nanoid() with negative argument panics

If Nanoid() function is called with a negative argument, the function panics.

E.g.

gonanoid.Nanoid(-1)

panics with:

panic: runtime error: makeslice: len out of range [recovered]                           
        panic: runtime error: makeslice: len out of range 

The function signature already allows to return an error so I suggest to return an error in this case instead of letting it panic.

Additionally, due to the (in my opinion not very Go ideomatic) function signature, it is possible to call this function with more than one argument. I suggest to return an error in this case as well.

I can provide a PR if you wish.

func naming suggestion

wouldnt it be better to use gonanoid.New(), gonanoid.Generate() or if the package had different name generator.Nanoid() or generator.NewNanoid()?

API 2.0

Let’s update the library to be compatible with the latest Nano ID 2.0:

  • Replace ~ to - in default alphabet
  • Add non-secure fast generator
  • Async API?

New discards 25% of the random bits

New uses bits 0-6 of each random byte (masks with 63) and the two high random bits are discarded.

I think it would be better not to discard randomness.

go-nanoid/gonanoid.go

Lines 89 to 97 in 0cc1b25

bytes := make([]byte, size)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
id := make([]rune, size)
for i := 0; i < size; i++ {
id[i] = defaultAlphabet[bytes[i]&63]
}

It is possible to write a loop and output 4 characters per each 3 random bytes and handle the remainder separately.

Or maybe reuse the base64 encoding code from standard library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.