2
2
3
3
Stability: 2 - Stable
4
4
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.
8
26
9
27
## punycode.decode(string)
10
28
<!-- YAML
11
29
added: v0.5.1
12
30
-->
13
31
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.
15
36
16
37
``` js
17
- // decode domain name parts
18
38
punycode .decode (' maana-pta' ); // 'mañana'
19
39
punycode .decode (' --dqo34k' ); // '☃-⌘'
20
40
```
@@ -24,10 +44,12 @@ punycode.decode('--dqo34k'); // '☃-⌘'
24
44
added: v0.5.1
25
45
-->
26
46
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.
28
51
29
52
``` js
30
- // encode domain name parts
31
53
punycode .encode (' mañana' ); // 'maana-pta'
32
54
punycode .encode (' ☃-⌘' ); // '--dqo34k'
33
55
```
@@ -37,29 +59,36 @@ punycode.encode('☃-⌘'); // '--dqo34k'
37
59
added: v0.6.1
38
60
-->
39
61
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.
43
68
44
69
``` js
45
70
// 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'
48
74
```
49
75
50
76
## punycode.toUnicode(domain)
51
77
<!-- YAML
52
78
added: v0.6.1
53
79
-->
54
80
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.
58
86
59
87
``` js
60
88
// decode domain names
61
89
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'
63
92
```
64
93
65
94
## punycode.ucs2
@@ -72,10 +101,10 @@ added: v0.7.0
72
101
added: v0.7.0
73
102
-->
74
103
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 .
79
108
80
109
``` js
81
110
punycode .ucs2 .decode (' abc' ); // [0x61, 0x62, 0x63]
@@ -88,7 +117,10 @@ punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
88
117
added: v0.7.0
89
118
-->
90
119
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.
92
124
93
125
``` js
94
126
punycode .ucs2 .encode ([0x61 , 0x62 , 0x63 ]); // 'abc'
@@ -100,7 +132,7 @@ punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
100
132
added: v0.6.1
101
133
-->
102
134
103
- A string representing the current Punycode.js version number.
135
+ Returns a string identifying the current [ Punycode.js] [ ] version number.
104
136
137
+ [ Punycode ] : https://tools.ietf.org/html/rfc3492
105
138
[ Punycode.js ] : https://mths.be/punycode
106
- [ JavaScript uses UCS-2 internally ] : https://mathiasbynens.be/notes/javascript-encoding
0 commit comments