Skip to content

Commit d4a2c82

Browse files
jasnellevanlucas
authored andcommitted
doc: general improvements to punycode.md copy
PR-URL: #7025 Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 1095ae1 commit d4a2c82

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

doc/api/punycode.md

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@
22

33
Stability: 2 - Stable
44

5-
[Punycode.js][] is bundled with Node.js v0.5.1+. Use `require('punycode')` to
6-
access it. (To use it with other Node.js versions, use npm to install the
7-
`punycode` module first.)
5+
The `punycode` module is a bundled version of the [Punycode.js][] module. It
6+
can be accessed using:
7+
8+
```js
9+
const punycode = require('punycode');
10+
```
11+
12+
[Punycode][] is a character encoding scheme defined by RFC 3492 that is
13+
primarily intended for use in Internationalized Domain Names. Because host
14+
names in URLs are limited to ASCII characters only, Domain Names that contain
15+
non-ASCII characters must be converted into ASCII using the Punycode scheme.
16+
For instance, the Japanese character that translates into the English word,
17+
`'example'` is `'例'`. The Internationalized Domain Name, `'例.com'` (equivalent
18+
to `'example.com'`) is represented by Punycode as the ASCII string
19+
`'xn--fsq.com'`.
20+
21+
The `punycode` module provides a simple implementation of the Punycode standard.
22+
23+
*Note*: The `punycode` module is a third-party dependency used by Node.js and
24+
made available to developers as a convenience. Fixes or other modifications to
25+
the module must be directed to the [Punycode.js][] project.
826

927
## punycode.decode(string)
1028
<!-- YAML
1129
added: v0.5.1
1230
-->
1331

14-
Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
32+
* `string` {String}
33+
34+
The `punycode.decode()` method converts a [Punycode][] string of ASCII-only
35+
characters to the equivalent string of Unicode codepoints.
1536

1637
```js
17-
// decode domain name parts
1838
punycode.decode('maana-pta'); // 'mañana'
1939
punycode.decode('--dqo34k'); // '☃-⌘'
2040
```
@@ -24,10 +44,12 @@ punycode.decode('--dqo34k'); // '☃-⌘'
2444
added: v0.5.1
2545
-->
2646

27-
Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols.
47+
* `string` {String}
48+
49+
The `punycode.encode()` method converts a string of Unicode codepoints to a
50+
[Punycode][] string of ASCII-only characters.
2851

2952
```js
30-
// encode domain name parts
3153
punycode.encode('mañana'); // 'maana-pta'
3254
punycode.encode('☃-⌘'); // '--dqo34k'
3355
```
@@ -37,29 +59,36 @@ punycode.encode('☃-⌘'); // '--dqo34k'
3759
added: v0.6.1
3860
-->
3961

40-
Converts a Unicode string representing a domain name to Punycode. Only the
41-
non-ASCII parts of the domain name will be converted, i.e. it doesn't matter if
42-
you call it with a domain that's already in ASCII.
62+
* `domain` {String}
63+
64+
The `punycode.toASCII()` method converts a Unicode string representing an
65+
Internationalized Domain Name to [Punycode][]. Only the non-ASCII parts of the
66+
domain name will be converted. Calling `punycode.toASCII()` on a string that
67+
already only contains ASCII characters will have no effect.
4368

4469
```js
4570
// encode domain names
46-
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
47-
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
71+
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
72+
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
73+
punycode.toASCII('example.com'); // 'example.com'
4874
```
4975

5076
## punycode.toUnicode(domain)
5177
<!-- YAML
5278
added: v0.6.1
5379
-->
5480

55-
Converts a Punycode string representing a domain name to Unicode. Only the
56-
Punycoded parts of the domain name will be converted, i.e. it doesn't matter if
57-
you call it on a string that has already been converted to Unicode.
81+
* `domain` {String}
82+
83+
The `punycode.toUnicode()` method converts a string representing a domain name
84+
containing [Punycode][] encoded characters into Unicode. Only the [Punycode][]
85+
encoded parts of the domain name are be converted.
5886

5987
```js
6088
// decode domain names
6189
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
62-
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
90+
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
91+
punycode.toUnicode('example.com'); // 'example.com'
6392
```
6493

6594
## punycode.ucs2
@@ -72,10 +101,10 @@ added: v0.7.0
72101
added: v0.7.0
73102
-->
74103

75-
Creates an array containing the numeric code point values of each Unicode
76-
symbol in the string. While [JavaScript uses UCS-2 internally][], this function
77-
will convert a pair of surrogate halves (each of which UCS-2 exposes as
78-
separate characters) into a single code point, matching UTF-16.
104+
* `string` {String}
105+
106+
The `punycode.ucs2.decode()` method returns an array containing the numeric
107+
codepoint values of each Unicode symbol in the string.
79108

80109
```js
81110
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
@@ -88,7 +117,10 @@ punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
88117
added: v0.7.0
89118
-->
90119

91-
Creates a string based on an array of numeric code point values.
120+
* `codePoints` {Array}
121+
122+
The `punycode.ucs2.encode()` method returns a string based on an array of
123+
numeric code point values.
92124

93125
```js
94126
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
@@ -100,7 +132,7 @@ punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
100132
added: v0.6.1
101133
-->
102134

103-
A string representing the current Punycode.js version number.
135+
Returns a string identifying the current [Punycode.js][] version number.
104136

137+
[Punycode]: https://tools.ietf.org/html/rfc3492
105138
[Punycode.js]: https://mths.be/punycode
106-
[JavaScript uses UCS-2 internally]: https://mathiasbynens.be/notes/javascript-encoding

0 commit comments

Comments
 (0)