punycode: update to v1.2.3
authorMathias Bynens <mathias@qiwi.be>
Thu, 20 Jun 2013 10:40:44 +0000 (12:40 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Thu, 20 Jun 2013 11:02:53 +0000 (13:02 +0200)
lib/punycode.js

index 163923c..41803ba 100644 (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) {
 
+       /** 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.
         * @name punycode
@@ -8,13 +17,6 @@
         */
        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 */
        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,
         * this function will convert a pair of surrogate halves (each of which
         * UCS-2 exposes as separate characters) into a single code point,
                    extra;
                while (counter < length) {
                        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
                                extra = string.charCodeAt(counter++);
                                if ((extra & 0xFC00) == 0xDC00) { // low surrogate
                                        output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
                                } 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 {
                                output.push(value);
        }
 
        /**
-        * 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`
         * @memberOf punycode.ucs2
         * @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).
         */
        function ucs2encode(array) {
         * Converts a basic code point into a digit/integer.
         * @see `digitToBasic()`
         * @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
         * representing integers) in the range `0` to `base - 1`, or `base` if
         * the code point does not represent a value.
         */
        function basicToDigit(codePoint) {
-               return codePoint - 48 < 10
-                       ? codePoint - 22
-                       : codePoint - 65 < 26
-                               ? codePoint - 65
-                               : codePoint - 97 < 26
-                                       ? codePoint - 97
-                                       : base;
+               if (codePoint - 48 < 10) {
+                       return codePoint - 22;
+               }
+               if (codePoint - 65 < 26) {
+                       return codePoint - 65;
+               }
+               if (codePoint - 97 < 26) {
+                       return codePoint - 97;
+               }
+               return base;
        }
 
        /**
         * representing integers) is `digit`, which needs to be in the range
         * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
         * 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) {
                //  0..25 map to ASCII a..z or A..Z
        }
 
        /**
-        * Converts a basic code point to lowercase if `flag` is falsy, or to
-        * uppercase if `flag` is truthy. The code point is unchanged if it's
-        * 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.
+        * Converts a Punycode string of ASCII-only symbols to a string of Unicode
+        * symbols.
         * @memberOf punycode
-        * @param {String} input The Punycode string of ASCII code points.
-        * @returns {String} The resulting string of Unicode code points.
+        * @param {String} input The Punycode string of ASCII-only symbols.
+        * @returns {String} The resulting string of Unicode symbols.
         */
        function decode(input) {
                // Don't use UCS-2
        }
 
        /**
-        * Converts a string of Unicode code points to a Punycode string of ASCII
-        * code points.
+        * Converts a string of Unicode symbols to a Punycode string of ASCII-only
+        * symbols.
         * @memberOf punycode
-        * @param {String} input The string of Unicode code points.
-        * @returns {String} The resulting Punycode string of ASCII code points.
+        * @param {String} input The string of Unicode symbols.
+        * @returns {String} The resulting Punycode string of ASCII-only symbols.
         */
        function encode(input) {
                var n,
                 * @memberOf punycode
                 * @type String
                 */
-               'version': '1.2.0',
+               'version': '1.2.3',
                /**
                 * 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>
                 * @memberOf punycode
                 * @type Object
        };
 
        /** Expose `punycode` */
-       if (freeExports) {
-               if (freeModule && freeModule.exports == freeExports) {
-                       // in Node.js or Ringo 0.8+
+       // Some AMD build optimizers, like r.js, check for specific condition patterns
+       // like the following:
+       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;
-               } else {
-                       // in Narwhal or Ringo 0.7-
+               } else { // in Narwhal or RingoJS v0.7.0-
                        for (key in punycode) {
                                punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
                        }
                }
-       } else if (freeDefine) {
-               // via curl.js or RequireJS
-               define('punycode', punycode);
-       } else {
-               // in a browser or Rhino
+       } else { // in Rhino or a web browser
                root.punycode = punycode;
        }