Skip to content

Commit 897208e

Browse files
mathiasbynensbnoordhuis
authored andcommitted
punycode: Update to v0.1.1.
1 parent 3421f43 commit 897208e

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

lib/punycode.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
initialN = 128, // 0x80
3535
delimiter = '-', // '\x2D'
3636

37+
/** Regular expressions */
38+
regexASCII = /[^\x20-\x7e]/,
39+
regexPunycode = /^xn--/,
40+
3741
/** Error messages */
3842
errors = {
3943
'overflow': 'Overflow: input needs wider integers to process.',
@@ -87,7 +91,7 @@
8791
*/
8892
function mapDomain(string, fn) {
8993
var glue = '.';
90-
return map(string.toLowerCase().split(glue), fn).join(glue);
94+
return map(string.split(glue), fn).join(glue);
9195
}
9296

9397
/**
@@ -196,6 +200,7 @@
196200

197201
/**
198202
* 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
199204
* caseless. The behavior is undefined if `codePoint` is not a basic code
200205
* point.
201206
* @private
@@ -482,22 +487,6 @@
482487
return output.join('');
483488
}
484489

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-
501490
/**
502491
* Converts a Punycode string representing a domain name to Unicode. Only the
503492
* Punycoded parts of the domain name will be converted, i.e. it doesn't
@@ -510,8 +499,24 @@
510499
*/
511500
function toUnicode(domain) {
512501
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)
515520
: string;
516521
});
517522
}
@@ -520,11 +525,10 @@
520525

521526
/** Define the public API */
522527
Punycode = {
523-
'version': '0.0.1337',
528+
'version': '0.1.1',
524529
/**
525530
* An object of methods to convert from JavaScript's internal character
526531
* representation to Unicode and back.
527-
* @static
528532
* @memberOf Punycode
529533
* @type Object
530534
*/

0 commit comments

Comments
 (0)