Skip to content

Commit a39f6fe

Browse files
authored
Merge pull request #536 from wpferguson/add_gen_uuid_to_dtutils_library
Add gen_uuid() function to dtutils library
2 parents ee7e920 + 8581af2 commit a39f6fe

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lib/dtutils.lua

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ dtutils.libdoc.functions["deprecated"] = {
374374
375375
du.deprecated(script_name, removal_string)
376376
script_name - name of the script being deprecated
377-
removal_strubg - a string explaining when the script will be removed]],
377+
removal_string - a string explaining when the script will be removed]],
378378
Description = [[deprecated prints an error message saying the script is deprecated and when it will be removed]],
379379
Return_Value = [[]],
380380
Limitations = [[]],
@@ -391,5 +391,40 @@ function dtutils.deprecated(script_name, removal_string)
391391
dt.print_error("WARNING: " .. script_name .. " is deprecated and will be removed in " .. removal_string)
392392
end
393393

394+
dtutils.libdoc.functions["gen_uuid"] = {
395+
Name = [[gen_uuid]],
396+
Synopsis = [[generate a UUID string]],
397+
Usage = [[local du = require "lib/dtutils"
398+
399+
uuid = du.gen_uuid(case)
400+
case - "upper" or "lower" to specify the case of the UUID string]],
401+
Description = [[gen_uuid prints an error message saying the script is gen_uuid and when it will be removed]],
402+
Return_Value = [[uuid - string - a hexidecimal string representing the UUID in the requested case]],
403+
Limitations = [[]],
404+
Example = [[]],
405+
See_Also = [[]],
406+
Reference = [[https://gist.github.com/jrus/3197011]],
407+
License = [[]],
408+
Copyright = [[]],
409+
}
410+
411+
function dtutils.gen_uuid(case)
412+
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
413+
414+
-- seed with os.time in seconds and add an extra degree of random for multiple calls in the same second
415+
math.randomseed(os.time(), math.random(0, 65536))
416+
417+
local uuid = string.gsub(template, '[xy]', function (c)
418+
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
419+
return string.format('%x', v)
420+
end
421+
)
422+
423+
if case and case == "upper" then
424+
uuid = string.upper(uuid)
425+
end
426+
427+
return uuid
428+
end
394429

395430
return dtutils

0 commit comments

Comments
 (0)