Skip to content

Instantly share code, notes, and snippets.

@cameronpcampbell
Last active June 1, 2025 04:34
Show Gist options
  • Save cameronpcampbell/cf6d9669add0cefcd645ad2e8a10ba2a to your computer and use it in GitHub Desktop.
Save cameronpcampbell/cf6d9669add0cefcd645ad2e8a10ba2a to your computer and use it in GitHub Desktop.
--!strict
type function default_nanoid()
return nanoid()
end
export type function nominal(input: type)
local phantom_table = types.newtable()
phantom_table:setwriteproperty(types.singleton("_phantom_nominal_id"), types.singleton(default_nanoid()()))
return types.intersectionof(input, phantom_table)
end
-- The nanoid type function was forked from `actboy168`'s lua nanoid implementation.
-- License below:
--[[
MIT License
Copyright (c) 2024 actboy168
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
type function countl_zero(x)
local n = 32
local y = bit32.rshift(x, 16)
if y ~= 0 then
n = n - 16
x = y
end
y = bit32.rshift(x, 8)
if y ~= 0 then
n = n - 8
x = y
end
y = bit32.rshift(x, 4)
if y ~= 0 then
n = n - 4
x = y
end
y = bit32.rshift(x, 2)
if y ~= 0 then
n = n - 2
x = y
end
y = bit32.rshift(x, 1)
if y ~= 0 then
n = n - 1
x = y
end
return n - x
end
type function nanoid(alphabet: string?, size: number?)
local alphabet = alphabet or "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
local size = size or 21
local RandomMaxBit = 31
local RandomMax = bit32.lshift(1, RandomMaxBit) - 1
local alphabet_table = {}
for c in string.gmatch(alphabet, ".") do
alphabet_table[#alphabet_table + 1] = c
end
local mask_bit = 32 - countl_zero(#alphabet_table - 1)
local mask = bit32.lshift(1, mask_bit) - 1
local id = {}
if #alphabet_table == mask + 1 then
local step = RandomMaxBit // mask_bit
local suffix = (size // step) * step
if suffix == size then
return function()
for cnt = 1, size - step + 1, step do
local rnd = math.random(0, RandomMax)
for i = 0, step - 1 do
local index = 1 + bit32.band(bit32.rshift(rnd, i * mask_bit), mask)
id[cnt + i] = alphabet_table[index]
end
end
return table.concat(id)
end
else
return function()
for cnt = 1, size - step + 1, step do
local rnd = math.random(0, RandomMax)
for i = 0, step - 1 do
local index = 1 + bit32.band(bit32.rshift(rnd, i * mask_bit), mask)
id[cnt + i] = alphabet_table[index]
end
end
local rnd = math.random(0, RandomMax)
for cnt = suffix + 1, size do
local index = 1 + bit32.band(bit32.rshift(rnd, (cnt - suffix - 1) * mask_bit), mask)
id[cnt] = alphabet_table[index]
end
return table.concat(id)
end
end
else
return function(): string
local cnt = 1
while true do
local rnd = math.random(0, RandomMax)
for i = 0, RandomMaxBit - mask_bit, mask_bit do
local index = 1 + bit32.band(bit32.rshift(rnd, i), mask)
if index <= #alphabet_table then
id[cnt] = alphabet_table[index]
cnt = cnt + 1
if cnt > size then
return table.concat(id)
end
end
end
end
end
end
end
return nil
@cameronpcampbell
Copy link
Author

Really hacky faked nominal types in luau by intersecting a type with a table with a uuid.

Example usage:

type Name = nominal<string>

local function verify_name(str: string): Name
    return (str :: any) :: Name
end

local function get_email_for_name(name: Name)
    -- gets email
    return "[email protected]"
end

local email = get_email_for_name("hello")

local email = get_email_for_name(verify_name("hello"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment