utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var types = require('../../../utils/types.js');
  4. const timeUnits = [
  5. ["Y", 1e3 * 60 * 60 * 24 * 365],
  6. ["M", 1e3 * 60 * 60 * 24 * 30],
  7. ["D", 1e3 * 60 * 60 * 24],
  8. ["H", 1e3 * 60 * 60],
  9. ["m", 1e3 * 60],
  10. ["s", 1e3],
  11. ["S", 1]
  12. ];
  13. const getTime = (value) => {
  14. return types.isNumber(value) ? new Date(value).getTime() : value.valueOf();
  15. };
  16. const formatTime = (timestamp, format) => {
  17. let timeLeft = timestamp;
  18. const escapeRegex = /\[([^\]]*)]/g;
  19. const replacedText = timeUnits.reduce((current, [name, unit]) => {
  20. const replaceRegex = new RegExp(`${name}+(?![^\\[\\]]*\\])`, "g");
  21. if (replaceRegex.test(current)) {
  22. const value = Math.floor(timeLeft / unit);
  23. timeLeft -= value * unit;
  24. return current.replace(replaceRegex, (match) => String(value).padStart(match.length, "0"));
  25. }
  26. return current;
  27. }, format);
  28. return replacedText.replace(escapeRegex, "$1");
  29. };
  30. exports.formatTime = formatTime;
  31. exports.getTime = getTime;
  32. //# sourceMappingURL=utils.js.map