Skip to content

Commit 24ba9fd

Browse files
mathiasbynensbnoordhuis
authored andcommitted
punycode: update to v1.2.3
1 parent f5e13ae commit 24ba9fd

1 file changed

Lines changed: 54 additions & 56 deletions

File tree

lib/punycode.js

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
/*! http://mths.be/punycode v1.2.0 by @mathias */
1+
/*! http://mths.be/punycode v1.2.3 by @mathias */
22
;(function(root) {
33

4+
/** Detect free variables */
5+
var freeExports = typeof exports == 'object' && exports;
6+
var freeModule = typeof module == 'object' && module &&
7+
module.exports == freeExports && module;
8+
var freeGlobal = typeof global == 'object' && global;
9+
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
10+
root = freeGlobal;
11+
}
12+
413
/**
514
* The `punycode` object.
615
* @name punycode
716
* @type Object
817
*/
918
var punycode,
1019

11-
/** Detect free variables `define`, `exports`, `module` and `require` */
12-
freeDefine = typeof define == 'function' && typeof define.amd == 'object' &&
13-
define.amd && define,
14-
freeExports = typeof exports == 'object' && exports,
15-
freeModule = typeof module == 'object' && module,
16-
freeRequire = typeof require == 'function' && require,
17-
1820
/** Highest positive signed 32-bit float value */
1921
maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
2022

@@ -90,7 +92,7 @@
9092
}
9193

9294
/**
93-
* Creates an array containing the decimal code points of each Unicode
95+
* Creates an array containing the numeric code points of each Unicode
9496
* character in the string. While JavaScript uses UCS-2 internally,
9597
* this function will convert a pair of surrogate halves (each of which
9698
* UCS-2 exposes as separate characters) into a single code point,
@@ -110,13 +112,16 @@
110112
extra;
111113
while (counter < length) {
112114
value = string.charCodeAt(counter++);
113-
if ((value & 0xF800) == 0xD800 && counter < length) {
115+
if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
114116
// high surrogate, and there is a next character
115117
extra = string.charCodeAt(counter++);
116118
if ((extra & 0xFC00) == 0xDC00) { // low surrogate
117119
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
118120
} else {
119-
output.push(value, extra);
121+
// unmatched surrogate; only append this code unit, in case the next
122+
// code unit is the high surrogate of a surrogate pair
123+
output.push(value);
124+
counter--;
120125
}
121126
} else {
122127
output.push(value);
@@ -126,11 +131,11 @@
126131
}
127132

128133
/**
129-
* Creates a string based on an array of decimal code points.
134+
* Creates a string based on an array of numeric code points.
130135
* @see `punycode.ucs2.decode`
131136
* @memberOf punycode.ucs2
132137
* @name encode
133-
* @param {Array} codePoints The array of decimal code points.
138+
* @param {Array} codePoints The array of numeric code points.
134139
* @returns {String} The new Unicode string (UCS-2).
135140
*/
136141
function ucs2encode(array) {
@@ -150,19 +155,22 @@
150155
* Converts a basic code point into a digit/integer.
151156
* @see `digitToBasic()`
152157
* @private
153-
* @param {Number} codePoint The basic (decimal) code point.
158+
* @param {Number} codePoint The basic numeric code point value.
154159
* @returns {Number} The numeric value of a basic code point (for use in
155160
* representing integers) in the range `0` to `base - 1`, or `base` if
156161
* the code point does not represent a value.
157162
*/
158163
function basicToDigit(codePoint) {
159-
return codePoint - 48 < 10
160-
? codePoint - 22
161-
: codePoint - 65 < 26
162-
? codePoint - 65
163-
: codePoint - 97 < 26
164-
? codePoint - 97
165-
: base;
164+
if (codePoint - 48 < 10) {
165+
return codePoint - 22;
166+
}
167+
if (codePoint - 65 < 26) {
168+
return codePoint - 65;
169+
}
170+
if (codePoint - 97 < 26) {
171+
return codePoint - 97;
172+
}
173+
return base;
166174
}
167175

168176
/**
@@ -174,7 +182,7 @@
174182
* representing integers) is `digit`, which needs to be in the range
175183
* `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
176184
* used; else, the lowercase form is used. The behavior is undefined
177-
* if flag is non-zero and `digit` has no uppercase form.
185+
* if `flag` is non-zero and `digit` has no uppercase form.
178186
*/
179187
function digitToBasic(digit, flag) {
180188
// 0..25 map to ASCII a..z or A..Z
@@ -198,25 +206,11 @@
198206
}
199207

200208
/**
201-
* Converts a basic code point to lowercase if `flag` is falsy, or to
202-
* uppercase if `flag` is truthy. The code point is unchanged if it's
203-
* caseless. The behavior is undefined if `codePoint` is not a basic code
204-
* point.
205-
* @private
206-
* @param {Number} codePoint The numeric value of a basic code point.
207-
* @returns {Number} The resulting basic code point.
208-
*/
209-
function encodeBasic(codePoint, flag) {
210-
codePoint -= (codePoint - 97 < 26) << 5;
211-
return codePoint + (!flag && codePoint - 65 < 26) << 5;
212-
}
213-
214-
/**
215-
* Converts a Punycode string of ASCII code points to a string of Unicode
216-
* code points.
209+
* Converts a Punycode string of ASCII-only symbols to a string of Unicode
210+
* symbols.
217211
* @memberOf punycode
218-
* @param {String} input The Punycode string of ASCII code points.
219-
* @returns {String} The resulting string of Unicode code points.
212+
* @param {String} input The Punycode string of ASCII-only symbols.
213+
* @returns {String} The resulting string of Unicode symbols.
220214
*/
221215
function decode(input) {
222216
// Don't use UCS-2
@@ -314,11 +308,11 @@
314308
}
315309

316310
/**
317-
* Converts a string of Unicode code points to a Punycode string of ASCII
318-
* code points.
311+
* Converts a string of Unicode symbols to a Punycode string of ASCII-only
312+
* symbols.
319313
* @memberOf punycode
320-
* @param {String} input The string of Unicode code points.
321-
* @returns {String} The resulting Punycode string of ASCII code points.
314+
* @param {String} input The string of Unicode symbols.
315+
* @returns {String} The resulting Punycode string of ASCII-only symbols.
322316
*/
323317
function encode(input) {
324318
var n,
@@ -470,10 +464,10 @@
470464
* @memberOf punycode
471465
* @type String
472466
*/
473-
'version': '1.2.0',
467+
'version': '1.2.3',
474468
/**
475469
* An object of methods to convert from JavaScript's internal character
476-
* representation (UCS-2) to decimal Unicode code points, and back.
470+
* representation (UCS-2) to Unicode code points, and back.
477471
* @see <http://mathiasbynens.be/notes/javascript-encoding>
478472
* @memberOf punycode
479473
* @type Object
@@ -489,21 +483,25 @@
489483
};
490484

491485
/** Expose `punycode` */
492-
if (freeExports) {
493-
if (freeModule && freeModule.exports == freeExports) {
494-
// in Node.js or Ringo 0.8+
486+
// Some AMD build optimizers, like r.js, check for specific condition patterns
487+
// like the following:
488+
if (
489+
typeof define == 'function' &&
490+
typeof define.amd == 'object' &&
491+
define.amd
492+
) {
493+
define(function() {
494+
return punycode;
495+
});
496+
} else if (freeExports && !freeExports.nodeType) {
497+
if (freeModule) { // in Node.js or RingoJS v0.8.0+
495498
freeModule.exports = punycode;
496-
} else {
497-
// in Narwhal or Ringo 0.7-
499+
} else { // in Narwhal or RingoJS v0.7.0-
498500
for (key in punycode) {
499501
punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
500502
}
501503
}
502-
} else if (freeDefine) {
503-
// via curl.js or RequireJS
504-
define('punycode', punycode);
505-
} else {
506-
// in a browser or Rhino
504+
} else { // in Rhino or a web browser
507505
root.punycode = punycode;
508506
}
509507

0 commit comments

Comments
 (0)