Module:StringUtils: Difference between revisions

From MENACE Official Wiki
add module for common string utilities
 
mNo edit summary
Line 42: Line 42:
elseif action == "upper" then
elseif action == "upper" then
return StringUtils.toUpperCase(input)
return StringUtils.toUpperCase(input)
elseif action == "upper" then
elseif action == "title" then
return StringUtils.toUpperCase(input)
return StringUtils.toTitleCase(input)
else
else
return "ERROR at StringUtils module: Invalid action"
return "ERROR at StringUtils module: Invalid action"

Revision as of 19:54, 7 October 2025

Documentation for this module may be created at Module:StringUtils/doc

---
--- Provides common utilities for strings
---

---@module StringUtils
local StringUtils = {}

--- Converts a string to lowercase
function StringUtils.toLowerCase(input)
	if type(input) == "string" then
		return string.lower(input)
	end
	return nil
end

--- Converts a string to uppercase
function StringUtils.toUpperCase(input)
	if type(input) == "string" then
		return string.upper(input)
	end
	return nil
end

--- Converts a string to titlecase
function StringUtils.toTitleCase(input)
    if type(input) == "string" then
        local result, count = input:gsub("(%a)([%w']*)", function(first, rest) 
            return first:upper() .. rest:lower() 
        end)
        return result
    end
    return nil
end

-- Main entry point for the module
function StringUtils.main(frame)
	local input = frame.args[1] or ""
	local action = frame.args[2] or "lower"
	
	if action == "lower" then
		return StringUtils.toLowerCase(input)
	elseif action == "upper" then
		return StringUtils.toUpperCase(input)
	elseif action == "title" then
		return StringUtils.toTitleCase(input)
	else
		return "ERROR at StringUtils module: Invalid action"
	end
end

return StringUtils