Module:FormatPhoneNumber

From Trans People Together Wiki
Revision as of 18:00, 8 May 2025 by Admin (talk | contribs) (Created page with "-- 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.m...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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