lua-uuid is a lightweight, native library for Lua (5.1 and newer) to deal with Universally Unique Id (UUID).
- On Unix-like distributions, it uses
libuuidto generate UUIDs; - On Windows, it uses the WINAPI
rpcrt4library; - On macOS / iOS, it uses the
CoreFoundationframework.
Important
On Windows or macOS, there is no need to install external dependencies.
On Unix-like distributions, lua-uuid depends on libuuid:
-
On Debian-based (e.g: Ubuntu) distributions:
sudo apt install -y uuid-dev
-
On RedHat-based (e.g: Fedora) distributions:
sudo dnf install libuuid-devel
-
On BSD-based (e.g: FreeBSD) distributions:
pkg install e2fsprogs-libuuid
Assuming that LuaRocks is properly installed and configured in the system, execute the following command:
luarocks install lua-uuidIn case LuaRocks is not an option, check the guides:
-
Generate GUIDs / UUIDs and print them
-- load the library local uuid = require("lua-uuid") -- generate UUIDs local id1 = uuid.new() local id2 = uuid.new() -- print each UUID print(id1) print(id2)
-
Generate GUIDs / UUIDs and get their string representations
-- load the library local uuid = require("lua-uuid") -- generate UUIDs local id1 = uuid.new() local id2 = uuid.new() -- get their string representations local s1 = tostring(id1) local s2 = tostring(id2) assert(type(s1) == 'string') assert(type(s2) == 'string') -- print each string print(s1) print(s2)
-- load the library
local uuid = require("lua-uuid")
-- parse UUIDs from string
local id1 = uuid.parse("33e4a9f2-8141-4734-a638-f2d08ee7d070")
local id2 = uuid.parse("653096e0-b09f-4626-b65e-07d4e21c70c6")
-- print each UUID
print(id1)
print(id2)-- load the library
local uuid = require("lua-uuid")
-- try to parse UUIDs from string
local id1, err1 = uuid.tryparse("some random string")
local id2, err2 = uuid.tryparse("653096e0-b09f-4626-b65e-07d4e21c70c6")
-- print each UUID
if (id1 == nil) then
-- this branch is going
-- to execute, because
-- the string is not
-- a valid GUID / UUID
print(err1)
else
print(id1)
end
if (id2 == nil) then
print(err2)
else
-- this branch is going
-- to execute
print(id2)
end-- load the library
local uuid = require("lua-uuid")
-- generate UUIDs
local id1 = uuid.new()
local id2 = uuid.new()
-- print each UUID
print(id1)
print(id2)
-- prints false
print(id1 == id2)
-- prints true
print(id1 == id1)
-- prints true
print(id2 == id2)-- load the library
local uuid = require("lua-uuid")
-- generate UUIDs
local id1 = uuid.new()
local id2 = uuid.new()
-- prints false
print(id1:isnil())
print(id2:isnil())
-- parse UUID
local id3 = uuid.parse("00000000-0000-0000-0000-000000000000")
-- prints true
print(id3:isnil())-- load the library
local uuid = require("lua-uuid")
-- print the library version
print("lua-uuid is version " .. uuid.version)- Description: The library version
- Signature:
version - Return:
(string) - Usage: See here
- Description: Generates a new GUID / UUID
- Signature:
new() - Return:
(userdata) - Usage: See here
- Remark: If the underlying library fails to generate GUID / UUID, this function throws an exception. Guard the call with
pcallto detect failures.
- Description: Parses a GUID / UUID from a string value
- Signature:
parse(value)- value (string): the string to be parsed
- Return:
(userdata) - Usage: See here
- Remark: If the underlying library fails to parse the GUID / UUID, this function throws an exception. Guard the call with
pcallto detect failures.
- Description: Tries to parse a GUID / UUID from a string value
- Signature:
tryparse(value)- value (string): the string to be parsed
- Return:
- obj: an
userdatarepresenting the GUID / UUID on success ornilon failure; - err:
nilon success orstringcontaining a description of the error.
- obj: an
- Usage: See here
- Description: Verifies whether the GUID / UUID is considered null or not.
Note
a GUID / UUID is considered null when its string representation is equal to 00000000-0000-0000-0000-000000000000.
- Signature:
instance:isnil()- instance (userdata): the GUID / UUID instance to check for nullity
- Return:
(boolean) - Usage: See here
- Description: Compares two GUIDs / UUIDs for equality
- Signature:
left == right- left (any): the left-side element
- right (any): the right-side element
- Return:
(boolean) - Usage: See here
- Description: Converts the GUID / UUID to string
- Signature:
tostring(value)- value (userdata): the GUID / UUID to perform the conversion
- Return:
(string) - Usage: See here
Browse the changelog