|
34 | 34 | initialN = 128, // 0x80 |
35 | 35 | delimiter = '-', // '\x2D' |
36 | 36 |
|
| 37 | + /** Regular expressions */ |
| 38 | + regexASCII = /[^\x20-\x7e]/, |
| 39 | + regexPunycode = /^xn--/, |
| 40 | + |
37 | 41 | /** Error messages */ |
38 | 42 | errors = { |
39 | 43 | 'overflow': 'Overflow: input needs wider integers to process.', |
|
87 | 91 | */ |
88 | 92 | function mapDomain(string, fn) { |
89 | 93 | var glue = '.'; |
90 | | - return map(string.toLowerCase().split(glue), fn).join(glue); |
| 94 | + return map(string.split(glue), fn).join(glue); |
91 | 95 | } |
92 | 96 |
|
93 | 97 | /** |
|
196 | 200 |
|
197 | 201 | /** |
198 | 202 | * Converts a basic code point to lowercase is `flag` is falsy, or to |
| 203 | + * uppercase if `flag` is truthy. The code point is unchanged if it's |
199 | 204 | * caseless. The behavior is undefined if `codePoint` is not a basic code |
200 | 205 | * point. |
201 | 206 | * @private |
|
482 | 487 | return output.join(''); |
483 | 488 | } |
484 | 489 |
|
485 | | - /** |
486 | | - * Converts a Unicode string representing a domain name to Punycode. Only the |
487 | | - * non-ASCII parts of the domain name will be converted, i.e. it doesn't |
488 | | - * matter if you call it with a domain that's already in ASCII. |
489 | | - * @memberOf Punycode |
490 | | - * @param {String} domain The domain name to convert, as a Unicode string. |
491 | | - * @returns {String} The Punycode representation of the given domain name. |
492 | | - */ |
493 | | - function toASCII(domain) { |
494 | | - return mapDomain(domain, function(string) { |
495 | | - return string.match(/[^a-zA-Z0-9-]/) |
496 | | - ? 'xn--' + encode(string) |
497 | | - : string; |
498 | | - }); |
499 | | - } |
500 | | - |
501 | 490 | /** |
502 | 491 | * Converts a Punycode string representing a domain name to Unicode. Only the |
503 | 492 | * Punycoded parts of the domain name will be converted, i.e. it doesn't |
|
510 | 499 | */ |
511 | 500 | function toUnicode(domain) { |
512 | 501 | return mapDomain(domain, function(string) { |
513 | | - return string.match(/^xn--/) |
514 | | - ? decode(string.slice(4)) |
| 502 | + return regexPunycode.test(string) |
| 503 | + ? decode(string.slice(4).toLowerCase()) |
| 504 | + : string; |
| 505 | + }); |
| 506 | + } |
| 507 | + |
| 508 | + /** |
| 509 | + * Converts a Unicode string representing a domain name to Punycode. Only the |
| 510 | + * non-ASCII parts of the domain name will be converted, i.e. it doesn't |
| 511 | + * matter if you call it with a domain that's already in ASCII. |
| 512 | + * @memberOf Punycode |
| 513 | + * @param {String} domain The domain name to convert, as a Unicode string. |
| 514 | + * @returns {String} The Punycode representation of the given domain name. |
| 515 | + */ |
| 516 | + function toASCII(domain) { |
| 517 | + return mapDomain(domain, function(string) { |
| 518 | + return regexASCII.test(string) |
| 519 | + ? 'xn--' + encode(string) |
515 | 520 | : string; |
516 | 521 | }); |
517 | 522 | } |
|
520 | 525 |
|
521 | 526 | /** Define the public API */ |
522 | 527 | Punycode = { |
523 | | - 'version': '0.0.1337', |
| 528 | + 'version': '0.1.1', |
524 | 529 | /** |
525 | 530 | * An object of methods to convert from JavaScript's internal character |
526 | 531 | * representation to Unicode and back. |
527 | | - * @static |
528 | 532 | * @memberOf Punycode |
529 | 533 | * @type Object |
530 | 534 | */ |
|
0 commit comments