Coder Social home page Coder Social logo

nanoid's Introduction

nanoid

Golang port of ai/nanoid (originally written in JavaScript).

Build Status GoDoc Go Report Card

Description

A tiny, secure URL-friendly unique string ID generator for Golang.

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Compact. It uses more symbols than UUID (A-Za-z0-9_~) and has the same number of unique options in just 22 symbols instead of 36.

No third party dependencies No need to pollute your $GOPATH

Install

$ go get github.com/jkomyno/nanoid

Testing

$ go test -v -bench=.

You should be able to see a log similar to the following one:

=== RUN   TestGeneratesURLFriendlyIDs
--- PASS: TestGeneratesURLFriendlyIDs (0.00s)
=== RUN   TestHasNoCollisions
--- PASS: TestHasNoCollisions (0.21s)
=== RUN   TestFlatDistribution
--- PASS: TestFlatDistribution (0.33s)
goos: linux
goarch: amd64
pkg: github.com/jkomyno/nanoid
BenchmarkNanoid-4        1000000              1704 ns/op
PASS
ok      github.com/jkomyno/nanoid       2.265s

Usage

This packages tries to offer an API as close as possible to the original JS module.

Normal

The Nanoid() function uses URL-friendly symbols (A-Za-z0-9_~) and returns an ID with 22 characters (to have the same collisions probability as UUID v4). Please note that it also returns an error, which (hopefully) will be nil.

import "github.com/jkomyno/nanoid"

id, err := nanoid.Nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"

Symbols -,.() are not encoded in URL, but in the end of a link they could be identified as a punctuation symbol.

If you want to reduce ID length (and increase collisions probability), you can pass length as argument:

import "github.com/jkomyno/nanoid"

id, err := nanoid.Nanoid(10) //=> "IRFa~VaY2b"

Custom Alphabet or Length

If you want to change the ID alphabet or the length you can use low-level Generate function.

import "github.com/jkomyno/nanoid"

id, err := nanoid.Generate("1234567890abcdef", 10) //=> "4f90d13a42"

Alphabet must contain less than 256 symbols.

Custom Random Bytes Generator

You can replace the default safe random generator using the Format function.

import (
    "crypto/rand"

    "github.com/jkomyno/nanoid"
)

func random(size int) ([]byte, error) {
	var randomBytes = make([]byte, size)
	_, err := rand.Read(randomBytes)
	return randomBytes, err
}

id, err := nanoid.Format(random, "abcdef", 10) //=> "fbaefaadeb"

Note that random function must follow this spec:

type RandomType func(int) ([]byte, error)

If you want to use the same URL-friendly symbols with format, or take a look at the other defaults value, you can use GetDefaults.

import "github.com/jkomyno/nanoid"

var defaults *nanoid.DefaultsType
defaults = nanoid.GetDefaults()
/*
	&DefaultsType{
		Alphabet: "_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
		Size:     22,
		MaskSize: 5,
	}
*/

Credits

ai - nanoid

License

The MIT License (MIT). Please see License File for more information.

nanoid's People

Contributors

jkomyno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mattxyzeth

nanoid's Issues

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.