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.

преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Punycode.js [![Build status](https://travis-ci.org/bestiejs/punycode.js.svg?branch=master)](https://travis-ci.org/bestiejs/punycode.js) [![Code coverage status](http://img.shields.io/coveralls/bestiejs/punycode.js/master.svg)](https://coveralls.io/r/bestiejs/punycode.js) [![Dependency status](https://gemnasium.com/bestiejs/punycode.js.svg)](https://gemnasium.com/bestiejs/punycode.js)
  2. A robust Punycode converter that fully complies to [RFC 3492](http://tools.ietf.org/html/rfc3492) and [RFC 5891](http://tools.ietf.org/html/rfc5891), and works on nearly all JavaScript platforms.
  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](http://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 is [bundled](https://github.com/joyent/node/blob/master/lib/punycode.js) with [Node.js v0.6.2+](https://github.com/joyent/node/compare/975f1930b1...61e796decc).
  10. ## Installation
  11. Via [npm](http://npmjs.org/) (only required for Node.js releases older than v0.6.2):
  12. ```bash
  13. npm install punycode
  14. ```
  15. Via [Bower](http://bower.io/):
  16. ```bash
  17. bower install punycode
  18. ```
  19. Via [Component](https://github.com/component/component):
  20. ```bash
  21. component install bestiejs/punycode.js
  22. ```
  23. In a browser:
  24. ```html
  25. <script src="punycode.js"></script>
  26. ```
  27. In [Narwhal](http://narwhaljs.org/), [Node.js](http://nodejs.org/), and [RingoJS](http://ringojs.org/):
  28. ```js
  29. var punycode = require('punycode');
  30. ```
  31. In [Rhino](http://www.mozilla.org/rhino/):
  32. ```js
  33. load('punycode.js');
  34. ```
  35. Using an AMD loader like [RequireJS](http://requirejs.org/):
  36. ```js
  37. require(
  38. {
  39. 'paths': {
  40. 'punycode': 'path/to/punycode'
  41. }
  42. },
  43. ['punycode'],
  44. function(punycode) {
  45. console.log(punycode);
  46. }
  47. );
  48. ```
  49. ## API
  50. ### `punycode.decode(string)`
  51. Converts a Punycode string of ASCII symbols to a string of Unicode symbols.
  52. ```js
  53. // decode domain name parts
  54. punycode.decode('maana-pta'); // 'mañana'
  55. punycode.decode('--dqo34k'); // '☃-⌘'
  56. ```
  57. ### `punycode.encode(string)`
  58. Converts a string of Unicode symbols to a Punycode string of ASCII symbols.
  59. ```js
  60. // encode domain name parts
  61. punycode.encode('mañana'); // 'maana-pta'
  62. punycode.encode('☃-⌘'); // '--dqo34k'
  63. ```
  64. ### `punycode.toUnicode(input)`
  65. 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.
  66. ```js
  67. // decode domain names
  68. punycode.toUnicode('xn--maana-pta.com');
  69. // → 'mañana.com'
  70. punycode.toUnicode('xn----dqo34k.com');
  71. // → '☃-⌘.com'
  72. // decode email addresses
  73. punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
  74. // → 'джумла@джpумлатест.bрфa'
  75. ```
  76. ### `punycode.toASCII(input)`
  77. Converts a 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.
  78. ```js
  79. // encode domain names
  80. punycode.toASCII('mañana.com');
  81. // → 'xn--maana-pta.com'
  82. punycode.toASCII('☃-⌘.com');
  83. // → 'xn----dqo34k.com'
  84. // encode email addresses
  85. punycode.toASCII('джумла@джpумлатест.bрфa');
  86. // → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
  87. ```
  88. ### `punycode.ucs2`
  89. #### `punycode.ucs2.decode(string)`
  90. 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.
  91. ```js
  92. punycode.ucs2.decode('abc');
  93. // → [0x61, 0x62, 0x63]
  94. // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
  95. punycode.ucs2.decode('\uD834\uDF06');
  96. // → [0x1D306]
  97. ```
  98. #### `punycode.ucs2.encode(codePoints)`
  99. Creates a string based on an array of numeric code point values.
  100. ```js
  101. punycode.ucs2.encode([0x61, 0x62, 0x63]);
  102. // → 'abc'
  103. punycode.ucs2.encode([0x1D306]);
  104. // → '\uD834\uDF06'
  105. ```
  106. ### `punycode.version`
  107. A string representing the current Punycode.js version number.
  108. ## Unit tests & code coverage
  109. After cloning this repository, run `npm install --dev` to install the dependencies needed for Punycode.js development and testing. You may want to install Istanbul _globally_ using `npm install istanbul -g`.
  110. Once that’s done, you can run the unit tests in Node using `npm test` or `node tests/tests.js`. To run the tests in Rhino, Ringo, Narwhal, PhantomJS, and web browsers as well, use `grunt test`.
  111. To generate the code coverage report, use `grunt cover`.
  112. Feel free to fork if you see possible improvements!
  113. ## Author
  114. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  115. |---|
  116. | [Mathias Bynens](https://mathiasbynens.be/) |
  117. ## Contributors
  118. | [![twitter/jdalton](https://gravatar.com/avatar/299a3d891ff1920b69c364d061007043?s=70)](https://twitter.com/jdalton "Follow @jdalton on Twitter") |
  119. |---|
  120. | [John-David Dalton](http://allyoucanleet.com/) |
  121. ## License
  122. Punycode.js is available under the [MIT](https://mths.be/mit) license.