index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var conversions = {};
  3. module.exports = conversions;
  4. function sign(x) {
  5. return x < 0 ? -1 : 1;
  6. }
  7. function evenRound(x) {
  8. // Round x to the nearest integer, choosing the even integer if it lies halfway between two.
  9. if ((x % 1) === 0.5 && (x & 1) === 0) { // [even number].5; round down (i.e. floor)
  10. return Math.floor(x);
  11. } else {
  12. return Math.round(x);
  13. }
  14. }
  15. function createNumberConversion(bitLength, typeOpts) {
  16. if (!typeOpts.unsigned) {
  17. --bitLength;
  18. }
  19. const lowerBound = typeOpts.unsigned ? 0 : -Math.pow(2, bitLength);
  20. const upperBound = Math.pow(2, bitLength) - 1;
  21. const moduloVal = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength) : Math.pow(2, bitLength);
  22. const moduloBound = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength - 1) : Math.pow(2, bitLength - 1);
  23. return function(V, opts) {
  24. if (!opts) opts = {};
  25. let x = +V;
  26. if (opts.enforceRange) {
  27. if (!Number.isFinite(x)) {
  28. throw new TypeError("Argument is not a finite number");
  29. }
  30. x = sign(x) * Math.floor(Math.abs(x));
  31. if (x < lowerBound || x > upperBound) {
  32. throw new TypeError("Argument is not in byte range");
  33. }
  34. return x;
  35. }
  36. if (!isNaN(x) && opts.clamp) {
  37. x = evenRound(x);
  38. if (x < lowerBound) x = lowerBound;
  39. if (x > upperBound) x = upperBound;
  40. return x;
  41. }
  42. if (!Number.isFinite(x) || x === 0) {
  43. return 0;
  44. }
  45. x = sign(x) * Math.floor(Math.abs(x));
  46. x = x % moduloVal;
  47. if (!typeOpts.unsigned && x >= moduloBound) {
  48. return x - moduloVal;
  49. } else if (typeOpts.unsigned) {
  50. if (x < 0) {
  51. x += moduloVal;
  52. } else if (x === -0) { // don't return negative zero
  53. return 0;
  54. }
  55. }
  56. return x;
  57. }
  58. }
  59. conversions["void"] = function () {
  60. return undefined;
  61. };
  62. conversions["boolean"] = function (val) {
  63. return !!val;
  64. };
  65. conversions["byte"] = createNumberConversion(8, { unsigned: false });
  66. conversions["octet"] = createNumberConversion(8, { unsigned: true });
  67. conversions["short"] = createNumberConversion(16, { unsigned: false });
  68. conversions["unsigned short"] = createNumberConversion(16, { unsigned: true });
  69. conversions["long"] = createNumberConversion(32, { unsigned: false });
  70. conversions["unsigned long"] = createNumberConversion(32, { unsigned: true });
  71. conversions["long long"] = createNumberConversion(32, { unsigned: false, moduloBitLength: 64 });
  72. conversions["unsigned long long"] = createNumberConversion(32, { unsigned: true, moduloBitLength: 64 });
  73. conversions["double"] = function (V) {
  74. const x = +V;
  75. if (!Number.isFinite(x)) {
  76. throw new TypeError("Argument is not a finite floating-point value");
  77. }
  78. return x;
  79. };
  80. conversions["unrestricted double"] = function (V) {
  81. const x = +V;
  82. if (isNaN(x)) {
  83. throw new TypeError("Argument is NaN");
  84. }
  85. return x;
  86. };
  87. // not quite valid, but good enough for JS
  88. conversions["float"] = conversions["double"];
  89. conversions["unrestricted float"] = conversions["unrestricted double"];
  90. conversions["DOMString"] = function (V, opts) {
  91. if (!opts) opts = {};
  92. if (opts.treatNullAsEmptyString && V === null) {
  93. return "";
  94. }
  95. return String(V);
  96. };
  97. conversions["ByteString"] = function (V, opts) {
  98. const x = String(V);
  99. let c = undefined;
  100. for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
  101. if (c > 255) {
  102. throw new TypeError("Argument is not a valid bytestring");
  103. }
  104. }
  105. return x;
  106. };
  107. conversions["USVString"] = function (V) {
  108. const S = String(V);
  109. const n = S.length;
  110. const U = [];
  111. for (let i = 0; i < n; ++i) {
  112. const c = S.charCodeAt(i);
  113. if (c < 0xD800 || c > 0xDFFF) {
  114. U.push(String.fromCodePoint(c));
  115. } else if (0xDC00 <= c && c <= 0xDFFF) {
  116. U.push(String.fromCodePoint(0xFFFD));
  117. } else {
  118. if (i === n - 1) {
  119. U.push(String.fromCodePoint(0xFFFD));
  120. } else {
  121. const d = S.charCodeAt(i + 1);
  122. if (0xDC00 <= d && d <= 0xDFFF) {
  123. const a = c & 0x3FF;
  124. const b = d & 0x3FF;
  125. U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b));
  126. ++i;
  127. } else {
  128. U.push(String.fromCodePoint(0xFFFD));
  129. }
  130. }
  131. }
  132. }
  133. return U.join('');
  134. };
  135. conversions["Date"] = function (V, opts) {
  136. if (!(V instanceof Date)) {
  137. throw new TypeError("Argument is not a Date object");
  138. }
  139. if (isNaN(V)) {
  140. return undefined;
  141. }
  142. return V;
  143. };
  144. conversions["RegExp"] = function (V, opts) {
  145. if (!(V instanceof RegExp)) {
  146. V = new RegExp(V);
  147. }
  148. return V;
  149. };