Luar | Script

-- Check if a value exists in a table (linear search) function table_utils.contains(tbl, value) for _, v in pairs(tbl) do if v == value then return true end end return false end

-- Log with timestamp function debug_utils.log(message, level) level = level or "INFO" local timestamp = os.date("%Y-%m-%d %H:%M:%S") print(string.format("[%s] [%s] %s", timestamp, level, message)) end

-- Append string to file function file_utils.append_file(filename, content) local f, err = io.open(filename, "a") if not f then return false, err end f:write(content) f:close() return true end

-- Check if string ends with a suffix function string_utils.ends_with(str, suffix) return #suffix == 0 or str:sub(-#suffix) == suffix end script luar

debug_utils.log("Script started") debug_utils.time_function(string_utils.trim, " test ") --]]

file_utils.write_file("test.txt", "Lua rocks!") print(file_utils.read_file("test.txt"))

-- Deep copy a table (handles nested tables) function table_utils.deep_copy(orig) local copy if type(orig) == "table" then copy = {} for k, v in pairs(orig) do copy[table_utils.deep_copy(k)] = table_utils.deep_copy(v) end setmetatable(copy, table_utils.deep_copy(getmetatable(orig))) else copy = orig end return copy end -- Check if a value exists in a

-- -------------------------------------------- -- 4. DATA VALIDATION -- -------------------------------------------- local validate = {}

-- Trim whitespace from both ends function string_utils.trim(str) return str:match("^%s*(.-)%s*$") end

-- Print table recursively (for debugging) function table_utils.print_table(tbl, indent) indent = indent or 0 for k, v in pairs(tbl) do local formatting = string.rep(" ", indent) .. tostring(k) .. ": " if type(v) == "table" then print(formatting) table_utils.print_table(v, indent + 1) else print(formatting .. tostring(v)) end end end ": " if type(v) == "table" then print(formatting)

-- Write string to file (overwrites) function file_utils.write_file(filename, content) local f, err = io.open(filename, "w") if not f then return false, err end f:write(content) f:close() return true end

-- Check if value is a number within range function validate.is_in_range(val, min, max) return type(val) == "number" and val >= min and val <= max end

-- Merge table t2 into t1 (overwrites t1 keys) function table_utils.merge(t1, t2) for k, v in pairs(t2) do t1[k] = v end return t1 end

-- -------------------------------------------- -- 5. DEBUGGING / TIMING -- -------------------------------------------- local debug_utils = {}