utils.mjs 946 B

12345678910111213141516171819202122232425262728293031
  1. import { isNumber } from "../../../utils/types.mjs";
  2. //#region ../../packages/components/countdown/src/utils.ts
  3. const timeUnits = [
  4. ["Y", 1e3 * 60 * 60 * 24 * 365],
  5. ["M", 1e3 * 60 * 60 * 24 * 30],
  6. ["D", 1e3 * 60 * 60 * 24],
  7. ["H", 1e3 * 60 * 60],
  8. ["m", 1e3 * 60],
  9. ["s", 1e3],
  10. ["S", 1]
  11. ];
  12. const getTime = (value) => {
  13. return isNumber(value) ? new Date(value).getTime() : value.valueOf();
  14. };
  15. const formatTime = (timestamp, format) => {
  16. let timeLeft = timestamp;
  17. return timeUnits.reduce((current, [name, unit]) => {
  18. const replaceRegex = new RegExp(`${name}+(?![^\\[\\]]*\\])`, "g");
  19. if (replaceRegex.test(current)) {
  20. const value = Math.floor(timeLeft / unit);
  21. timeLeft -= value * unit;
  22. return current.replace(replaceRegex, (match) => String(value).padStart(match.length, "0"));
  23. }
  24. return current;
  25. }, format).replace(/\[([^\]]*)]/g, "$1");
  26. };
  27. //#endregion
  28. export { formatTime, getTime };
  29. //# sourceMappingURL=utils.mjs.map