Code polish
This commit is contained in:
@@ -1,26 +1,61 @@
|
|||||||
-- Custom Lua helpers for math operations
|
-- Custom Lua helpers for math operations
|
||||||
|
|
||||||
|
--- Returns the minimum of two numbers
|
||||||
|
--- @param a number First number
|
||||||
|
--- @param b number Second number
|
||||||
|
--- @return number Minimum value
|
||||||
function min(a, b) return math.min(a, b) end
|
function min(a, b) return math.min(a, b) end
|
||||||
|
|
||||||
|
--- Returns the maximum of two numbers
|
||||||
|
--- @param a number First number
|
||||||
|
--- @param b number Second number
|
||||||
|
--- @return number Maximum value
|
||||||
function max(a, b) return math.max(a, b) end
|
function max(a, b) return math.max(a, b) end
|
||||||
|
|
||||||
|
--- Rounds a number to n decimal places
|
||||||
|
--- @param x number Number to round
|
||||||
|
--- @param n number? Number of decimal places (default: 0)
|
||||||
|
--- @return number Rounded number
|
||||||
function round(x, n)
|
function round(x, n)
|
||||||
if n == nil then n = 0 end
|
if n == nil then n = 0 end
|
||||||
return math.floor(x * 10 ^ n + 0.5) / 10 ^ n
|
return math.floor(x * 10 ^ n + 0.5) / 10 ^ n
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Returns the floor of a number
|
||||||
|
--- @param x number Number to floor
|
||||||
|
--- @return number Floored number
|
||||||
function floor(x) return math.floor(x) end
|
function floor(x) return math.floor(x) end
|
||||||
|
|
||||||
|
--- Returns the ceiling of a number
|
||||||
|
--- @param x number Number to ceil
|
||||||
|
--- @return number Ceiled number
|
||||||
function ceil(x) return math.ceil(x) end
|
function ceil(x) return math.ceil(x) end
|
||||||
|
|
||||||
|
--- Converts string to uppercase
|
||||||
|
--- @param s string String to convert
|
||||||
|
--- @return string Uppercase string
|
||||||
function upper(s) return string.upper(s) end
|
function upper(s) return string.upper(s) end
|
||||||
|
|
||||||
|
--- Converts string to lowercase
|
||||||
|
--- @param s string String to convert
|
||||||
|
--- @return string Lowercase string
|
||||||
function lower(s) return string.lower(s) end
|
function lower(s) return string.lower(s) end
|
||||||
|
|
||||||
|
--- Formats a string using Lua string.format
|
||||||
|
--- @param s string Format string
|
||||||
|
--- @param ... any Values to format
|
||||||
|
--- @return string Formatted string
|
||||||
function format(s, ...) return string.format(s, ...) end
|
function format(s, ...) return string.format(s, ...) end
|
||||||
|
|
||||||
|
--- Removes leading and trailing whitespace from string
|
||||||
|
--- @param s string String to trim
|
||||||
|
--- @return string Trimmed string
|
||||||
function trim(s) return string.gsub(s, "^%s*(.-)%s*$", "%1") end
|
function trim(s) return string.gsub(s, "^%s*(.-)%s*$", "%1") end
|
||||||
|
|
||||||
-- String split helper
|
--- Splits a string by separator
|
||||||
|
--- @param inputstr string String to split
|
||||||
|
--- @param sep string? Separator pattern (default: whitespace)
|
||||||
|
--- @return table Array of string parts
|
||||||
function strsplit(inputstr, sep)
|
function strsplit(inputstr, sep)
|
||||||
if sep == nil then sep = "%s" end
|
if sep == nil then sep = "%s" end
|
||||||
local t = {}
|
local t = {}
|
||||||
@@ -328,15 +363,24 @@ function toCSV(rows, options)
|
|||||||
return table.concat(rowStrings, "\n")
|
return table.concat(rowStrings, "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- String to number conversion helper
|
--- Converts string to number, returns 0 if invalid
|
||||||
|
--- @param str string String to convert
|
||||||
|
--- @return number Numeric value or 0
|
||||||
function num(str) return tonumber(str) or 0 end
|
function num(str) return tonumber(str) or 0 end
|
||||||
|
|
||||||
-- Number to string conversion
|
--- Converts number to string
|
||||||
|
--- @param num number Number to convert
|
||||||
|
--- @return string String representation
|
||||||
function str(num) return tostring(num) end
|
function str(num) return tostring(num) end
|
||||||
|
|
||||||
-- Check if string is numeric
|
--- Checks if string is numeric
|
||||||
|
--- @param str string String to check
|
||||||
|
--- @return boolean True if string is numeric
|
||||||
function is_number(str) return tonumber(str) ~= nil end
|
function is_number(str) return tonumber(str) ~= nil end
|
||||||
|
|
||||||
|
--- Checks if table is a sequential array (1-indexed with no gaps)
|
||||||
|
--- @param t table Table to check
|
||||||
|
--- @return boolean True if table is an array
|
||||||
function isArray(t)
|
function isArray(t)
|
||||||
if type(t) ~= "table" then return false end
|
if type(t) ~= "table" then return false end
|
||||||
local max = 0
|
local max = 0
|
||||||
|
|||||||
@@ -470,7 +470,8 @@ TABLE FUNCTIONS:
|
|||||||
isArray(t) - Returns true if table is a sequential array
|
isArray(t) - Returns true if table is a sequential array
|
||||||
|
|
||||||
XML HELPER FUNCTIONS:
|
XML HELPER FUNCTIONS:
|
||||||
findElements(root, tagName) - Find all elements with specific tag name
|
findElements(root, tagName) - Find all elements with specific tag name (recursive)
|
||||||
|
findFirstElement(parent, tagName) - Find first direct child with specific tag name
|
||||||
visitElements(root, callback) - Visit all elements recursively
|
visitElements(root, callback) - Visit all elements recursively
|
||||||
callback(element, depth, path)
|
callback(element, depth, path)
|
||||||
filterElements(root, predicate) - Find elements matching condition
|
filterElements(root, predicate) - Find elements matching condition
|
||||||
@@ -484,6 +485,10 @@ XML HELPER FUNCTIONS:
|
|||||||
setAttr(element, attrName, value) - Set attribute value
|
setAttr(element, attrName, value) - Set attribute value
|
||||||
getText(element) - Get element text content
|
getText(element) - Get element text content
|
||||||
setText(element, text) - Set element text content
|
setText(element, text) - Set element text content
|
||||||
|
addChild(parent, child) - Add child element to parent
|
||||||
|
removeChildren(parent, tagName) - Remove all children with specific tag name
|
||||||
|
getChildren(parent, tagName) - Get all direct children with specific tag name
|
||||||
|
countChildren(parent, tagName) - Count direct children with specific tag name
|
||||||
|
|
||||||
JSON HELPER FUNCTIONS:
|
JSON HELPER FUNCTIONS:
|
||||||
visitJSON(data, callback) - Visit all values in JSON structure
|
visitJSON(data, callback) - Visit all values in JSON structure
|
||||||
|
|||||||
Reference in New Issue
Block a user