E.164 Phone Number Normalizer
Normalize local and international phone input into E.164 format with country dial codes, trunk-prefix handling and length checks.
Code
const DIAL_CODES = {
US: "1",
GB: "44",
CN: "86",
DE: "49",
IN: "91",
JP: "81",
};
export function normalizePhone(input, options = {}) {
if (typeof input !== "string") throw new TypeError("phone number must be a string");
const { defaultCountry = "US" } = options;
if (!Object.prototype.hasOwnProperty.call(DIAL_CODES, defaultCountry)) {
throw new RangeError(`unsupported default country "${defaultCountry}"`);
}
const dialCode = DIAL_CODES[defaultCountry];
const trimmed = input.trim();
if (trimmed === "") throw new Error("phone number is empty");
const hadPlus = trimmed.startsWith("+");
let digits = trimmed.replace(/\D/g, "");
if (!hadPlus && digits.startsWith("00")) {
digits = digits.slice(2);
} else if (!hadPlus) {
digits = digits.replace(/^0+/, "");
digits = `${dialCode}${digits}`;
}
if (digits.length < 8 || digits.length > 15) {
throw new RangeError("E.164 numbers contain 8 to 15 digits");
}
if (digits.startsWith("0")) throw new Error("E.164 national numbers cannot start with zero");
return `+${digits}`;
}
console.log(normalizePhone("(415) 555-0132"));
console.log(normalizePhone("+44 20 7946 0958"));
console.log(normalizePhone("0049 30 12345678"));
console.log(normalizePhone("0132 555 0199", { defaultCountry: "CN" }));Output
Captured from a sandboxed run of this snippet in the build pipeline.
+14155550132
+442079460958
+493012345678
+861325550199How it works
Count the function normalizePhone as the entire surface area of E.164 Phone Number Normalizer.
The failure path is explicit — TypeError, RangeError and Error is raised when the guard condition holds.
What to watch for
- The guard
typeof input !== "string"short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
Entry signature: export function normalizePhone(input, options = {})
Import normalizePhone from the file and call it — the rest of the code is implementation detail.
Statements that matter
if (digits.startsWith("0")) throw new Error("E.164 national numbers cannot start with zero");
if (typeof input !== "string") throw new TypeError("phone number must be a string");
throw new RangeError(`unsupported default country "${defaultCountry}"`);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.Phone Number FormatterNormalize a phone number with an optional country code into E.164, RFC 3966, and grouped national formats, with digit-count validation.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- How are precondition violations signaled?
- The code throws `TypeError`, `RangeError` and `Error` — the guard `typeof input !== "string"` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `normalizePhone` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.