/alphakey

Handy little library for converting ints to short strings (for things like url shorteners and the like)

Primary LanguageGo

AlphaKey

For a Codename project I was working on I needed human readable/shareable short keys, so I ended up reimplementing the solution found on this stack overflow answer about "Bijective Functions" in Go and figured it might be of use to anyone else who needs to put together a url shortener or the like.

Usage

To get going, just import this

import "github.com/joho/alphakey"

You can either call a couple of the helper functions directly

key := alphakey.GetKeyForInt(198898042) // returns "ninja"
num := alphakey.GetIntForKey("dgt")     // returns 1337

Or if you want a bit more control you can instantiate your own converter and control the alphabet used and any offset you might want

converter := &alphakey.Converter{
  alphakey.UnambiguousLowercaseAlphabet, // all lower case, no l or o, because humans
  497,                                   // an arbitrary offset so early sharers don't get a or b
}

key := converter.GetKey(1988483) // returns "ninja"
num := converter.GetInt("emq")   // returns 1337

You can also check the godocs for AlphaKey

Build Status

wercker status


© 2013 John Barton, MIT Licenced