common.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const { Pattern } = require('hexo-util');
  3. const moment = require('moment-timezone');
  4. const micromatch = require('micromatch');
  5. const DURATION_MINUTE = 1000 * 60;
  6. function isMatch(path, patterns) {
  7. if (!patterns) return false;
  8. return micromatch.isMatch(path, patterns);
  9. }
  10. function isTmpFile(path) {
  11. return path.endsWith('%') || path.endsWith('~');
  12. }
  13. function isHiddenFile(path) {
  14. return /(^|\/)[_.]/.test(path);
  15. }
  16. function isExcludedFile(path, config) {
  17. if (isTmpFile(path)) return true;
  18. if (isMatch(path, config.exclude)) return true;
  19. if (isHiddenFile(path) && !isMatch(path, config.include)) return true;
  20. return false;
  21. }
  22. exports.ignoreTmpAndHiddenFile = new Pattern(path => {
  23. if (isTmpFile(path) || isHiddenFile(path)) return false;
  24. return true;
  25. });
  26. exports.isTmpFile = isTmpFile;
  27. exports.isHiddenFile = isHiddenFile;
  28. exports.isExcludedFile = isExcludedFile;
  29. exports.toDate = date => {
  30. if (!date || moment.isMoment(date)) return date;
  31. if (!(date instanceof Date)) {
  32. date = new Date(date);
  33. }
  34. if (isNaN(date.getTime())) return;
  35. return date;
  36. };
  37. exports.timezone = (date, timezone) => {
  38. if (moment.isMoment(date)) date = date.toDate();
  39. const offset = date.getTimezoneOffset();
  40. const ms = date.getTime();
  41. const target = moment.tz.zone(timezone).utcOffset(ms);
  42. const diff = (offset - target) * DURATION_MINUTE;
  43. return new Date(ms - diff);
  44. };
  45. exports.isMatch = isMatch;