index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parse;
  6. function parse(input) {
  7. input = input.toUpperCase();
  8. var splitIndex = input.indexOf("P");
  9. var mantissa, exponent;
  10. if (splitIndex !== -1) {
  11. mantissa = input.substring(0, splitIndex);
  12. exponent = parseInt(input.substring(splitIndex + 1));
  13. } else {
  14. mantissa = input;
  15. exponent = 0;
  16. }
  17. var dotIndex = mantissa.indexOf(".");
  18. if (dotIndex !== -1) {
  19. var integerPart = parseInt(mantissa.substring(0, dotIndex), 16);
  20. var sign = Math.sign(integerPart);
  21. integerPart = sign * integerPart;
  22. var fractionLength = mantissa.length - dotIndex - 1;
  23. var fractionalPart = parseInt(mantissa.substring(dotIndex + 1), 16);
  24. var fraction = fractionLength > 0 ? fractionalPart / Math.pow(16, fractionLength) : 0;
  25. if (sign === 0) {
  26. if (fraction === 0) {
  27. mantissa = sign;
  28. } else {
  29. if (Object.is(sign, -0)) {
  30. mantissa = -fraction;
  31. } else {
  32. mantissa = fraction;
  33. }
  34. }
  35. } else {
  36. mantissa = sign * (integerPart + fraction);
  37. }
  38. } else {
  39. mantissa = parseInt(mantissa, 16);
  40. }
  41. return mantissa * (splitIndex !== -1 ? Math.pow(2, exponent) : 1);
  42. }