Advertisement
mathiaas

utils

Apr 7th, 2024 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. function _G.typeCheck(value, expectedType, functionName)
  2.     if type(value) ~= expectedType then
  3.         error("TypeError in " .. functionName .. ": Expected a " .. expectedType .. ", but received type '" .. type(value) .. "'.", 2)
  4.     end
  5. end
  6.  
  7.  
  8. function _G.findIndex(t, value)
  9.     typeCheck(t, "table", "findIndex")
  10.     typeCheck(value, "string", "findIndex")
  11.  
  12.     for i, v in ipairs(t) do
  13.         if v == value then
  14.             return i
  15.         end
  16.     end
  17.     return nil
  18. end
  19.  
  20.  
  21. function _G.inList(item, t)
  22.     typeCheck(item, "string", "inList")
  23.     typeCheck(table, "table", "inList")
  24.  
  25.     for _, listValue in ipairs(t) do
  26.         if item == listValue then return true end
  27.     end
  28.     return false
  29. end
  30.  
  31.  
  32. function _G.require(filePath)
  33.     typeCheck(filePath, "table", "printTable")
  34.  
  35.     local file = io.open(filePath, "r")
  36.     if not file then
  37.         error("Could not load file " .. filePath)
  38.         return nil
  39.     end
  40.     local fileContent = file:read("*all")
  41.     file:close()
  42.  
  43.     local chunk = load(fileContent, "@"..filePath)
  44.     return chunk()
  45. end
  46.  
  47.  
  48. function _G.printTable(t, indent)
  49.     typeCheck(t, "table", "printTable")
  50.  
  51.     if indent ~= nil then
  52.         typeCheck(indent, "string", "printTable")
  53.     end
  54.     indent = indent or ""
  55.     for k, v in pairs(t) do
  56.         if type(v) == "table" then
  57.             print(indent .. k .. ": ")
  58.             printTable(v, indent .. "  ")
  59.         else
  60.             print(indent .. k .. ": " .. tostring(v))
  61.         end
  62.     end
  63. end
  64.  
  65.  
  66. function _G.map(t, func, sequential)
  67.     typeCheck(t, "table", "tmap")
  68.     typeCheck(func, "function", "tmap")
  69.     if sequential ~= nil then
  70.         typeCheck(sequential, "boolean", "tmap")
  71.     end
  72.  
  73.     local result = {}
  74.     local iterator = sequential and ipairs or pairs
  75.  
  76.     for k, v in iterator(t) do
  77.         table.insert(result, func(k, v))
  78.     end
  79.     return result
  80. end
  81.  
  82.  
  83. function _G.find(t, func)
  84.     typeCheck(t, "table", "tmap")
  85.     typeCheck(func, "function", "tmap")
  86.     for k, v in pairs(t) do
  87.         local result = func(k, v)
  88.         if result ~= nil then return result end
  89.     end
  90.     return false
  91. end
  92.  
  93.  
  94. function _G.splitString(str, delimiter)
  95.     delimiter = delimiter or " "
  96.     local result = {}
  97.     for match in (str..delimiter):gmatch("(.-)"..delimiter) do
  98.         table.insert(result, match)
  99.     end
  100.     return result
  101. end
  102.  
  103.  
  104. function _G.loadEnv(path)
  105.     local path = path or ".env"
  106.     local env = {}
  107.     if not fs.exists(path) then
  108.         return env
  109.     end
  110.     local file = fs.open(path, "r")
  111.     local line = file.readLine()
  112.     while line do
  113.         local key, value = line:match("([^=]+)=(.+)")
  114.         if key and value then
  115.             key = key:match("^%s*(.-)%s*$")  -- Trim whitespace from both ends of the key
  116.             value = value:match("^%s*['\"]?(.-)['\"]?%s*$")  -- Trim and remove possible quotes
  117.             if value == "true" then value = true -- Attempt boolean conversion
  118.             elseif value == "false" then value = false -- Attempt boolean conversion
  119.             elseif tonumber(value) then value = tonumber(value) -- Attempt number conversion
  120.             end
  121.             env[key] = value
  122.         end
  123.         line = file.readLine()
  124.     end
  125.     file.close()
  126.     return env
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement