Implementation of TypeID in Lua

luacheck

Changed files
+11 -11
spec
+8 -8
spec/uuid7_spec.lua
··· 7 7 assert.are.equal(16, #bytes) 8 8 9 9 -- all values are valid bytes (0-255) 10 - for i, byte in ipairs(bytes) do 10 + for _, byte in ipairs(bytes) do 11 11 assert.is_true(byte >= 0 and byte <= 255) 12 12 end 13 13 ··· 17 17 -- variant, 9th byte 18 18 assert.are.equal(2, (bytes[9] >> 6) & 0x3) 19 19 end) 20 - 20 + 21 21 it("converts a byte table to a valid UUID string", function() 22 22 local bytes = { 23 23 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x70, 0x08, 24 24 0x80, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 25 25 } 26 - 26 + 27 27 local uuid_str = uuid7.to_string(bytes) 28 - 28 + 29 29 assert.are.equal("01020304-0506-7008-800a-0b0c0d0e0f10", uuid_str) 30 30 end) 31 - 31 + 32 32 it("generates a valid UUIDv7 string", function() 33 33 local uuid_str = uuid7.generate() 34 - 34 + 35 35 -- Check format: 8-4-4-4-12 (36 chars with hyphens) 36 36 assert.are.equal(36, #uuid_str) 37 37 assert.is_true(uuid_str:match("^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$") ~= nil) 38 - 38 + 39 39 -- Check version (position 15, should be 7) 40 40 assert.are.equal("7", uuid_str:sub(15, 15)) 41 - 41 + 42 42 -- Check variant (position 20, should be 8, 9, a, or b) 43 43 assert.is_true(uuid_str:sub(20, 20):match("[89ab]") ~= nil) 44 44 end)
+2 -2
typeid.lua
··· 1 1 local Base32 = require("base32") 2 - local millitime = require("millitime") 3 2 local UUID7 = require("uuid7") 4 3 5 4 local TypeID = {} ··· 95 94 local uuid_bytes = {} 96 95 97 96 -- parse groups from UUID string like "01893726-efee-7f02-8fbf-9f2b7bc2f910" 98 - local g1, g2, g3, g4, g5 = uuid_str:match("(%x%x%x%x%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x%x%x%x%x%x%x%x%x)") 97 + local uuid_template = "(%x%x%x%x%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x)%-(%x%x%x%x%x%x%x%x%x%x%x%x)" 98 + local g1, g2, g3, g4, g5 = uuid_str:match(uuid_template) 99 99 100 100 if not g1 then 101 101 error("invalid UUID format")
+1 -1
uuid7.lua
··· 42 42 r8, 43 43 44 44 -- 9th byte: random mask with variant 45 - 0x80 | (r9 & 0x3f), 45 + 0x80 | (r9 & VARIANT_RFC4122), 46 46 47 47 -- 10th byte: random 48 48 r10,