refact: uuid

This commit is contained in:
Thomas Lambert
2023-04-29 00:21:09 +02:00
parent 43408481a2
commit 4e2a433178
3 changed files with 33 additions and 32 deletions

View File

@@ -15,6 +15,12 @@ function M.file_exists(fname)
end
end
-- Strips an extension from a file name, escaping "." properly, eg:
-- strip_extension("path/Filename.md", ".md") -> "path/Filename"
local function strip_extension(str, ext)
return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "")
end
-- Prompts the user for a note title
function M.prompt_title(ext, defaultFile, callback)
local canceledStr = "__INPUT_CANCELLED__"
@@ -39,4 +45,25 @@ function M.prompt_title(ext, defaultFile, callback)
end)
end
local function random_variable(length)
math.randomseed(os.clock() ^ 5)
local res = ""
for _ = 1, length do
res = res .. string.char(math.random(97, 122))
end
return res
end
function M.new_uuid(uuid_style)
local uuid
if uuid_style == "rand" then
uuid = random_variable(6)
elseif type(uuid_style) == "function" then
uuid = uuid_style()
else
uuid = os.date(uuid_style)
end
return uuid
end
return M

View File

@@ -18,10 +18,4 @@ function M.strip(s, chars_to_remove)
return s:gsub("[" .. M.escape(chars_to_remove) .. "]", "")
end
-- strip an extension from a file name, escaping "." properly, eg:
-- strip_extension("path/Filename.md", ".md") -> "path/Filename"
local function strip_extension(str, ext)
return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "")
end
return M