CUID generator for Lua.
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.travis-ci
spec
src
.busted
.luacheckrc
.travis.yml
LICENSE
Makefile
README.md docs: pretty build badges/flags on README Apr 13, 2018
cuid-0.5-1.rockspec

README.md

CUID

CUID generator for Lua.

Travis Coveralls github LuaRocks license PRs Welcome

For more information, see: http://usecuid.org

Installation

If available on LuaRocks:

$ luarocks --local install cuid

Otherwise, you could install through this root project directory:

$ luarocks --local make

Usage

To load this library, just type:

local cuid = require ("cuid")

Once loaded, you can generate fresh CUIDs through:

local id = cuid.generate ( )

As an example of CUID, we have c00p6qup20000ckkzslahp5pn, where:

  • c is the CUID prefix (so it's a valid variable).
  • 00p6qup2 is a timestamp/continuous number.
  • 0000 is the internal sequential counter.
  • ckkz is the machine/host fingerprint.
  • slahp5pn are pseudo-random numbers.

To generate slugs (shorter and collision-weak CUIDs), just type the following:

local slug = cuid.slug ( )

Slugs are made of 8 characters, and there's no prefix (as is the case of CUIDs - "c" in the case). So, slugs are often used as suffixes, for instance, on URLs.

Configuration

You could as well set a custom fingerprint for CUID generation by an environment variable. This environment variable is called LUA_CUID_FINGERPRINT.

Whenever the cuid library is loaded, it will lookup such environment variable to generate a fingerprint if it is indeed defined.

It's also possible to set a custom fingerprint prior CUID/slug generation, just type the following for both cases:

local cuid = require 'cuid'

local fingerprint_input = "It's me, Mario!"

local id   = cuid.generate (fingerprint_input)
local slug = cuid.slug (fingerprint_input)

Such fingerprint text is used only once per function call, that is, on the next function call, everything remains the same of previous state prior custom fingerprint's function call.

Remarks

Pull requests and issues are welcome! Happy hacking!