Module:FormatPhoneNumber

From Trans People Together Wiki

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

-- Module:Phone
local p = {}

-- Formats a phone number:
--  • If it doesn’t start with “+”, returns empty string.
--  • If it is “+1-AAA-BBB-CCCC”, returns “(AAA) BBB-CCCC”.
--  • Otherwise, returns the input unchanged.
function p.format(frame)
    local input = frame.args[1] or ''
    -- must start with +
    if mw.ustring.sub(input, 1, 1) ~= '+' then
        return ''
    end

    -- match +1-123-456-7890
    local area, prefix, line = mw.ustring.match(
        input,
        '^%+1%-(%d%d%d)%-(%d%d%d)%-(%d%d%d%d)$'
    )

    if area then
        return '(' .. area .. ') ' .. prefix .. '-' .. line
    else
        -- other international formats: leave unchanged
        return input
    end
end

return p