utils.js 661 B

1234567891011121314151617181920212223242526272829303132
  1. exports.nano = time => +time[0] * 1e9 + +time[1];
  2. exports.scale = {
  3. 'w': 6048e11,
  4. 'd': 864e11,
  5. 'h': 36e11,
  6. 'm': 6e10,
  7. 's': 1e9,
  8. 'ms': 1e6,
  9. 'μs': 1e3,
  10. 'ns': 1,
  11. };
  12. exports.regex = {
  13. 'w': /^(w((ee)?k)?s?)$/,
  14. 'd': /^(d(ay)?s?)$/,
  15. 'h': /^(h((ou)?r)?s?)$/,
  16. 'm': /^(min(ute)?s?|m)$/,
  17. 's': /^((sec(ond)?)s?|s)$/,
  18. 'ms': /^(milli(second)?s?|ms)$/,
  19. 'μs': /^(micro(second)?s?|μs)$/,
  20. 'ns': /^(nano(second)?s?|ns?)$/,
  21. };
  22. exports.isSmallest = function(uom, unit) {
  23. return exports.regex[uom].test(unit);
  24. };
  25. exports.round = function(num, digits) {
  26. const n = Math.abs(num);
  27. return /[0-9]/.test(digits) ? n.toFixed(digits) : Math.round(n);
  28. };