peerlibrary/punycode.js

Name: punycode.js

Owner: PeerLibrary

Description: A robust Punycode converter that fully complies to RFC 3492 and RFC 5891.

Created: 2015-11-07 22:35:06.0

Updated: 2015-11-07 22:35:07.0

Pushed: 2015-09-02 11:00:21.0

Homepage: https://mths.be/punycode

Size: 3365

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Punycode.js Build status Code coverage status Dependency status

A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.

This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm:

This project is bundled with Node.js v0.6.2+ and io.js v1.0.0+.

Installation

Via npm (only required for Node.js releases older than v0.6.2):

install punycode

Via Bower:

r install punycode

Via Component:

onent install bestiejs/punycode.js

In a browser:

ipt src="punycode.js"></script>

In Node.js, io.js, Narwhal, and RingoJS:

punycode = require('punycode');

In Rhino:

('punycode.js');

Using an AMD loader like RequireJS:

ire(

'paths': {
  'punycode': 'path/to/punycode'
}

punycode'],
nction(punycode) {
console.log(punycode);


API
punycode.decode(string)

Converts a Punycode string of ASCII symbols to a string of Unicode symbols.

ecode domain name parts
code.decode('maana-pta'); // 'maņana'
code.decode('--dqo34k'); // '?-?'
punycode.encode(string)

Converts a string of Unicode symbols to a Punycode string of ASCII symbols.

ncode domain name parts
code.encode('maņana'); // 'maana-pta'
code.encode('?-?'); // '--dqo34k'
punycode.toUnicode(input)

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.

ecode domain names
code.toUnicode('xn--maana-pta.com');
 'maņana.com'
code.toUnicode('xn----dqo34k.com');
 '?-?.com'

ecode email addresses
code.toUnicode('??????@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
 '??????@??p????????.b??a'
punycode.toASCII(input)

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.

ncode domain names
code.toASCII('maņana.com');
 'xn--maana-pta.com'
code.toASCII('?-?.com');
 'xn----dqo34k.com'

ncode email addresses
code.toASCII('??????@??p????????.b??a');
 '??????@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
punycode.ucs2
punycode.ucs2.decode(string)

Creates an array containing the numeric code point values of each Unicode symbol in the string. While JavaScript uses UCS-2 internally, 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.

code.ucs2.decode('abc');
 [0x61, 0x62, 0x63]
urrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
code.ucs2.decode('\uD834\uDF06');
 [0x1D306]
punycode.ucs2.encode(codePoints)

Creates a string based on an array of numeric code point values.

code.ucs2.encode([0x61, 0x62, 0x63]);
 'abc'
code.ucs2.encode([0x1D306]);
 '\uD834\uDF06'
punycode.version

A string representing the current Punycode.js version number.

Unit tests & code coverage

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.

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.

To generate the code coverage report, use grunt cover.

Feel free to fork if you see possible improvements!

Author

| twitter/mathias | |—| | Mathias Bynens |

Contributors

| twitter/jdalton | |—| | John-David Dalton |

License

Punycode.js is available under the MIT license.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.