Skip to content

Instantly share code, notes, and snippets.

@cameronpcampbell
Created February 19, 2025 14:39
Show Gist options
  • Save cameronpcampbell/3577992adf249af40031b2849aa8c4e6 to your computer and use it in GitHub Desktop.
Save cameronpcampbell/3577992adf249af40031b2849aa8c4e6 to your computer and use it in GitHub Desktop.
StringToPascal.luau
--!strict
type function _safeIs(input: any, isType: any)
if typeof(input) ~= "userdata" then return false end
local is = input.is
if not is then return false end
return is(input, isType)
end
type function _arrayToUnion(arr: { any })
local arrLength = #arr
return if arrLength == 0 then types.never elseif arrLength == 1 then arr[1] else types.unionof(table.unpack(arr))
end
type function _arrayToIntersection(arr: { any })
local arrLength = #arr
return if arrLength == 0 then types.never elseif arrLength == 1 then arr[1] else types.intersectionof(table.unpack(arr))
end
type function _tableMap(input: { any }, fn: (item: any, idx: number) -> ...any)
local output = table.create(#input)
for idx, item in input do output[idx] = fn(item, idx) end
return output
end
type function _safeValue(input: any)
if _safeIs(input, "singleton") then return input:value() end
return input
end
type function _toPascal(input: string)
return types.singleton(string.upper(string.sub(input, 1, 1))..string.sub(input, 2, -1))
end
export type function StringToPascal(input: string)
if _safeIs(input, "union") then return _arrayToUnion(_tableMap(input:components(), StringToPascal)) end
if _safeIs(input, "intersection") then return _arrayToIntersection(_tableMap(input:components(), StringToPascal)) end
input = _safeValue(input)
if typeof(input) ~= "string" then return input end
return _toPascal(input)
end
return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment