stringifyNumber.js 748 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. function stringifyNumber({ format, minFractionDigits, tag, value }) {
  3. if (typeof value === 'bigint')
  4. return String(value);
  5. const num = typeof value === 'number' ? value : Number(value);
  6. if (!isFinite(num))
  7. return isNaN(num) ? '.nan' : num < 0 ? '-.inf' : '.inf';
  8. let n = JSON.stringify(value);
  9. if (!format &&
  10. minFractionDigits &&
  11. (!tag || tag === 'tag:yaml.org,2002:float') &&
  12. /^\d/.test(n)) {
  13. let i = n.indexOf('.');
  14. if (i < 0) {
  15. i = n.length;
  16. n += '.';
  17. }
  18. let d = minFractionDigits - (n.length - i - 1);
  19. while (d-- > 0)
  20. n += '0';
  21. }
  22. return n;
  23. }
  24. exports.stringifyNumber = stringifyNumber;