123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- 'use strict';
- const maxInt = 2147483647;
- const base = 36;
- const tMin = 1;
- const tMax = 26;
- const skew = 38;
- const damp = 700;
- const initialBias = 72;
- const initialN = 128;
- const delimiter = '-';
- const regexPunycode = /^xn--/;
- const regexNonASCII = /[^\0-\x7E]/;
- const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g;
- const errors = {
- 'overflow': 'Overflow: input needs wider integers to process',
- 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
- 'invalid-input': 'Invalid input'
- };
- const baseMinusTMin = base - tMin;
- const floor = Math.floor;
- const stringFromCharCode = String.fromCharCode;
- function error(type) {
- throw new RangeError(errors[type]);
- }
- function map(array, fn) {
- const result = [];
- let length = array.length;
- while (length--) {
- result[length] = fn(array[length]);
- }
- return result;
- }
- function mapDomain(string, fn) {
- const parts = string.split('@');
- let result = '';
- if (parts.length > 1) {
-
-
- result = parts[0] + '@';
- string = parts[1];
- }
-
- string = string.replace(regexSeparators, '\x2E');
- const labels = string.split('.');
- const encoded = map(labels, fn).join('.');
- return result + encoded;
- }
- function ucs2decode(string) {
- const output = [];
- let counter = 0;
- const length = string.length;
- while (counter < length) {
- const value = string.charCodeAt(counter++);
- if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
-
- const extra = string.charCodeAt(counter++);
- if ((extra & 0xFC00) == 0xDC00) {
- output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
- } else {
-
-
- output.push(value);
- counter--;
- }
- } else {
- output.push(value);
- }
- }
- return output;
- }
- const ucs2encode = array => String.fromCodePoint(...array);
- const basicToDigit = function(codePoint) {
- if (codePoint - 0x30 < 0x0A) {
- return codePoint - 0x16;
- }
- if (codePoint - 0x41 < 0x1A) {
- return codePoint - 0x41;
- }
- if (codePoint - 0x61 < 0x1A) {
- return codePoint - 0x61;
- }
- return base;
- };
- const digitToBasic = function(digit, flag) {
-
-
- return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
- };
- const adapt = function(delta, numPoints, firstTime) {
- let k = 0;
- delta = firstTime ? floor(delta / damp) : delta >> 1;
- delta += floor(delta / numPoints);
- for (; delta > baseMinusTMin * tMax >> 1; k += base) {
- delta = floor(delta / baseMinusTMin);
- }
- return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
- };
- const decode = function(input) {
-
- const output = [];
- const inputLength = input.length;
- let i = 0;
- let n = initialN;
- let bias = initialBias;
-
-
-
- let basic = input.lastIndexOf(delimiter);
- if (basic < 0) {
- basic = 0;
- }
- for (let j = 0; j < basic; ++j) {
-
- if (input.charCodeAt(j) >= 0x80) {
- error('not-basic');
- }
- output.push(input.charCodeAt(j));
- }
-
-
- for (let index = basic > 0 ? basic + 1 : 0; index < inputLength; ) {
-
-
-
-
-
- let oldi = i;
- for (let w = 1, k = base; ; k += base) {
- if (index >= inputLength) {
- error('invalid-input');
- }
- const digit = basicToDigit(input.charCodeAt(index++));
- if (digit >= base || digit > floor((maxInt - i) / w)) {
- error('overflow');
- }
- i += digit * w;
- const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
- if (digit < t) {
- break;
- }
- const baseMinusT = base - t;
- if (w > floor(maxInt / baseMinusT)) {
- error('overflow');
- }
- w *= baseMinusT;
- }
- const out = output.length + 1;
- bias = adapt(i - oldi, out, oldi == 0);
-
-
- if (floor(i / out) > maxInt - n) {
- error('overflow');
- }
- n += floor(i / out);
- i %= out;
-
- output.splice(i++, 0, n);
- }
- return String.fromCodePoint(...output);
- };
- const encode = function(input) {
- const output = [];
-
- input = ucs2decode(input);
-
- let inputLength = input.length;
-
- let n = initialN;
- let delta = 0;
- let bias = initialBias;
-
- for (const currentValue of input) {
- if (currentValue < 0x80) {
- output.push(stringFromCharCode(currentValue));
- }
- }
- let basicLength = output.length;
- let handledCPCount = basicLength;
-
-
-
- if (basicLength) {
- output.push(delimiter);
- }
-
- while (handledCPCount < inputLength) {
-
-
- let m = maxInt;
- for (const currentValue of input) {
- if (currentValue >= n && currentValue < m) {
- m = currentValue;
- }
- }
-
-
- const handledCPCountPlusOne = handledCPCount + 1;
- if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
- error('overflow');
- }
- delta += (m - n) * handledCPCountPlusOne;
- n = m;
- for (const currentValue of input) {
- if (currentValue < n && ++delta > maxInt) {
- error('overflow');
- }
- if (currentValue == n) {
-
- let q = delta;
- for (let k = base; ; k += base) {
- const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
- if (q < t) {
- break;
- }
- const qMinusT = q - t;
- const baseMinusT = base - t;
- output.push(
- stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
- );
- q = floor(qMinusT / baseMinusT);
- }
- output.push(stringFromCharCode(digitToBasic(q, 0)));
- bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
- delta = 0;
- ++handledCPCount;
- }
- }
- ++delta;
- ++n;
- }
- return output.join('');
- };
- const toUnicode = function(input) {
- return mapDomain(input, function(string) {
- return regexPunycode.test(string)
- ? decode(string.slice(4).toLowerCase())
- : string;
- });
- };
- const toASCII = function(input) {
- return mapDomain(input, function(string) {
- return regexNonASCII.test(string)
- ? 'xn--' + encode(string)
- : string;
- });
- };
- const punycode = {
-
- 'version': '2.1.0',
-
- 'ucs2': {
- 'decode': ucs2decode,
- 'encode': ucs2encode
- },
- 'decode': decode,
- 'encode': encode,
- 'toASCII': toASCII,
- 'toUnicode': toUnicode
- };
- module.exports = punycode;
|