utils.js 1.1 KB

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