|
| 1 | +--- |
| 2 | +description: General information based on the latest ./README.md content |
| 3 | +globs: |
| 4 | +--- |
| 5 | +Update it if APIs change: |
| 6 | + |
| 7 | +# ts-punycode |
| 8 | + |
| 9 | +A modern TypeScript implementation of Punycode (RFC 3492) with full RFC 5891 support. This library helps you convert Unicode domain names to ASCII (Punycode) and back. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- 🔄 **Bidirectional Conversion** - Convert between Unicode and Punycode seamlessly |
| 14 | +- 📧 **Email Support** - Handles email addresses with Unicode domains |
| 15 | +- 💪 **Type-Safe** - Full TypeScript support with comprehensive type definitions |
| 16 | +- 🌐 **RFC Compliant** - Implements RFC 3492 and RFC 5891 specifications |
| 17 | +- 🧪 **Well-Tested** - Comprehensive test suite ensuring reliability |
| 18 | +- 📦 **Zero Dependencies** - Lightweight and self-contained |
| 19 | +- 🔍 **Unicode 15.1** - Support for the latest Unicode standard |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +# Using npm |
| 25 | +npm install ts-punycode |
| 26 | + |
| 27 | +# Using yarn |
| 28 | +yarn add ts-punycode |
| 29 | + |
| 30 | +# Using pnpm |
| 31 | +pnpm add ts-punycode |
| 32 | + |
| 33 | +# Using bun |
| 34 | +bun add ts-punycode |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Basic Usage |
| 40 | + |
| 41 | +```typescript |
| 42 | +import punycode from 'ts-punycode' |
| 43 | + |
| 44 | +// Convert Unicode to Punycode |
| 45 | +punycode.encode('münchen') // Returns 'mnchen-3ya' |
| 46 | + |
| 47 | +// Convert Punycode to Unicode |
| 48 | +punycode.decode('mnchen-3ya') // Returns 'münchen' |
| 49 | + |
| 50 | +// Convert Domain Names |
| 51 | +punycode.toASCII('münchen.de') // Returns 'xn--mnchen-3ya.de' |
| 52 | +punycode.toUnicode('xn--mnchen-3ya.de') // Returns 'münchen.de' |
| 53 | + |
| 54 | +// Handle Email Addresses |
| 55 | +punycode.toASCII('user@münchen.de') // Returns ' [email protected]' |
| 56 | +punycode.toUnicode(' [email protected]') // Returns 'user@münchen.de' |
| 57 | +``` |
| 58 | + |
| 59 | +### Advanced Usage |
| 60 | + |
| 61 | +```typescript |
| 62 | +import punycode from 'ts-punycode' |
| 63 | +// import { punycode } from 'ts-punycode' |
| 64 | + |
| 65 | +// Working with Unicode Code Points |
| 66 | +const codePoints = punycode.ucs2.decode('☃') // Returns [9731] |
| 67 | +const string = punycode.ucs2.encode([9731]) // Returns '☃' |
| 68 | + |
| 69 | +// Version Information |
| 70 | +console.log(punycode.version) // Returns current version |
| 71 | +``` |
| 72 | + |
| 73 | +## API Reference |
| 74 | + |
| 75 | +### Main Methods |
| 76 | + |
| 77 | +- **`encode(input: string): string`** |
| 78 | + Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols. |
| 79 | + |
| 80 | +- **`decode(input: string): string`** |
| 81 | + Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols. |
| 82 | + |
| 83 | +- **`toASCII(input: string): string`** |
| 84 | + Converts a Unicode string representing a domain name or email address to Punycode. |
| 85 | + |
| 86 | +- **`toUnicode(input: string): string`** |
| 87 | + Converts a Punycode string representing a domain name or email address to Unicode. |
| 88 | + |
| 89 | +### UCS-2 Utilities |
| 90 | + |
| 91 | +- **`ucs2.decode(string: string): number[]`** |
| 92 | + Creates an array containing the numeric code points of each Unicode character. |
| 93 | + |
| 94 | +- **`ucs2.encode(codePoints: number[]): string`** |
| 95 | + Creates a string based on an array of numeric code points. |
| 96 | + |
| 97 | +## Error Handling |
| 98 | + |
| 99 | +The library throws `RangeError` with descriptive messages for various error conditions: |
| 100 | + |
| 101 | +- **Overflow**: When input needs wider integers to process |
| 102 | +- **Not Basic**: When encountering illegal input >= 0x80 (not a basic code point) |
| 103 | +- **Invalid Input**: For general invalid input conditions |
0 commit comments