punycode: update to v1.2.3

This commit is contained in:
Mathias Bynens 2013-06-20 12:40:44 +02:00 committed by Ben Noordhuis
parent f5e13ae9b5
commit 24ba9fdec8

View File

@ -1,6 +1,15 @@
/*! http://mths.be/punycode v1.2.0 by @mathias */ /*! http://mths.be/punycode v1.2.3 by @mathias */
;(function(root) { ;(function(root) {
/** Detect free variables */
var freeExports = typeof exports == 'object' && exports;
var freeModule = typeof module == 'object' && module &&
module.exports == freeExports && module;
var freeGlobal = typeof global == 'object' && global;
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
root = freeGlobal;
}
/** /**
* The `punycode` object. * The `punycode` object.
* @name punycode * @name punycode
@ -8,13 +17,6 @@
*/ */
var punycode, var punycode,
/** Detect free variables `define`, `exports`, `module` and `require` */
freeDefine = typeof define == 'function' && typeof define.amd == 'object' &&
define.amd && define,
freeExports = typeof exports == 'object' && exports,
freeModule = typeof module == 'object' && module,
freeRequire = typeof require == 'function' && require,
/** Highest positive signed 32-bit float value */ /** Highest positive signed 32-bit float value */
maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
@ -90,7 +92,7 @@
} }
/** /**
* Creates an array containing the decimal code points of each Unicode * Creates an array containing the numeric code points of each Unicode
* character in the string. While JavaScript uses UCS-2 internally, * character in the string. While JavaScript uses UCS-2 internally,
* this function will convert a pair of surrogate halves (each of which * this function will convert a pair of surrogate halves (each of which
* UCS-2 exposes as separate characters) into a single code point, * UCS-2 exposes as separate characters) into a single code point,
@ -110,13 +112,16 @@
extra; extra;
while (counter < length) { while (counter < length) {
value = string.charCodeAt(counter++); value = string.charCodeAt(counter++);
if ((value & 0xF800) == 0xD800 && counter < length) { if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
// high surrogate, and there is a next character // high surrogate, and there is a next character
extra = string.charCodeAt(counter++); extra = string.charCodeAt(counter++);
if ((extra & 0xFC00) == 0xDC00) { // low surrogate if ((extra & 0xFC00) == 0xDC00) { // low surrogate
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
} else { } else {
output.push(value, extra); // unmatched surrogate; only append this code unit, in case the next
// code unit is the high surrogate of a surrogate pair
output.push(value);
counter--;
} }
} else { } else {
output.push(value); output.push(value);
@ -126,11 +131,11 @@
} }
/** /**
* Creates a string based on an array of decimal code points. * Creates a string based on an array of numeric code points.
* @see `punycode.ucs2.decode` * @see `punycode.ucs2.decode`
* @memberOf punycode.ucs2 * @memberOf punycode.ucs2
* @name encode * @name encode
* @param {Array} codePoints The array of decimal code points. * @param {Array} codePoints The array of numeric code points.
* @returns {String} The new Unicode string (UCS-2). * @returns {String} The new Unicode string (UCS-2).
*/ */
function ucs2encode(array) { function ucs2encode(array) {
@ -150,19 +155,22 @@
* Converts a basic code point into a digit/integer. * Converts a basic code point into a digit/integer.
* @see `digitToBasic()` * @see `digitToBasic()`
* @private * @private
* @param {Number} codePoint The basic (decimal) code point. * @param {Number} codePoint The basic numeric code point value.
* @returns {Number} The numeric value of a basic code point (for use in * @returns {Number} The numeric value of a basic code point (for use in
* representing integers) in the range `0` to `base - 1`, or `base` if * representing integers) in the range `0` to `base - 1`, or `base` if
* the code point does not represent a value. * the code point does not represent a value.
*/ */
function basicToDigit(codePoint) { function basicToDigit(codePoint) {
return codePoint - 48 < 10 if (codePoint - 48 < 10) {
? codePoint - 22 return codePoint - 22;
: codePoint - 65 < 26 }
? codePoint - 65 if (codePoint - 65 < 26) {
: codePoint - 97 < 26 return codePoint - 65;
? codePoint - 97 }
: base; if (codePoint - 97 < 26) {
return codePoint - 97;
}
return base;
} }
/** /**
@ -174,7 +182,7 @@
* representing integers) is `digit`, which needs to be in the range * representing integers) is `digit`, which needs to be in the range
* `0` to `base - 1`. If `flag` is non-zero, the uppercase form is * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
* used; else, the lowercase form is used. The behavior is undefined * used; else, the lowercase form is used. The behavior is undefined
* if flag is non-zero and `digit` has no uppercase form. * if `flag` is non-zero and `digit` has no uppercase form.
*/ */
function digitToBasic(digit, flag) { function digitToBasic(digit, flag) {
// 0..25 map to ASCII a..z or A..Z // 0..25 map to ASCII a..z or A..Z
@ -198,25 +206,11 @@
} }
/** /**
* Converts a basic code point to lowercase if `flag` is falsy, or to * Converts a Punycode string of ASCII-only symbols to a string of Unicode
* uppercase if `flag` is truthy. The code point is unchanged if it's * symbols.
* caseless. The behavior is undefined if `codePoint` is not a basic code
* point.
* @private
* @param {Number} codePoint The numeric value of a basic code point.
* @returns {Number} The resulting basic code point.
*/
function encodeBasic(codePoint, flag) {
codePoint -= (codePoint - 97 < 26) << 5;
return codePoint + (!flag && codePoint - 65 < 26) << 5;
}
/**
* Converts a Punycode string of ASCII code points to a string of Unicode
* code points.
* @memberOf punycode * @memberOf punycode
* @param {String} input The Punycode string of ASCII code points. * @param {String} input The Punycode string of ASCII-only symbols.
* @returns {String} The resulting string of Unicode code points. * @returns {String} The resulting string of Unicode symbols.
*/ */
function decode(input) { function decode(input) {
// Don't use UCS-2 // Don't use UCS-2
@ -314,11 +308,11 @@
} }
/** /**
* Converts a string of Unicode code points to a Punycode string of ASCII * Converts a string of Unicode symbols to a Punycode string of ASCII-only
* code points. * symbols.
* @memberOf punycode * @memberOf punycode
* @param {String} input The string of Unicode code points. * @param {String} input The string of Unicode symbols.
* @returns {String} The resulting Punycode string of ASCII code points. * @returns {String} The resulting Punycode string of ASCII-only symbols.
*/ */
function encode(input) { function encode(input) {
var n, var n,
@ -470,10 +464,10 @@
* @memberOf punycode * @memberOf punycode
* @type String * @type String
*/ */
'version': '1.2.0', 'version': '1.2.3',
/** /**
* An object of methods to convert from JavaScript's internal character * An object of methods to convert from JavaScript's internal character
* representation (UCS-2) to decimal Unicode code points, and back. * representation (UCS-2) to Unicode code points, and back.
* @see <http://mathiasbynens.be/notes/javascript-encoding> * @see <http://mathiasbynens.be/notes/javascript-encoding>
* @memberOf punycode * @memberOf punycode
* @type Object * @type Object
@ -489,21 +483,25 @@
}; };
/** Expose `punycode` */ /** Expose `punycode` */
if (freeExports) { // Some AMD build optimizers, like r.js, check for specific condition patterns
if (freeModule && freeModule.exports == freeExports) { // like the following:
// in Node.js or Ringo 0.8+ if (
typeof define == 'function' &&
typeof define.amd == 'object' &&
define.amd
) {
define(function() {
return punycode;
});
} else if (freeExports && !freeExports.nodeType) {
if (freeModule) { // in Node.js or RingoJS v0.8.0+
freeModule.exports = punycode; freeModule.exports = punycode;
} else { } else { // in Narwhal or RingoJS v0.7.0-
// in Narwhal or Ringo 0.7-
for (key in punycode) { for (key in punycode) {
punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
} }
} }
} else if (freeDefine) { } else { // in Rhino or a web browser
// via curl.js or RequireJS
define('punycode', punycode);
} else {
// in a browser or Rhino
root.punycode = punycode; root.punycode = punycode;
} }