common.js 1.4 KB

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