punycode-esm 1.0.15 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.npmignore ADDED
@@ -0,0 +1,13 @@
1
+ CHANGELOG.md
2
+ project.json
3
+ tsconfig.json
4
+
5
+ coverage/**
6
+ eslint.config.js
7
+
8
+ dist/tsconfig.tsbuildinfo
9
+ dist/**/*.map
10
+ dist-test/**
11
+ test/**
12
+
13
+ src/**
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div style="text-align:center">
2
2
 
3
3
  # punycode-esm
4
- ESM native version of punycode.js.
4
+ ESM and TypeScript port of the punycode.js library.
5
5
 
6
6
  [![npm package](https://badge.fury.io/js/punycode-esm.svg)](https://www.npmjs.com/package/punycode-esm)
7
7
  [![License](https://img.shields.io/npm/l/punycode-esm.svg)](https://github.com/JacobLey/leyman/blob/main/tools/punycode-esm/LICENSE)
@@ -9,27 +9,17 @@ ESM native version of punycode.js.
9
9
  </div>
10
10
 
11
11
  ## Contents
12
- - [Introduction](#introduction)
13
12
  - [Install](#install)
14
13
  - [Example](#example)
15
14
  - [Usage](#usage)
16
15
  - [API](#api)
17
- - [decode](#decodestring)
18
16
  - [encode](#encodestring)
19
- - [toUnicode](#tounicodestring)
17
+ - [decode](#decodestring)
20
18
  - [toASCII](#toasciistring)
19
+ - [toUnicode](#tounicodestring)
21
20
  - [ucs2Decode](#ucs2decodestring)
22
21
  - [ucs2Encode](#ucs2encodecodepoints)
23
-
24
- ## Introduction
25
-
26
- ESM/Typescript native version of [punycode.js](https://www.npmjs.com/package/punycode).
27
-
28
- ESM native users / Typescript `nodenext` users may have some issues with the existing punycode library, so this hopefully serves as a viable alternative.
29
-
30
- Logically this package _should_ be the same as punycode.js. The only advantage of this library is ease-of-use for modern typescript/ESM.
31
-
32
- Credit goes to [Mathias Bynens](https://mathiasbynens.be/) for original implementation of this logic.
22
+ - [Credit](#credit)
33
23
 
34
24
  ## Install
35
25
 
@@ -43,80 +33,137 @@ npm i punycode-esm
43
33
  import { toASCII, toUnicode } from 'punycode-esm';
44
34
 
45
35
  // encode domain names
46
- toASCII('mañana.com'); // 'xn--maana-pta.com'
47
- toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
36
+ toASCII('mañana.com'); // 'xn--maana-pta.com'
37
+ toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
48
38
 
49
39
  // decode domain names
50
40
  toUnicode('xn--maana-pta.com'); // 'mañana.com'
51
- toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
41
+ toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
52
42
  ```
53
43
 
54
44
  ## Usage
55
45
 
56
- `punycode-esm` is an ESM module. That means it _must_ be `import`ed. To load from a CJS module, use dynamic import `const { encode } = await import('punycode-esm');`.
46
+ `punycode-esm` is an ESM module. It must be `import`ed. To load from a CJS module, use dynamic import: `const { encode } = await import('punycode-esm')`.
57
47
 
58
48
  ## API
59
49
 
60
- ### `decode(string)`
50
+ ### `encode(string)`
61
51
 
62
- Converts a Punycode string of ASCII symbols to a string of Unicode symbols.
52
+ Converts a string of Unicode symbols (a single domain label) to a Punycode string of ASCII-only symbols.
63
53
 
64
- ```ts
65
- // decode domain name parts
66
- Punycode.decode('maana-pta'); // 'mañana'
67
- Punycode.decode('--dqo34k'); // '☃-⌘'
68
- ```
54
+ **Parameters**
69
55
 
70
- ### `encode(string)`
56
+ | Parameter | Type | Default | Description |
57
+ |-----------|------|---------|-------------|
58
+ | `string` | `string` | — | Required. A Unicode domain label (no dots). |
71
59
 
72
- Converts a string of Unicode symbols to a Punycode string of ASCII symbols.
60
+ **Returns** `string` — the Punycode-encoded ASCII label.
73
61
 
74
62
  ```ts
75
- // encode domain name parts
76
- Punycode.encode('mañana'); // 'maana-pta'
77
- Punycode.encode('☃-⌘'); // '--dqo34k'
63
+ encode('mañana'); // 'maana-pta'
64
+ encode('☃-⌘'); // '--dqo34k'
78
65
  ```
79
66
 
80
- ### `toUnicode(string)`
67
+ ---
81
68
 
82
- Converts a Punycode string representing a domain name or an email address to Unicode. Only the Punycoded parts of the input will be converted, i.e. it doesn’t matter if you call it on a string that has already been converted to Unicode.
69
+ ### `decode(string)`
83
70
 
84
- ```ts
85
- // decode domain names
86
- Punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
87
- Punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
71
+ Converts a Punycode string of ASCII-only symbols (a single domain label) to a string of Unicode symbols.
88
72
 
89
- // decode email addresses
90
- Punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'); // 'джумла@джpумлатест.bрфa'
73
+ **Parameters**
74
+
75
+ | Parameter | Type | Default | Description |
76
+ |-----------|------|---------|-------------|
77
+ | `string` | `string` | — | Required. A Punycode-encoded ASCII label (no dots). |
78
+
79
+ **Returns** `string` — the decoded Unicode label.
80
+
81
+ ```ts
82
+ decode('maana-pta'); // 'mañana'
83
+ decode('--dqo34k'); // '☃-⌘'
91
84
  ```
92
85
 
86
+ ---
87
+
93
88
  ### `toASCII(string)`
94
89
 
95
- Converts a lowercased Unicode string representing a domain name or an email address to Punycode. Only the non-ASCII parts of the input will be converted, i.e. it doesn’t matter if you call it with a domain that’s already in ASCII.
90
+ Converts a Unicode domain name or email address to its Punycode (ASCII) representation. Only non-ASCII labels are converted; already-ASCII labels are left unchanged.
91
+
92
+ **Parameters**
93
+
94
+ | Parameter | Type | Default | Description |
95
+ |-----------|------|---------|-------------|
96
+ | `string` | `string` | — | Required. A Unicode domain name or email address. |
97
+
98
+ **Returns** `string` — the ASCII-compatible encoding of the domain or email.
96
99
 
97
100
  ```ts
98
- Punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
99
- Punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
101
+ toASCII('mañana.com'); // 'xn--maana-pta.com'
102
+ toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
103
+ toASCII('джумла@джpумлатест.bрфa'); // 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
104
+ ```
105
+
106
+ ---
107
+
108
+ ### `toUnicode(string)`
109
+
110
+ Converts a Punycode domain name or email address to Unicode. Only Punycode-encoded labels (prefixed with `xn--`) are converted; others are left unchanged.
111
+
112
+ **Parameters**
100
113
 
101
- // encode email addresses
102
- Punycode.toASCII('джумла@джpумлатест.bрфa'); // 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
114
+ | Parameter | Type | Default | Description |
115
+ |-----------|------|---------|-------------|
116
+ | `string` | `string` | — | Required. A Punycode domain name or email address. |
117
+
118
+ **Returns** `string` — the Unicode representation of the domain or email.
119
+
120
+ ```ts
121
+ toUnicode('xn--maana-pta.com'); // 'mañana.com'
122
+ toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
123
+ toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'); // 'джумла@джpумлатест.bрфa'
103
124
  ```
104
125
 
126
+ ---
127
+
105
128
  ### `ucs2Decode(string)`
106
129
 
107
- Creates an array containing the numeric code point values of each Unicode symbol in the string. While [JavaScript uses UCS-2 internally](https://mathiasbynens.be/notes/javascript-encoding), this function will convert a pair of surrogate halves (each of which UCS-2 exposes as separate characters) into a single code point, matching UTF-16.
130
+ Creates an array of Unicode code point values from a string. Surrogate pairs (as used internally by JavaScript's UCS-2 encoding) are combined into a single code point, matching UTF-16 semantics.
131
+
132
+ **Parameters**
133
+
134
+ | Parameter | Type | Default | Description |
135
+ |-----------|------|---------|-------------|
136
+ | `string` | `string` | — | Required. A UCS-2 encoded string. |
137
+
138
+ **Returns** `number[]` — array of Unicode code point integers.
108
139
 
109
140
  ```ts
110
- ucs2Decode('abc'); // [0x61, 0x62, 0x63]
111
- // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
112
- punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
141
+ ucs2Decode('abc'); // [0x61, 0x62, 0x63]
142
+ ucs2Decode('\uD834\uDF06'); // [0x1D306]
113
143
  ```
114
144
 
145
+ ---
146
+
115
147
  ### `ucs2Encode(codePoints)`
116
148
 
117
- Creates a string based on an array of numeric code point values.
149
+ Creates a string from an array of Unicode code point values.
150
+
151
+ **Parameters**
152
+
153
+ | Parameter | Type | Default | Description |
154
+ |-----------|------|---------|-------------|
155
+ | `codePoints` | `readonly number[]` | — | Required. Array of Unicode code point integers. |
156
+
157
+ **Returns** `string` — the UCS-2 encoded string.
118
158
 
119
159
  ```ts
120
- punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
121
- punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
160
+ ucs2Encode([0x61, 0x62, 0x63]); // 'abc'
161
+ ucs2Encode([0x1D306]); // '\uD834\uDF06'
122
162
  ```
163
+
164
+ ## Credit
165
+
166
+ This is a direct ESM/TypeScript port of [punycode.js](https://github.com/mathiasbynens/punycode.js) by [Mathias Bynens](https://mathiasbynens.be/). The logic is identical; this package exists for compatibility with TypeScript `moduleResolution: nodenext` and pure-ESM projects. Logic is in sync with `punycode` v2.3.1.
167
+
168
+ - Original npm package: [`punycode`](https://www.npmjs.com/package/punycode)
169
+ - Original GitHub repository: [mathiasbynens/punycode.js](https://github.com/mathiasbynens/punycode.js)
package/dist/punycode.js CHANGED
@@ -9,7 +9,7 @@ const initialBias = 72;
9
9
  const initialN = 128;
10
10
  const delimiter = '-';
11
11
  const baseMinusTMin = base - tMin;
12
- const regexNonASCII = /[^\0-\u007E]/u;
12
+ const regexNonASCII = /[^\0-\u007F]/u;
13
13
  const regexSeparators = /[\u002E\u3002\uFF0E\uFF61]/gu;
14
14
  const mapDomain = (domain, callback)=>{
15
15
  const [first, ...rest] = domain.split('@');
@@ -49,13 +49,13 @@ export const ucs2Decode = (string)=>{
49
49
  };
50
50
  export const ucs2Encode = (codePoints)=>String.fromCodePoint(...codePoints);
51
51
  const basicToDigit = (codePoint)=>{
52
- if (codePoint - 0x30 < 0x0a) {
53
- return codePoint - 0x16;
52
+ if (codePoint >= 0x30 && codePoint < 0x3a) {
53
+ return 26 + (codePoint - 0x30);
54
54
  }
55
- if (codePoint - 0x41 < 0x1a) {
55
+ if (codePoint >= 0x41 && codePoint < 0x5b) {
56
56
  return codePoint - 0x41;
57
57
  }
58
- if (codePoint - 0x61 < 0x1a) {
58
+ if (codePoint >= 0x61 && codePoint < 0x7b) {
59
59
  return codePoint - 0x61;
60
60
  }
61
61
  return base;
@@ -105,7 +105,8 @@ export const decode = (input)=>{
105
105
  const digit = basicToDigit(input.codePointAt(index));
106
106
  ++index;
107
107
  checkOverflow(digit, base, {
108
- gte: true
108
+ gte: true,
109
+ error: 'invalid-input'
109
110
  });
110
111
  checkOverflow(digit, Math.floor((maxInt - i) / w));
111
112
  i += digit * w;
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "punycode-esm",
3
- "version": "1.0.15",
3
+ "version": "2.0.1",
4
4
  "description": "Typescript + ESM version of punycode.js",
5
5
  "engines": {
6
- "node": ">=20"
6
+ "node": ">=24"
7
7
  },
8
8
  "type": "module",
9
9
  "author": "JacobLey",
10
10
  "license": "MIT",
11
- "repository": "JacobLey/leyman",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/JacobLey/leyman.git",
14
+ "directory": "tools/punycode-esm"
15
+ },
12
16
  "homepage": "https://github.com/JacobLey/leyman/tree/main/tools/punycode-esm#readme",
13
17
  "bugs": {
14
18
  "url": "https://github.com/JacobLey/leyman/issues"
@@ -28,20 +32,18 @@
28
32
  "#errors": "./dist/lib/errors.js"
29
33
  },
30
34
  "devDependencies": {
31
- "@biomejs/biome": "^1.9.4",
32
- "@swc/cli": "^0.6.0",
33
- "@swc/core": "^1.10.15",
34
- "@types/chai": "^5.0.1",
35
- "@types/mocha": "^10.0.10",
36
- "@types/node": "^22.13.1",
37
- "c8": "^10.1.3",
38
- "chai": "^5.1.2",
39
- "eslint": "^9.20.0",
40
- "mocha": "^11.1.0",
41
- "typescript": "^5.7.3",
42
- "mocha-chain": "^0.0.11",
35
+ "@biomejs/biome": "^2.5.14",
43
36
  "@leyman/eslint-config": "^0.0.1",
44
- "pnpm-dedicated-lockfile": "^0.0.4",
45
- "nx-update-ts-references": "^0.2.1"
37
+ "@leyman/expect": "^0.0.1",
38
+ "@swc/cli": "^0.8.1",
39
+ "@swc/core": "^1.16.2",
40
+ "@types/mocha": "^10.0.10",
41
+ "@types/node": "^24.19.0",
42
+ "@typescript/native": "npm:typescript@^7.0.2",
43
+ "c8": "^12.0.0",
44
+ "eslint": "^9.39.5",
45
+ "mocha": "^12.0.2",
46
+ "mocha-chain": "^1.0.1",
47
+ "typescript": "npm:@typescript/typescript6@^6.0.2"
46
48
  }
47
49
  }