From acdb58a44792582427a94c51a3d3f4f6afe79003 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 5 Apr 2014 14:41:31 +0100 Subject: [PATCH] Added lume.uuid(), tests and README.md section --- README.md | 4 ++++ lume.lua | 11 +++++++++++ test/test_lume.lua | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 4239ffc..26aaeeb 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,10 @@ Executes the lua code inside `str`. lume.dostring("print('Hello!')") -- Prints "Hello!" ``` +### lume.uuid() +Generates a random UUID string; version 4 as specified in +[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt). + ### lume.hotswap(modname) Reloads an already loaded module in place, allowing you to immediately see the effects of code changes without having to restart the program. `modname` should diff --git a/lume.lua b/lume.lua index 191ef13..b2e4109 100644 --- a/lume.lua +++ b/lume.lua @@ -361,6 +361,17 @@ function lume.dostring(str) end +function lume.uuid() + local hex = "0123456789abcdef" + local fn = function(x) + local r = math.random(16) - 1 + r = (x == "x") and (r + 1) or (r % 4) + 9 + return hex:sub(r, r) + end + return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn)) +end + + function lume.hotswap(modname) local oldglobal = lume.clone(_G) local updated = {} diff --git a/test/test_lume.lua b/test/test_lume.lua index eb88a19..97cac16 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -371,6 +371,12 @@ tests["lume.dostring"] = function() testeq( lume.dostring([[return 12345]]), 12345 ) end + +tests["lume.uuid"] = function() + testeq( type(lume.uuid()), "string" ) + testeq( #lume.uuid(), 36 ) +end + -- lume.hotswap tests["lume.hotswap"] = function() local ok, err = lume.hotswap("bad_module_name")