LEFT | RIGHT |
| 1 /* |
| 2 * Copyright (C) 2011-2016 Mathias Bynens <https://mathiasbynens.be/> |
| 3 * Copyright (C) 2016 Eyeo GmbH (Minor modifications for compatibility.) |
| 4 * |
| 5 * Permission is hereby granted, free of charge, to any person obtaining |
| 6 * a copy of this software and associated documentation files (the |
| 7 * "Software"), to deal in the Software without restriction, including |
| 8 * without limitation the rights to use, copy, modify, merge, publish, |
| 9 * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 * permit persons to whom the Software is furnished to do so, subject to |
| 11 * the following conditions: |
| 12 * |
| 13 * The above copyright notice and this permission notice shall be |
| 14 * included in all copies or substantial portions of the Software. |
| 15 * |
| 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 */ |
| 24 |
1 'use strict'; | 25 'use strict'; |
2 | 26 |
3 /** Highest positive signed 32-bit float value */ | 27 /** Highest positive signed 32-bit float value */ |
4 const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 | 28 const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 |
5 | 29 |
6 /** Bootstring parameters */ | 30 /** Bootstring parameters */ |
7 const base = 36; | 31 const base = 36; |
8 const tMin = 1; | 32 const tMin = 1; |
9 const tMax = 26; | 33 const tMax = 26; |
10 const skew = 38; | 34 const skew = 38; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 147 } |
124 | 148 |
125 /** | 149 /** |
126 * Creates a string based on an array of numeric code points. | 150 * Creates a string based on an array of numeric code points. |
127 * @see `punycode.ucs2.decode` | 151 * @see `punycode.ucs2.decode` |
128 * @memberOf punycode.ucs2 | 152 * @memberOf punycode.ucs2 |
129 * @name encode | 153 * @name encode |
130 * @param {Array} codePoints The array of numeric code points. | 154 * @param {Array} codePoints The array of numeric code points. |
131 * @returns {String} The new Unicode string (UCS-2). | 155 * @returns {String} The new Unicode string (UCS-2). |
132 */ | 156 */ |
133 const ucs2encode = array => String.fromCodePoint(...array); | 157 const ucs2encode = array => String.fromCodePoint.apply(null, array); |
134 | 158 |
135 /** | 159 /** |
136 * Converts a basic code point into a digit/integer. | 160 * Converts a basic code point into a digit/integer. |
137 * @see `digitToBasic()` | 161 * @see `digitToBasic()` |
138 * @private | 162 * @private |
139 * @param {Number} codePoint The basic numeric code point value. | 163 * @param {Number} codePoint The basic numeric code point value. |
140 * @returns {Number} The numeric value of a basic code point (for use in | 164 * @returns {Number} The numeric value of a basic code point (for use in |
141 * representing integers) in the range `0` to `base - 1`, or `base` if | 165 * representing integers) in the range `0` to `base - 1`, or `base` if |
142 * the code point does not represent a value. | 166 * the code point does not represent a value. |
143 */ | 167 */ |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 291 } |
268 | 292 |
269 n += floor(i / out); | 293 n += floor(i / out); |
270 i %= out; | 294 i %= out; |
271 | 295 |
272 // Insert `n` at position `i` of the output. | 296 // Insert `n` at position `i` of the output. |
273 output.splice(i++, 0, n); | 297 output.splice(i++, 0, n); |
274 | 298 |
275 } | 299 } |
276 | 300 |
277 » return String.fromCodePoint(...output); | 301 » return String.fromCodePoint.apply(null, output); |
278 }; | 302 }; |
279 | 303 |
280 /** | 304 /** |
281 * Converts a string of Unicode symbols (e.g. a domain name label) to a | 305 * Converts a string of Unicode symbols (e.g. a domain name label) to a |
282 * Punycode string of ASCII-only symbols. | 306 * Punycode string of ASCII-only symbols. |
283 * @memberOf punycode | 307 * @memberOf punycode |
284 * @param {String} input The string of Unicode symbols. | 308 * @param {String} input The string of Unicode symbols. |
285 * @returns {String} The resulting Punycode string of ASCII-only symbols. | 309 * @returns {String} The resulting Punycode string of ASCII-only symbols. |
286 */ | 310 */ |
287 const encode = function(input) { | 311 const encode = function(input) { |
288 const output = []; | 312 const output = []; |
289 | 313 |
290 // Convert the input in UCS-2 to an array of Unicode code points. | 314 // Convert the input in UCS-2 to an array of Unicode code points. |
291 input = ucs2decode(input); | 315 input = ucs2decode(input); |
292 | 316 |
293 // Cache the length. | 317 // Cache the length. |
294 let inputLength = input.length; | 318 let inputLength = input.length; |
295 | 319 |
296 // Initialize the state. | 320 // Initialize the state. |
297 let n = initialN; | 321 let n = initialN; |
298 let delta = 0; | 322 let delta = 0; |
299 let bias = initialBias; | 323 let bias = initialBias; |
300 | 324 |
301 // Handle the basic code points. | 325 // Handle the basic code points. |
302 » for (const currentValue of input) { | 326 » for (let currentValue of input) { |
303 if (currentValue < 0x80) { | 327 if (currentValue < 0x80) { |
304 output.push(stringFromCharCode(currentValue)); | 328 output.push(stringFromCharCode(currentValue)); |
305 } | 329 } |
306 } | 330 } |
307 | 331 |
308 let basicLength = output.length; | 332 let basicLength = output.length; |
309 let handledCPCount = basicLength; | 333 let handledCPCount = basicLength; |
310 | 334 |
311 // `handledCPCount` is the number of code points that have been handled; | 335 // `handledCPCount` is the number of code points that have been handled; |
312 // `basicLength` is the number of basic code points. | 336 // `basicLength` is the number of basic code points. |
313 | 337 |
314 // Finish the basic string with a delimiter unless it's empty. | 338 // Finish the basic string with a delimiter unless it's empty. |
315 if (basicLength) { | 339 if (basicLength) { |
316 output.push(delimiter); | 340 output.push(delimiter); |
317 } | 341 } |
318 | 342 |
319 // Main encoding loop: | 343 // Main encoding loop: |
320 while (handledCPCount < inputLength) { | 344 while (handledCPCount < inputLength) { |
321 | 345 |
322 // All non-basic code points < n have been handled already. Find
the next | 346 // All non-basic code points < n have been handled already. Find
the next |
323 // larger one: | 347 // larger one: |
324 let m = maxInt; | 348 let m = maxInt; |
325 » » for (const currentValue of input) { | 349 » » for (let currentValue of input) { |
326 if (currentValue >= n && currentValue < m) { | 350 if (currentValue >= n && currentValue < m) { |
327 m = currentValue; | 351 m = currentValue; |
328 } | 352 } |
329 } | 353 } |
330 | 354 |
331 // Increase `delta` enough to advance the decoder's <n,i> state
to <m,0>, | 355 // Increase `delta` enough to advance the decoder's <n,i> state
to <m,0>, |
332 // but guard against overflow. | 356 // but guard against overflow. |
333 const handledCPCountPlusOne = handledCPCount + 1; | 357 const handledCPCountPlusOne = handledCPCount + 1; |
334 if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { | 358 if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { |
335 error('overflow'); | 359 error('overflow'); |
336 } | 360 } |
337 | 361 |
338 delta += (m - n) * handledCPCountPlusOne; | 362 delta += (m - n) * handledCPCountPlusOne; |
339 n = m; | 363 n = m; |
340 | 364 |
341 » » for (const currentValue of input) { | 365 » » for (let currentValue of input) { |
342 if (currentValue < n && ++delta > maxInt) { | 366 if (currentValue < n && ++delta > maxInt) { |
343 error('overflow'); | 367 error('overflow'); |
344 } | 368 } |
345 if (currentValue == n) { | 369 if (currentValue == n) { |
346 // Represent delta as a generalized variable-len
gth integer. | 370 // Represent delta as a generalized variable-len
gth integer. |
347 let q = delta; | 371 let q = delta; |
348 for (let k = base; /* no condition */; k += base
) { | 372 for (let k = base; /* no condition */; k += base
) { |
349 const t = k <= bias ? tMin : (k >= bias
+ tMax ? tMax : k - bias); | 373 const t = k <= bias ? tMin : (k >= bias
+ tMax ? tMax : k - bias); |
350 if (q < t) { | 374 if (q < t) { |
351 break; | 375 break; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 return mapDomain(input, function(string) { | 430 return mapDomain(input, function(string) { |
407 return regexNonASCII.test(string) | 431 return regexNonASCII.test(string) |
408 ? 'xn--' + encode(string) | 432 ? 'xn--' + encode(string) |
409 : string; | 433 : string; |
410 }); | 434 }); |
411 }; | 435 }; |
412 | 436 |
413 /*--------------------------------------------------------------------------*/ | 437 /*--------------------------------------------------------------------------*/ |
414 | 438 |
415 /** Define the public API */ | 439 /** Define the public API */ |
416 const punycode = { | 440 exports = { |
417 /** | 441 /** |
418 * A string representing the current Punycode.js version number. | 442 * A string representing the current Punycode.js version number. |
419 * @memberOf punycode | 443 * @memberOf punycode |
420 * @type String | 444 * @type String |
421 */ | 445 */ |
422 'version': '2.0.0', | 446 'version': '2.0.0', |
423 /** | 447 /** |
424 * An object of methods to convert from JavaScript's internal character | 448 * An object of methods to convert from JavaScript's internal character |
425 * representation (UCS-2) to Unicode code points, and back. | 449 * representation (UCS-2) to Unicode code points, and back. |
426 * @see <https://mathiasbynens.be/notes/javascript-encoding> | 450 * @see <https://mathiasbynens.be/notes/javascript-encoding> |
427 * @memberOf punycode | 451 * @memberOf punycode |
428 * @type Object | 452 * @type Object |
429 */ | 453 */ |
430 'ucs2': { | 454 'ucs2': { |
431 'decode': ucs2decode, | 455 'decode': ucs2decode, |
432 'encode': ucs2encode | 456 'encode': ucs2encode |
433 }, | 457 }, |
434 'decode': decode, | 458 'decode': decode, |
435 'encode': encode, | 459 'encode': encode, |
436 'toASCII': toASCII, | 460 'toASCII': toASCII, |
437 'toUnicode': toUnicode | 461 'toUnicode': toUnicode |
438 }; | 462 }; |
439 | |
440 module.exports = punycode; | |
LEFT | RIGHT |