From a089b160a25c6df36e5d32fd355906a6d46ff2a6 Mon Sep 17 00:00:00 2001 From: Josh Perry Date: Thu, 26 May 2022 14:05:04 +0100 Subject: [PATCH 01/20] uuid: added a uuid module Only contains a function for (the most useful) uuid4 at present. --- uuid.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 uuid.lua diff --git a/uuid.lua b/uuid.lua new file mode 100644 index 0000000..beadbf7 --- /dev/null +++ b/uuid.lua @@ -0,0 +1,31 @@ +--[[ + uuid generation +]] + +local path = (...):gsub("uuid", "") + +local uuid = {} + +--helper for optionally passed random; defaults to love.math.random if present, otherwise math.random +local _global_random = math.random +if love and love.math and love.math.random then + _global_random = love.math.random +end + +local function _random(min, max, r) + return r and r:random(min, max) + or _global_random(min, max) +end + +local uuid4Template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" + +--generate a UUID version 4 (random) +function uuid.uuid4() + return uuid4Template:gsub("[xy]", function (c) + -- x should be 0x0-0xF, the single y should be 0x8-0xB + -- 4 should always just be 4 (denoting uuid version) + return string.format("%x", c == "x" and _random(0x0, 0xF) or _random(0x8, 0xB)) + end) +end + +return uuid From 5f2e7b437da4e8ae679ebb14cac21228ff4c13f2 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:02:32 -0300 Subject: [PATCH 03/20] uuid: Fix variable casing --- uuid.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uuid.lua b/uuid.lua index beadbf7..aaced33 100644 --- a/uuid.lua +++ b/uuid.lua @@ -17,11 +17,11 @@ local function _random(min, max, r) or _global_random(min, max) end -local uuid4Template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" +local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" --generate a UUID version 4 (random) function uuid.uuid4() - return uuid4Template:gsub("[xy]", function (c) + return uuid4_template:gsub("[xy]", function (c) -- x should be 0x0-0xF, the single y should be 0x8-0xB -- 4 should always just be 4 (denoting uuid version) return string.format("%x", c == "x" and _random(0x0, 0xF) or _random(0x8, 0xB)) From e630418372cf5ee48e5c24e22e011d07bbcee25d Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:06:07 -0300 Subject: [PATCH 04/20] uuid: Add a passed rng to uuid4 --- uuid.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uuid.lua b/uuid.lua index aaced33..c4f07c1 100644 --- a/uuid.lua +++ b/uuid.lua @@ -20,11 +20,11 @@ end local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" --generate a UUID version 4 (random) -function uuid.uuid4() +function uuid.uuid4(rng) return uuid4_template:gsub("[xy]", function (c) -- x should be 0x0-0xF, the single y should be 0x8-0xB -- 4 should always just be 4 (denoting uuid version) - return string.format("%x", c == "x" and _random(0x0, 0xF) or _random(0x8, 0xB)) + return string.format("%x", c == "x" and _random(0x0, 0xF, rng) or _random(0x8, 0xB, rng)) end) end From 57bc1a2edbf5f0bf174e6c20ca55262b2e6c1088 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:11:53 -0300 Subject: [PATCH 05/20] uuid: Use mathx's _random instead of stringx's --- uuid.lua | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/uuid.lua b/uuid.lua index c4f07c1..d341eee 100644 --- a/uuid.lua +++ b/uuid.lua @@ -6,15 +6,11 @@ local path = (...):gsub("uuid", "") local uuid = {} ---helper for optionally passed random; defaults to love.math.random if present, otherwise math.random -local _global_random = math.random -if love and love.math and love.math.random then - _global_random = love.math.random -end - -local function _random(min, max, r) - return r and r:random(min, max) - or _global_random(min, max) +--(internal; use a provided random generator object, or not) +local function _random(rng, ...) + if rng then return rng:random(...) end + if love then return nlove.math.random(...) end + return math.random(...) end local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" @@ -24,7 +20,7 @@ function uuid.uuid4(rng) return uuid4_template:gsub("[xy]", function (c) -- x should be 0x0-0xF, the single y should be 0x8-0xB -- 4 should always just be 4 (denoting uuid version) - return string.format("%x", c == "x" and _random(0x0, 0xF, rng) or _random(0x8, 0xB, rng)) + return string.format("%x", c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB)) end) end From 2642c304adfa5a392aef97c8f89a25e11ca0a8a6 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:23:11 -0300 Subject: [PATCH 06/20] uuid: Discard gsub's 2nd return value --- uuid.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/uuid.lua b/uuid.lua index d341eee..18ea0a2 100644 --- a/uuid.lua +++ b/uuid.lua @@ -17,11 +17,13 @@ local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" --generate a UUID version 4 (random) function uuid.uuid4(rng) - return uuid4_template:gsub("[xy]", function (c) - -- x should be 0x0-0xF, the single y should be 0x8-0xB - -- 4 should always just be 4 (denoting uuid version) + -- 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) return string.format("%x", c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB)) end) + + return out end return uuid From 370b33b96fa6e96ee60242fc1cf00b2a6b32acd2 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:25:26 -0300 Subject: [PATCH 07/20] woops typo --- uuid.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uuid.lua b/uuid.lua index 18ea0a2..efbf6a0 100644 --- a/uuid.lua +++ b/uuid.lua @@ -9,7 +9,7 @@ local uuid = {} --(internal; use a provided random generator object, or not) local function _random(rng, ...) if rng then return rng:random(...) end - if love then return nlove.math.random(...) end + if love then return love.math.random(...) end return math.random(...) end From 3c959caa239249f62d03d3ac5b0df3d7ee9cc593 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:41:02 -0300 Subject: [PATCH 08/20] uuid: Slight style fix Don't need to capture the 2nd value oops, also separated the format call into multiple lines --- uuid.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/uuid.lua b/uuid.lua index efbf6a0..8f8339d 100644 --- a/uuid.lua +++ b/uuid.lua @@ -19,8 +19,11 @@ local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" function uuid.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) - return string.format("%x", c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB)) + local out = uuid4_template:gsub("[xy]", function (c) + return string.format( + "%x", + c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB) + ) end) return out From 4d8d69b729a5c0cd87fdd0c4960b13193969c593 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 13:16:54 -0300 Subject: [PATCH 09/20] tests: Add some uuid4 tests --- .test/tests.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.test/tests.lua b/.test/tests.lua index 6b96236..e379429 100644 --- a/.test/tests.lua +++ b/.test/tests.lua @@ -5,6 +5,7 @@ package.path = package.path .. ";../?.lua" local assert = require("batteries.assert") local tablex = require("batteries.tablex") +local uuid = require("batteries.uuid") -- tablex {{{ @@ -155,3 +156,26 @@ local function test_spairs() 10, 8, 7 })) end + +local function test_uuid4() + for i = 1, 100 do + local id = uuid.uuid4() + + -- right len + assert(#id == 36) + -- right amount of non hyphen characters + assert(#id:gsub("-", "") == 32) + + -- 15th char is always a 4 + assert(id:sub(15, 15) == "4") + -- 20th char is always between 0x8 and 0xb + local y = tonumber("0x" .. id:sub(20, 20)) + assert(y >= 0x8 and y <= 0xb) + + -- everything is a valid 8 bit num + for char in id:gsub("-", ""):gmatch(".") do + local num = assert(tonumber("0x" .. char)) + assert(num >= 0 and num <= 0xf) + end + end +end From 228620d61c7c9c79dbc50efafdabacf68508d6ad Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 13:51:12 -0300 Subject: [PATCH 10/20] uuid: Add an ulid func + helper funcs --- uuid.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/uuid.lua b/uuid.lua index 8f8339d..13d0a26 100644 --- a/uuid.lua +++ b/uuid.lua @@ -29,4 +29,51 @@ function uuid.uuid4(rng) return out end +-- Crockford's Base32 https://en.wikipedia.org/wiki/Base32 +local _encoding = { + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", + "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z" +} + +--since ulid needs time since unix epoch with miliseconds, we can just +--use socket. if that's not loaded, they'll have to provide their own +local function _now(time_func, ...) + if package.loaded.socket then return package.loaded.socket.gettime(...) end + if require("socket") then return require("socket").gettime(...) end + if time_func then return time_func(...) end + error("assertion failed: socket can't be found and no time function provided") +end + +--generate the time part of an ulid +local function _encode_time(time) + time = math.floor((time or _now()) * 1000) + + local out = {} + + for i = 10, 1, -1 do + local mod = time % #_encoding + out[i] = _encoding[mod + 1] + time = (time - mod) / #_encoding + end + + return table.concat(out) +end + +local function _encode_random(rng) + local out = {} + + for i = 1, 10 do + out[i] = _encoding[math.floor(_random(rng) * #_encoding) + 1] + end + + return table.concat(out) +end + +--generate an ULID (see https://github.com/ulid/spec) +--implementation based on https://github.com/Tieske/ulid.lua +function uuid.ulid(rng, time_func) + return _encode_time() .. _encode_random() +end + return uuid From 04d658d601411d0f2fb4fd4f76c390e96cd241ec Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 14:01:46 -0300 Subject: [PATCH 11/20] uuid: Slightly better docs on ulid --- uuid.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/uuid.lua b/uuid.lua index 13d0a26..9deeea0 100644 --- a/uuid.lua +++ b/uuid.lua @@ -29,7 +29,7 @@ function uuid.uuid4(rng) return out end --- Crockford's Base32 https://en.wikipedia.org/wiki/Base32 +-- crockford's base32 https://en.wikipedia.org/wiki/Base32 local _encoding = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", @@ -60,6 +60,7 @@ local function _encode_time(time) return table.concat(out) end +--generate the random part of an ulid local function _encode_random(rng) local out = {} @@ -70,7 +71,8 @@ local function _encode_random(rng) return table.concat(out) end ---generate an ULID (see https://github.com/ulid/spec) +--generate an ULID using this rng at this time +--see https://github.com/ulid/spec --implementation based on https://github.com/Tieske/ulid.lua function uuid.ulid(rng, time_func) return _encode_time() .. _encode_random() From b39e295eb4f99a5eb9c4e507b5ff6fcaba682473 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 14:02:00 -0300 Subject: [PATCH 12/20] uuid: Add a time arg to ulid --- uuid.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uuid.lua b/uuid.lua index 9deeea0..065a229 100644 --- a/uuid.lua +++ b/uuid.lua @@ -74,8 +74,8 @@ end --generate an ULID using this rng at this time --see https://github.com/ulid/spec --implementation based on https://github.com/Tieske/ulid.lua -function uuid.ulid(rng, time_func) - return _encode_time() .. _encode_random() +function uuid.ulid(rng, time) + return _encode_time(time) .. _encode_random(rng) end return uuid From 8d8b0dc243837482ce65074db33564a32f7139d7 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:02:30 -0300 Subject: [PATCH 13/20] uuid: Fix ulid length --- uuid.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uuid.lua b/uuid.lua index 065a229..4aae349 100644 --- a/uuid.lua +++ b/uuid.lua @@ -64,7 +64,7 @@ end local function _encode_random(rng) local out = {} - for i = 1, 10 do + for i = 1, 16 do out[i] = _encoding[math.floor(_random(rng) * #_encoding) + 1] end From c0aba5027ccad61b9323cec3e13187b8fc497e93 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:02:44 -0300 Subject: [PATCH 14/20] tests: Add ulid tests --- .test/tests.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.test/tests.lua b/.test/tests.lua index e379429..1474625 100644 --- a/.test/tests.lua +++ b/.test/tests.lua @@ -179,3 +179,17 @@ local function test_uuid4() end end end + +local function test_ulid() + for i = 1, 100 do + local ulid = assert(uuid.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) + assert(a == b) + -- don't have characters out of crockford base32 + assert(not ulid:match("[ILOU%l]")) + end +end From b656042ba748bff22eec24cf51fdeba3c75996ac Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:07:50 -0300 Subject: [PATCH 15/20] uuid: Remove ulid helper funcs --- uuid.lua | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/uuid.lua b/uuid.lua index 4aae349..dc1c05f 100644 --- a/uuid.lua +++ b/uuid.lua @@ -45,37 +45,26 @@ local function _now(time_func, ...) error("assertion failed: socket can't be found and no time function provided") end ---generate the time part of an ulid -local function _encode_time(time) - time = math.floor((time or _now()) * 1000) - - local out = {} - - for i = 10, 1, -1 do - local mod = time % #_encoding - out[i] = _encoding[mod + 1] - time = (time - mod) / #_encoding - end - - return table.concat(out) -end - ---generate the random part of an ulid -local function _encode_random(rng) - local out = {} - - for i = 1, 16 do - out[i] = _encoding[math.floor(_random(rng) * #_encoding) + 1] - end - - return table.concat(out) -end - ---generate an ULID using this rng at this time +--generate an ULID using this rng at this time (now by default) --see https://github.com/ulid/spec --implementation based on https://github.com/Tieske/ulid.lua function uuid.ulid(rng, time) - return _encode_time(time) .. _encode_random(rng) + time = math.floor((time or _now()) * 1000) + + local time_part = {} + local random_part = {} + + for i = 10, 1, -1 do + local mod = time % #_encoding + time_part[i] = _encoding[mod + 1] + time = (time - mod) / #_encoding + end + + for i = 1, 16 do + random_part[i] = _encoding[math.floor(_random(rng) * #_encoding) + 1] + end + + return table.concat(time_part) .. table.concat(random_part) end return uuid From 71e9e4572431fe6bb7c3041b8d82b7d07b22662c Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:16:29 -0300 Subject: [PATCH 16/20] uuid: Slight doc improvements --- uuid.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/uuid.lua b/uuid.lua index dc1c05f..b252db2 100644 --- a/uuid.lua +++ b/uuid.lua @@ -1,5 +1,12 @@ --[[ - uuid generation + uuid/ulid generation + + uuid is version 4, ulid is an alternative to uuid (see + https://github.com/ulid/spec). + + todo: + this ulid isn't guaranteed to be sortable for ulids generated + within the same second yet ]] local path = (...):gsub("uuid", "") @@ -15,21 +22,21 @@ end local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" ---generate a UUID version 4 (random) +--generate a UUID version 4 function uuid.uuid4(rng) - -- x should be 0x0-0xF, the single y should be 0x8-0xB - -- 4 should always just be 4 (denoting uuid version) + --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) return string.format( "%x", - c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB) + c == "x" and _random(rng, 0x0, 0xf) or _random(rng, 0x8, 0xb) ) end) return out end --- crockford's base32 https://en.wikipedia.org/wiki/Base32 +--crockford's base32 https://en.wikipedia.org/wiki/Base32 local _encoding = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", @@ -46,7 +53,6 @@ local function _now(time_func, ...) end --generate an ULID using this rng at this time (now by default) ---see https://github.com/ulid/spec --implementation based on https://github.com/Tieske/ulid.lua function uuid.ulid(rng, time) time = math.floor((time or _now()) * 1000) From 6e277df64a0c245fc7a2e021c5e07e21540f4c7a Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Wed, 11 Jan 2023 10:38:52 -0300 Subject: [PATCH 17/20] Rename uuid.lua to identifier.lua --- .test/tests.lua | 8 ++++---- uuid.lua => identifier.lua | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) rename uuid.lua => identifier.lua (92%) diff --git a/.test/tests.lua b/.test/tests.lua index 1474625..6159dcf 100644 --- a/.test/tests.lua +++ b/.test/tests.lua @@ -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]")) diff --git a/uuid.lua b/identifier.lua similarity index 92% rename from uuid.lua rename to identifier.lua index b252db2..0caf837 100644 --- a/uuid.lua +++ b/identifier.lua @@ -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 From c038281f3c82a60157bfe3902b4ab4d2638cbd76 Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Sat, 14 Jan 2023 10:14:58 -0300 Subject: [PATCH 18/20] tests: lazy guard against no socket error --- .test/tests.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.test/tests.lua b/.test/tests.lua index 6159dcf..a993ff8 100644 --- a/.test/tests.lua +++ b/.test/tests.lua @@ -181,6 +181,9 @@ local function test_uuid4() end local function test_ulid() + -- bail if there's no appropriate time func + if select(2, pcall(identifier.ulid)):find('time function') then return end + for i = 1, 100 do local ulid = assert(identifier.ulid()) From 3252a55936be25a2ebdc7e531323f4912d6c51cc Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Sat, 14 Jan 2023 10:25:28 -0300 Subject: [PATCH 19/20] identifier: Guard against require error in case socket's missing --- identifier.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identifier.lua b/identifier.lua index 0caf837..e7352e4 100644 --- a/identifier.lua +++ b/identifier.lua @@ -47,7 +47,7 @@ local _encoding = { --use socket. if that's not loaded, they'll have to provide their own local function _now(time_func, ...) if package.loaded.socket then return package.loaded.socket.gettime(...) end - if require("socket") then return require("socket").gettime(...) end + if pcall(require, "socket") then return require("socket").gettime(...) end if time_func then return time_func(...) end error("assertion failed: socket can't be found and no time function provided") end From 26bc74c91637fe65da87bc72817d62834760299e Mon Sep 17 00:00:00 2001 From: rhy <81539300+rhynomatt@users.noreply.github.com> Date: Sat, 14 Jan 2023 10:27:11 -0300 Subject: [PATCH 20/20] tests: Let's maybe not spam the actions console --- .test/tests.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.test/tests.lua b/.test/tests.lua index a993ff8..43407fb 100644 --- a/.test/tests.lua +++ b/.test/tests.lua @@ -158,7 +158,7 @@ local function test_spairs() end local function test_uuid4() - for i = 1, 100 do + for i = 1, 5 do local id = identifier.uuid4() -- right len @@ -184,7 +184,7 @@ local function test_ulid() -- bail if there's no appropriate time func if select(2, pcall(identifier.ulid)):find('time function') then return end - for i = 1, 100 do + for i = 1, 5 do local ulid = assert(identifier.ulid()) -- right len