validate.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * 判断是否是外部链接
  3. * @param {string} path 路径
  4. * @returns {boolean}
  5. */
  6. export function isExternal(path) {
  7. return /^(https?:|mailto:|tel:)/.test(path);
  8. }
  9. /**
  10. * 格式化时间
  11. * @param {Date|string|number} time 时间
  12. * @param {string} format 格式
  13. * @returns {string}
  14. */
  15. export function formatDate(time, format = "YYYY-MM-DD HH:mm:ss") {
  16. if (!time) {
  17. return "";
  18. }
  19. const date = new Date(time);
  20. const year = date.getFullYear();
  21. const month = date.getMonth() + 1;
  22. const day = date.getDate();
  23. const hour = date.getHours();
  24. const minute = date.getMinutes();
  25. const second = date.getSeconds();
  26. const formatMap = {
  27. YYYY: year.toString(),
  28. MM: month.toString().padStart(2, "0"),
  29. DD: day.toString().padStart(2, "0"),
  30. HH: hour.toString().padStart(2, "0"),
  31. mm: minute.toString().padStart(2, "0"),
  32. ss: second.toString().padStart(2, "0"),
  33. };
  34. return format.replace(/(YYYY|MM|DD|HH|mm|ss)/g, (match) => formatMap[match]);
  35. }
  36. /**
  37. * 深拷贝
  38. * @param {Object} obj 要拷贝的对象
  39. * @returns {Object}
  40. */
  41. export function deepClone(obj) {
  42. if (obj === null || typeof obj !== "object") {
  43. return obj;
  44. }
  45. const result = Array.isArray(obj) ? [] : {};
  46. for (const key in obj) {
  47. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  48. result[key] = deepClone(obj[key]);
  49. }
  50. }
  51. return result;
  52. }