formatters.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.floatFormatter = exports.keepSignificantDigits = exports.integerFormatter = void 0;
  4. const leftPadding = (size) => {
  5. return (input) => {
  6. if (input.length < size) {
  7. return " ".repeat(size - input.length) + input;
  8. }
  9. return input;
  10. };
  11. };
  12. exports.integerFormatter = (size) => {
  13. const padding = leftPadding(size);
  14. return (integer) => padding("" + integer);
  15. };
  16. const { floor, log10, pow } = Math;
  17. const numberOfDigits = (n) => floor(log10(n) + 1);
  18. exports.keepSignificantDigits = (digits) => (value) => {
  19. const valueDigits = numberOfDigits(value);
  20. if (valueDigits > digits) {
  21. const extraDigits = valueDigits - digits;
  22. const magnitude = pow(10, extraDigits);
  23. return value - (value % magnitude);
  24. }
  25. return value;
  26. };
  27. exports.floatFormatter = (size, fractionDigits) => {
  28. const numberFormatter = new Intl.NumberFormat("en-US", {
  29. maximumFractionDigits: fractionDigits,
  30. minimumFractionDigits: fractionDigits,
  31. useGrouping: false,
  32. });
  33. const padding = leftPadding(size);
  34. return (float) => padding(numberFormatter.format(float));
  35. };
  36. //# sourceMappingURL=formatters.js.map