Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string-punycode-to-ascii.js 5.1KB

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