租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.6 KiB

3 months ago
  1. # Punycode.js [![punycode on npm](https://img.shields.io/npm/v/punycode)](https://www.npmjs.com/package/punycode) [![](https://data.jsdelivr.com/v1/package/npm/punycode/badge)](https://www.jsdelivr.com/package/npm/punycode)
  2. Punycode.js is a robust Punycode converter that fully complies to [RFC 3492](https://tools.ietf.org/html/rfc3492) and [RFC 5891](https://tools.ietf.org/html/rfc5891).
  3. This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm:
  4. * [The C example code from RFC 3492](https://tools.ietf.org/html/rfc3492#appendix-C)
  5. * [`punycode.c` by _Markus W. Scherer_ (IBM)](http://opensource.apple.com/source/ICU/ICU-400.42/icuSources/common/punycode.c)
  6. * [`punycode.c` by _Ben Noordhuis_](https://github.com/bnoordhuis/punycode/blob/master/punycode.c)
  7. * [JavaScript implementation by _some_](http://stackoverflow.com/questions/183485/can-anyone-recommend-a-good-free-javascript-for-punycode-to-unicode-conversion/301287#301287)
  8. * [`punycode.js` by _Ben Noordhuis_](https://github.com/joyent/node/blob/426298c8c1c0d5b5224ac3658c41e7c2a3fe9377/lib/punycode.js) (note: [not fully compliant](https://github.com/joyent/node/issues/2072))
  9. This project was [bundled](https://github.com/joyent/node/blob/master/lib/punycode.js) with Node.js from [v0.6.2+](https://github.com/joyent/node/compare/975f1930b1...61e796decc) until [v7](https://github.com/nodejs/node/pull/7941) (soft-deprecated).
  10. This project provides a CommonJS module that uses ES2015+ features and JavaScript module, which work in modern Node.js versions and browsers. For the old Punycode.js version that offers the same functionality in a UMD build with support for older pre-ES2015 runtimes, including Rhino, Ringo, and Narwhal, see [v1.4.1](https://github.com/mathiasbynens/punycode.js/releases/tag/v1.4.1).
  11. ## Installation
  12. Via [npm](https://www.npmjs.com/):
  13. ```bash
  14. npm install punycode --save
  15. ```
  16. In [Node.js](https://nodejs.org/):
  17. > ⚠️ Note that userland modules don't hide core modules.
  18. > For example, `require('punycode')` still imports the deprecated core module even if you executed `npm install punycode`.
  19. > Use `require('punycode/')` to import userland modules rather than core modules.
  20. ```js
  21. const punycode = require('punycode/');
  22. ```
  23. ## API
  24. ### `punycode.decode(string)`
  25. Converts a Punycode string of ASCII symbols to a string of Unicode symbols.
  26. ```js
  27. // decode domain name parts
  28. punycode.decode('maana-pta'); // 'mañana'
  29. punycode.decode('--dqo34k'); // '☃-⌘'
  30. ```
  31. ### `punycode.encode(string)`
  32. Converts a string of Unicode symbols to a Punycode string of ASCII symbols.
  33. ```js
  34. // encode domain name parts
  35. punycode.encode('mañana'); // 'maana-pta'
  36. punycode.encode('☃-⌘'); // '--dqo34k'
  37. ```
  38. ### `punycode.toUnicode(input)`
  39. 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.
  40. ```js
  41. // decode domain names
  42. punycode.toUnicode('xn--maana-pta.com');
  43. // → 'mañana.com'
  44. punycode.toUnicode('xn----dqo34k.com');
  45. // → '☃-⌘.com'
  46. // decode email addresses
  47. punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
  48. // → 'джумла@джpумлатест.bрфa'
  49. ```
  50. ### `punycode.toASCII(input)`
  51. 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.
  52. ```js
  53. // encode domain names
  54. punycode.toASCII('mañana.com');
  55. // → 'xn--maana-pta.com'
  56. punycode.toASCII('☃-⌘.com');
  57. // → 'xn----dqo34k.com'
  58. // encode email addresses
  59. punycode.toASCII('джумла@джpумлатест.bрфa');
  60. // → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
  61. ```
  62. ### `punycode.ucs2`
  63. #### `punycode.ucs2.decode(string)`
  64. 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.
  65. ```js
  66. punycode.ucs2.decode('abc');
  67. // → [0x61, 0x62, 0x63]
  68. // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
  69. punycode.ucs2.decode('\uD834\uDF06');
  70. // → [0x1D306]
  71. ```
  72. #### `punycode.ucs2.encode(codePoints)`
  73. Creates a string based on an array of numeric code point values.
  74. ```js
  75. punycode.ucs2.encode([0x61, 0x62, 0x63]);
  76. // → 'abc'
  77. punycode.ucs2.encode([0x1D306]);
  78. // → '\uD834\uDF06'
  79. ```
  80. ### `punycode.version`
  81. A string representing the current Punycode.js version number.
  82. ## For maintainers
  83. ### How to publish a new release
  84. 1. On the `main` branch, bump the version number in `package.json`:
  85. ```sh
  86. npm version patch -m 'Release v%s'
  87. ```
  88. Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
  89. Note that this produces a Git commit + tag.
  90. 1. Push the release commit and tag:
  91. ```sh
  92. git push && git push --tags
  93. ```
  94. Our CI then automatically publishes the new release to npm, under both the [`punycode`](https://www.npmjs.com/package/punycode) and [`punycode.js`](https://www.npmjs.com/package/punycode.js) names.
  95. ## Author
  96. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  97. |---|
  98. | [Mathias Bynens](https://mathiasbynens.be/) |
  99. ## License
  100. Punycode.js is available under the [MIT](https://mths.be/mit) license.