Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created July 21, 2021 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/a07d7be805f8b84eb04bfccf2ac65b57 to your computer and use it in GitHub Desktop.
Save howmanysmall/a07d7be805f8b84eb04bfccf2ac65b57 to your computer and use it in GitHub Desktop.
local Immutable = require("Immutable")
local string_format = string.format
local string_find = string.find
local string_sub = string.sub
local tostring = tostring
local type = type
local NewMath = {}
function NewMath.round(Number)
if not Number then
error("missing argument #1 to 'round' (number expected)", 2)
end
local NumberType = type(Number)
if NumberType ~= "number" then
error(string.format("invalid argument #1 to 'round' (number expected, got %s)", NumberType), 2)
end
if Number >= 0 then
Number = Number + 0.5
return Number - Number % 1
else
Number = Number - 0.5
return Number + (1 - Number % 1)
end
end
function NewMath.clamp(Number, Min, Max)
if not Number then
error("missing argument #1 to 'clamp' (number expected)", 2)
end
if not Min then
error("missing argument #2 to 'clamp' (number expected)", 2)
end
if not Max then
error("missing argument #3 to 'clamp' (number expected)", 2)
end
local NumberType = type(Number)
if NumberType ~= "number" then
error(string_format("invalid argument #1 to 'clamp' (number expected, got %s)", NumberType), 2)
end
local MinType = type(Min)
if MinType ~= "number" then
error(string_format("invalid argument #2 to 'clamp' (number expected, got %s)", MinType), 2)
end
local MaxType = type(Max)
if MaxType ~= "number" then
error(string_format("invalid argument #3 to 'clamp' (number expected, got %s)", MaxType), 2)
end
if Max < Min then
error("invalid argument #3 to 'clamp' (max must be greater than or equal to min)", 2)
end
return Number > Max and Max or Number < Min and Min or Number
end
function NewMath.sign(Number)
if not Number then
error("missing argument #1 to 'sign' (number expected)", 2)
end
local NumberType = type(Number)
if NumberType ~= "number" then
error(string_format("invalid argument #1 to 'sign' (number expected, got %s)", NumberType), 2)
end
return Number < 0 and -1 or Number > 1 and 1 or 0
end
local NewString = {}
function NewString.split(PossibleString, PossibleSeparator)
if not PossibleString then
error("missing argument #1 to 'split' (string expected)", 2)
end
local StringType = type(PossibleString)
if StringType ~= "string" or StringType ~= "number" then
error(string_format("invalid argument #1 to 'split' (string expected, got %s)", StringType), 2)
end
if PossibleSeparator ~= nil then
local SeparatorType = type(PossibleSeparator)
if SeparatorType ~= "string" or SeparatorType ~= "number" then
error(string_format("invalid argument #2 to 'split' (string expected, got %s)", SeparatorType), 2)
end
end
local String = tostring(PossibleString)
if PossibleSeparator == "" then
local Array = {}
for Index = 1, #String do
Array[Index] = string_sub(String, Index, Index)
end
return Array
else
local Separator
if PossibleSeparator == nil then
Separator = ","
else
Separator = tostring(PossibleSeparator)
end
local Array = {}
local Count = 1
local Position = 1
local Start, End = string_find(String, Separator, Position, true)
while Start do
Array[Count] = string_sub(String, Position, Start - 1)
Count = Count + 1
Position = End + 1
Start, End = string_find(String, Separator, Position, true)
end
Array[Count] = string_sub(String, Position)
return Array
end
end
local NewTable = {}
function NewTable.clear(Table)
if not Table then
error("missing argument #1 to 'clear' (table expected)", 2)
end
local TableType = type(Table)
if TableType ~= "table" then
error(string_format("invalid argument #1 to 'clear' (table expected, got %s)", TableType), 2)
end
for Index in next, Table do
Table[Index] = nil
end
end
function NewTable.create(Size, PreallocatedValue)
if not Size then
error("missing argument #1 to 'create' (number expected)", 2)
end
local SizeType = type(Size)
if SizeType ~= "number" then
error(string_format("invalid argument #1 to 'create' (number expected, got %s)", SizeType), 2)
end
if PreallocatedValue ~= nil then
local Array = {}
for Index = 1, Size do
Array[Index] = PreallocatedValue
end
return Array
else
return {}
end
end
function NewTable.find(Table, Value, Init)
if not Table then
error("missing argument #1 to 'find' (table expected)", 2)
end
if Value == nil then
error("missing argument #2")
end
local TableType = type(Table)
if TableType ~= "table" then
error(string_format("invalid argument #1 to 'find' (table expected, got %s)", TableType), 2)
end
if Init ~= nil then
local InitType = type(Init)
if InitType ~= "number" then
error(string_format("invalid argument #3 to 'find' (number expected, got %s)", InitType), 2)
end
end
for Index = Init or 1, #Table do
if rawget(Table, Index) == Value then
return Index
end
end
return nil
end
function NewTable.move(a1, f, e, t, a2)
-- not even gonna typecheck this lol
a2 = a2 or a1
t = t + e
for i = e, f, -1 do
t = t - 1
a2[t] = a1[i]
end
return a2
end
local function Pascalify(Dictionary)
if type(Dictionary) == "table" then
local NewDictionary = {}
for Index, Value in next, Dictionary do
if type(Value) ~= "function" then
if type(Index) == "string" then
NewDictionary[string.upper(string_sub(Index, 1, 1)) .. string_sub(Index, 2)] = Pascalify(Value)
else
NewDictionary[Index] = Pascalify(Value)
end
end
end
return NewDictionary
else
return Dictionary
end
end
_G.math = Immutable.Dictionary.Join(_G.math, NewMath)
_G.string = Immutable.Dictionary.Join(_G.string, NewString)
_G.table = Immutable.Dictionary.Join(_G.table, NewTable)
_G = Immutable.Dictionary.Join(_G, Pascalify(_G))
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment