Convert an email domain to punycode

Keep the local part and convert an international domain to its ASCII xn-- form.

JavaScript Version 1.0.0 · MIT · by Oveyon · .md

convert-an-email-domain-to-punycode.js
function toAsciiAddress(address) { const at = address.lastIndexOf('@'); return `${address.slice(0, at)}@${new URL(`http://${address.slice(at + 1)}`).hostname}`; }

Input & expected output

Input

"ana@bücher.example"

Expected output

"ana@xn--bcher-kva.example"

The edge case

Input

"ana@EXAMPLE.com"

Expected output

"ana@example.com"

Know the limits

  • Oveyon accepts ASCII addresses only: an international domain must be sent in its xn-- form, anything else is refused with address_not_ascii.
  • Only the domain is converted. A non-ASCII local part (EAI) cannot be made ASCII this way.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Written by Oveyon for this edition of 1loc.dev; it is not part of the original collection. Copyright (c) 2026 Oveyon. MIT licensed.

Its file in the public edition: contents/convert-an-email-domain-to-punycode.mdx in akamind/1loc, commit 0a77f131b22d. The function above is the implementation the lab runs and the tests exercise; the test suite checks that it is the code that file carries. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.