Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function _G.typeCheck(value, expectedType, functionName)
- if type(value) ~= expectedType then
- error("TypeError in " .. functionName .. ": Expected a " .. expectedType .. ", but received type '" .. type(value) .. "'.", 2)
- end
- end
- function _G.findIndex(t, value)
- typeCheck(t, "table", "findIndex")
- typeCheck(value, "string", "findIndex")
- for i, v in ipairs(t) do
- if v == value then
- return i
- end
- end
- return nil
- end
- function _G.inList(item, t)
- typeCheck(item, "string", "inList")
- typeCheck(table, "table", "inList")
- for _, listValue in ipairs(t) do
- if item == listValue then return true end
- end
- return false
- end
- function _G.require(filePath)
- typeCheck(filePath, "table", "printTable")
- local file = io.open(filePath, "r")
- if not file then
- error("Could not load file " .. filePath)
- return nil
- end
- local fileContent = file:read("*all")
- file:close()
- local chunk = load(fileContent, "@"..filePath)
- return chunk()
- end
- function _G.printTable(t, indent)
- typeCheck(t, "table", "printTable")
- if indent ~= nil then
- typeCheck(indent, "string", "printTable")
- end
- indent = indent or ""
- for k, v in pairs(t) do
- if type(v) == "table" then
- print(indent .. k .. ": ")
- printTable(v, indent .. " ")
- else
- print(indent .. k .. ": " .. tostring(v))
- end
- end
- end
- function _G.map(t, func, sequential)
- typeCheck(t, "table", "tmap")
- typeCheck(func, "function", "tmap")
- if sequential ~= nil then
- typeCheck(sequential, "boolean", "tmap")
- end
- local result = {}
- local iterator = sequential and ipairs or pairs
- for k, v in iterator(t) do
- table.insert(result, func(k, v))
- end
- return result
- end
- function _G.find(t, func)
- typeCheck(t, "table", "tmap")
- typeCheck(func, "function", "tmap")
- for k, v in pairs(t) do
- local result = func(k, v)
- if result ~= nil then return result end
- end
- return false
- end
- function _G.splitString(str, delimiter)
- delimiter = delimiter or " "
- local result = {}
- for match in (str..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match)
- end
- return result
- end
- function _G.loadEnv(path)
- local path = path or ".env"
- local env = {}
- if not fs.exists(path) then
- return env
- end
- local file = fs.open(path, "r")
- local line = file.readLine()
- while line do
- local key, value = line:match("([^=]+)=(.+)")
- if key and value then
- key = key:match("^%s*(.-)%s*$") -- Trim whitespace from both ends of the key
- value = value:match("^%s*['\"]?(.-)['\"]?%s*$") -- Trim and remove possible quotes
- if value == "true" then value = true -- Attempt boolean conversion
- elseif value == "false" then value = false -- Attempt boolean conversion
- elseif tonumber(value) then value = tonumber(value) -- Attempt number conversion
- end
- env[key] = value
- end
- line = file.readLine()
- end
- file.close()
- return env
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement