Module:Capitalize words

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search

This module capitalizes the first letter of each word in an input string, assuming the words are delimited by whitespace.

Usage: {{#invoke:Capitalize words|capitalize|<input string>}}.


local p = {}

-- Capitalize the first letter of each word in a string
-- Words are separated by spaces
function p.capitalize(frame)
    local str = frame.args[1] or ""
    
    -- Handle empty string
    if str == "" then
        return ""
    end
    
    -- Capitalize first letter of each word
    local result = str:gsub("(%a)([%w_']*)", function(first, rest)
        return first:upper() .. rest:lower()
    end)
    
    return result
end

return p