or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-punycode

A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/punycode@2.3.x

To install, run

npx @tessl/cli install tessl/npm-punycode@2.3.0

index.mddocs/

Punycode

Punycode is a robust converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms. It provides encoding and decoding functionality for internationalized domain names and email addresses, converting between Unicode and ASCII representations.

Package Information

  • Package Name: punycode
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install punycode
  • Build Output: CommonJS (punycode.js) and ES modules (punycode.es6.js, generated via build script)

Core Imports

CommonJS (recommended):

const punycode = require('punycode/');

ES Modules:

// Default import (entire punycode object)
import punycode from 'punycode/punycode.es6.js';

// Named imports (individual functions) 
import { decode, encode, toASCII, toUnicode, ucs2decode, ucs2encode } from 'punycode/punycode.es6.js';

Note: Use the trailing slash (punycode/) to import the userland module instead of Node.js's deprecated core module.

Basic Usage

CommonJS:

const punycode = require('punycode/');

// Convert Unicode to ASCII (Punycode)
const encoded = punycode.encode('mañana');  // 'maana-pta'
const decoded = punycode.decode('maana-pta');  // 'mañana'

// Convert domain names
const unicodeDomain = punycode.toUnicode('xn--maana-pta.com');  // 'mañana.com'
const asciiDomain = punycode.toASCII('mañana.com');  // 'xn--maana-pta.com'

// Work with email addresses
const unicodeEmail = punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
const asciiEmail = punycode.toASCII('джумла@джpумлатест.bрфa');

// Convert between strings and code points
const codePoints = punycode.ucs2.decode('abc');  // [0x61, 0x62, 0x63]
const string = punycode.ucs2.encode([0x61, 0x62, 0x63]);  // 'abc'

ES Modules (using named imports):

import { encode, decode, toUnicode, toASCII, ucs2decode, ucs2encode } from 'punycode/punycode.es6.js';

// Convert Unicode to ASCII (Punycode)
const encoded = encode('mañana');  // 'maana-pta'
const decoded = decode('maana-pta');  // 'mañana'

// Convert domain names
const unicodeDomain = toUnicode('xn--maana-pta.com');  // 'mañana.com'
const asciiDomain = toASCII('mañana.com');  // 'xn--maana-pta.com'

// Convert between strings and code points
const codePoints = ucs2decode('abc');  // [0x61, 0x62, 0x63]
const string = ucs2encode([0x61, 0x62, 0x63]);  // 'abc'

Capabilities

String Encoding and Decoding

Core Punycode conversion functions for transforming Unicode strings to ASCII-safe representations and vice versa.

/**
 * Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols
 * @param {string} input - The Punycode string of ASCII-only symbols
 * @returns {string} The resulting string of Unicode symbols
 * @throws {RangeError} On invalid input or overflow
 */
function decode(input);

/**
 * Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols
 * @param {string} input - The string of Unicode symbols
 * @returns {string} The resulting Punycode string of ASCII-only symbols
 * @throws {RangeError} On overflow
 */
function encode(input);

Usage Examples:

// Basic string conversion
punycode.encode('mañana');  // 'maana-pta'
punycode.decode('maana-pta');  // 'mañana'

// Unicode characters
punycode.encode('☃-⌘');  // '--dqo34k'
punycode.decode('--dqo34k');  // '☃-⌘'

// Complex Unicode strings
punycode.encode('他们为什么不说中文');  // 'ihqwcrb4cv8a8dqg056pqjye'
punycode.decode('ihqwcrb4cv8a8dqg056pqjye');  // '他们为什么不说中文'

Domain and Email Conversion

High-level functions for converting complete domain names and email addresses, handling only the parts that need conversion.

/**
 * Converts a Punycode string representing a domain name or email address to Unicode
 * Only the Punycoded parts of the input will be converted
 * @param {string} input - The Punycoded domain name or email address to convert to Unicode
 * @returns {string} The Unicode representation of the given Punycode string
 */
function toUnicode(input);

/**
 * Converts a Unicode string representing a domain name or email address to Punycode
 * Only the non-ASCII parts of the domain name will be converted
 * @param {string} input - The domain name or email address to convert, as a Unicode string
 * @returns {string} The Punycode representation of the given domain name or email address
 */
function toASCII(input);

Usage Examples:

// Domain name conversion
punycode.toASCII('mañana.com');  // 'xn--maana-pta.com'
punycode.toUnicode('xn--maana-pta.com');  // 'mañana.com'

// Email address conversion
punycode.toASCII('user@mañana.com');  // 'user@xn--maana-pta.com'
punycode.toUnicode('user@xn--maana-pta.com');  // 'user@mañana.com'

// Multi-part domains
punycode.toASCII('☃-⌘.example.com');  // 'xn----dqo34k.example.com'
punycode.toUnicode('xn----dqo34k.example.com');  // '☃-⌘.example.com'

// Safe to call on already converted strings
punycode.toASCII('example.com');  // 'example.com' (no change)
punycode.toUnicode('example.com');  // 'example.com' (no change)

UCS-2 Utilities

Low-level utilities for converting between JavaScript's internal character representation (UCS-2) and Unicode code points, with proper handling of surrogate pairs.

/**
 * Object containing methods to convert between JavaScript's internal character 
 * representation (UCS-2) and Unicode code points
 */
const ucs2 = {
  /**
   * Creates an array containing the numeric code points of each Unicode character in the string
   * Handles surrogate pairs properly, converting them to single code points (UTF-16 compatible)
   * @param {string} string - The Unicode input string (UCS-2)
   * @returns {number[]} The new array of numeric code points
   */
  decode: function(string),
  
  /**
   * Creates a string based on an array of numeric code points
   * @param {number[]} codePoints - The array of numeric code points
   * @returns {string} The new Unicode string (UCS-2)
   */
  encode: function(codePoints)
};

Usage Examples:

// Convert string to code points
punycode.ucs2.decode('abc');  // [0x61, 0x62, 0x63]
punycode.ucs2.decode('♥');  // [0x2665]

// Handle surrogate pairs properly
punycode.ucs2.decode('\uD834\uDF06');  // [0x1D306] (single code point)

// Convert code points back to string
punycode.ucs2.encode([0x61, 0x62, 0x63]);  // 'abc'
punycode.ucs2.encode([0x1D306]);  // '\uD834\uDF06'

// Work with mixed content
const input = 'Hello ♥ World';
const codePoints = punycode.ucs2.decode(input);
const restored = punycode.ucs2.encode(codePoints);
console.log(restored === input);  // true

Version Information

Access to the current library version.

/**
 * A string representing the current Punycode.js version number
 * @type {string}
 */
const version;

Usage Example:

console.log(punycode.version);  // '2.3.1'

Error Handling

All punycode functions may throw RangeError exceptions in the following cases:

  • Overflow: Input needs wider integers to process
  • Invalid input: Input contains invalid characters or sequences
  • Not basic: Input contains illegal characters (>= 0x80) in contexts requiring basic code points
try {
  const result = punycode.decode('invalid-input');
} catch (error) {
  if (error instanceof RangeError) {
    console.error('Punycode error:', error.message);
    // Handle specific error cases
  }
}

Types

// All functions are available on the main punycode object
interface Punycode {
  decode(input: string): string;
  encode(input: string): string;
  toUnicode(input: string): string;
  toASCII(input: string): string;
  ucs2: {
    decode(string: string): number[];
    encode(codePoints: number[]): string;
  };
  version: string;
}