source: trip-planner-front/node_modules/core-js/internals/string-punycode-to-ascii.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.1 KB
Line 
1'use strict';
2// based on https://github.com/bestiejs/punycode.js/blob/master/punycode.js
3var maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
4var base = 36;
5var tMin = 1;
6var tMax = 26;
7var skew = 38;
8var damp = 700;
9var initialBias = 72;
10var initialN = 128; // 0x80
11var delimiter = '-'; // '\x2D'
12var regexNonASCII = /[^\0-\u007E]/; // non-ASCII chars
13var regexSeparators = /[.\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
14var OVERFLOW_ERROR = 'Overflow: input needs wider integers to process';
15var baseMinusTMin = base - tMin;
16var floor = Math.floor;
17var stringFromCharCode = String.fromCharCode;
18
19/**
20 * Creates an array containing the numeric code points of each Unicode
21 * character in the string. While JavaScript uses UCS-2 internally,
22 * this function will convert a pair of surrogate halves (each of which
23 * UCS-2 exposes as separate characters) into a single code point,
24 * matching UTF-16.
25 */
26var ucs2decode = function (string) {
27 var output = [];
28 var counter = 0;
29 var length = string.length;
30 while (counter < length) {
31 var value = string.charCodeAt(counter++);
32 if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
33 // It's a high surrogate, and there is a next character.
34 var extra = string.charCodeAt(counter++);
35 if ((extra & 0xFC00) == 0xDC00) { // Low surrogate.
36 output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
37 } else {
38 // It's an unmatched surrogate; only append this code unit, in case the
39 // next code unit is the high surrogate of a surrogate pair.
40 output.push(value);
41 counter--;
42 }
43 } else {
44 output.push(value);
45 }
46 }
47 return output;
48};
49
50/**
51 * Converts a digit/integer into a basic code point.
52 */
53var digitToBasic = function (digit) {
54 // 0..25 map to ASCII a..z or A..Z
55 // 26..35 map to ASCII 0..9
56 return digit + 22 + 75 * (digit < 26);
57};
58
59/**
60 * Bias adaptation function as per section 3.4 of RFC 3492.
61 * https://tools.ietf.org/html/rfc3492#section-3.4
62 */
63var adapt = function (delta, numPoints, firstTime) {
64 var k = 0;
65 delta = firstTime ? floor(delta / damp) : delta >> 1;
66 delta += floor(delta / numPoints);
67 for (; delta > baseMinusTMin * tMax >> 1; k += base) {
68 delta = floor(delta / baseMinusTMin);
69 }
70 return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
71};
72
73/**
74 * Converts a string of Unicode symbols (e.g. a domain name label) to a
75 * Punycode string of ASCII-only symbols.
76 */
77// eslint-disable-next-line max-statements -- TODO
78var encode = function (input) {
79 var output = [];
80
81 // Convert the input in UCS-2 to an array of Unicode code points.
82 input = ucs2decode(input);
83
84 // Cache the length.
85 var inputLength = input.length;
86
87 // Initialize the state.
88 var n = initialN;
89 var delta = 0;
90 var bias = initialBias;
91 var i, currentValue;
92
93 // Handle the basic code points.
94 for (i = 0; i < input.length; i++) {
95 currentValue = input[i];
96 if (currentValue < 0x80) {
97 output.push(stringFromCharCode(currentValue));
98 }
99 }
100
101 var basicLength = output.length; // number of basic code points.
102 var handledCPCount = basicLength; // number of code points that have been handled;
103
104 // Finish the basic string with a delimiter unless it's empty.
105 if (basicLength) {
106 output.push(delimiter);
107 }
108
109 // Main encoding loop:
110 while (handledCPCount < inputLength) {
111 // All non-basic code points < n have been handled already. Find the next larger one:
112 var m = maxInt;
113 for (i = 0; i < input.length; i++) {
114 currentValue = input[i];
115 if (currentValue >= n && currentValue < m) {
116 m = currentValue;
117 }
118 }
119
120 // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, but guard against overflow.
121 var handledCPCountPlusOne = handledCPCount + 1;
122 if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
123 throw RangeError(OVERFLOW_ERROR);
124 }
125
126 delta += (m - n) * handledCPCountPlusOne;
127 n = m;
128
129 for (i = 0; i < input.length; i++) {
130 currentValue = input[i];
131 if (currentValue < n && ++delta > maxInt) {
132 throw RangeError(OVERFLOW_ERROR);
133 }
134 if (currentValue == n) {
135 // Represent delta as a generalized variable-length integer.
136 var q = delta;
137 for (var k = base; /* no condition */; k += base) {
138 var t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
139 if (q < t) break;
140 var qMinusT = q - t;
141 var baseMinusT = base - t;
142 output.push(stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT)));
143 q = floor(qMinusT / baseMinusT);
144 }
145
146 output.push(stringFromCharCode(digitToBasic(q)));
147 bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
148 delta = 0;
149 ++handledCPCount;
150 }
151 }
152
153 ++delta;
154 ++n;
155 }
156 return output.join('');
157};
158
159module.exports = function (input) {
160 var encoded = [];
161 var labels = input.toLowerCase().replace(regexSeparators, '\u002E').split('.');
162 var i, label;
163 for (i = 0; i < labels.length; i++) {
164 label = labels[i];
165 encoded.push(regexNonASCII.test(label) ? 'xn--' + encode(label) : label);
166 }
167 return encoded.join('.');
168};
Note: See TracBrowser for help on using the repository browser.