mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 06:04:35 +00:00
Rename uuid.lua to identifier.lua
This commit is contained in:
parent
71e9e45724
commit
6e277df64a
@ -5,7 +5,7 @@ package.path = package.path .. ";../?.lua"
|
||||
|
||||
local assert = require("batteries.assert")
|
||||
local tablex = require("batteries.tablex")
|
||||
local uuid = require("batteries.uuid")
|
||||
local identifier = require("batteries.identifier")
|
||||
|
||||
-- tablex {{{
|
||||
|
||||
@ -159,7 +159,7 @@ end
|
||||
|
||||
local function test_uuid4()
|
||||
for i = 1, 100 do
|
||||
local id = uuid.uuid4()
|
||||
local id = identifier.uuid4()
|
||||
|
||||
-- right len
|
||||
assert(#id == 36)
|
||||
@ -182,12 +182,12 @@ end
|
||||
|
||||
local function test_ulid()
|
||||
for i = 1, 100 do
|
||||
local ulid = assert(uuid.ulid())
|
||||
local ulid = assert(identifier.ulid())
|
||||
|
||||
-- right len
|
||||
assert(#ulid == 26)
|
||||
-- have the same timestamp with the same time
|
||||
local a, b = uuid.ulid(nil, 1):sub(1, 10), uuid.ulid(nil, 1):sub(1, 10)
|
||||
local a, b = identifier.ulid(nil, 1):sub(1, 10), identifier.ulid(nil, 1):sub(1, 10)
|
||||
assert(a == b)
|
||||
-- don't have characters out of crockford base32
|
||||
assert(not ulid:match("[ILOU%l]"))
|
||||
|
@ -1,5 +1,5 @@
|
||||
--[[
|
||||
uuid/ulid generation
|
||||
identifier generation
|
||||
|
||||
uuid is version 4, ulid is an alternative to uuid (see
|
||||
https://github.com/ulid/spec).
|
||||
@ -9,9 +9,9 @@
|
||||
within the same second yet
|
||||
]]
|
||||
|
||||
local path = (...):gsub("uuid", "")
|
||||
local path = (...):gsub("identifier", "")
|
||||
|
||||
local uuid = {}
|
||||
local identifier = {}
|
||||
|
||||
--(internal; use a provided random generator object, or not)
|
||||
local function _random(rng, ...)
|
||||
@ -23,7 +23,7 @@ end
|
||||
local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
||||
|
||||
--generate a UUID version 4
|
||||
function uuid.uuid4(rng)
|
||||
function identifier.uuid4(rng)
|
||||
--x should be 0x0-0xf, the single y should be 0x8-0xb
|
||||
--4 should always just be 4 (denoting uuid version)
|
||||
local out = uuid4_template:gsub("[xy]", function (c)
|
||||
@ -54,7 +54,7 @@ end
|
||||
|
||||
--generate an ULID using this rng at this time (now by default)
|
||||
--implementation based on https://github.com/Tieske/ulid.lua
|
||||
function uuid.ulid(rng, time)
|
||||
function identifier.ulid(rng, time)
|
||||
time = math.floor((time or _now()) * 1000)
|
||||
|
||||
local time_part = {}
|
||||
@ -73,4 +73,4 @@ function uuid.ulid(rng, time)
|
||||
return table.concat(time_part) .. table.concat(random_part)
|
||||
end
|
||||
|
||||
return uuid
|
||||
return identifier
|
Loading…
Reference in New Issue
Block a user