devided-page-path.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as pathUtils from '../utils/path-utils';
  2. // https://regex101.com/r/BahpKX/2
  3. const PATTERN_INCLUDE_DATE = /^(.+\/[^/]+)\/(\d{4}|\d{4}\/\d{2}|\d{4}\/\d{2}\/\d{2})$/;
  4. // https://regex101.com/r/WVpPpY/1
  5. const PATTERN_DEFAULT = /^((.*)\/)?([^/]+)$/;
  6. export class DevidedPagePath {
  7. constructor(path, skipNormalize = false, evalDatePath = false) {
  8. this.isRoot = false;
  9. this.isFormerRoot = false;
  10. this.former = null;
  11. this.latter = null;
  12. // root
  13. if (path == null || path === '' || path === '/') {
  14. this.isRoot = true;
  15. this.latter = '/';
  16. return;
  17. }
  18. const pagePath = skipNormalize ? path : pathUtils.normalizePath(path);
  19. this.latter = pagePath;
  20. // evaluate date path
  21. if (evalDatePath) {
  22. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  23. if (matchDate != null) {
  24. this.former = matchDate[1];
  25. this.latter = matchDate[2];
  26. return;
  27. }
  28. }
  29. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  30. if (matchDefault != null) {
  31. this.isFormerRoot = matchDefault[1] === '/';
  32. this.former = matchDefault[2];
  33. this.latter = matchDefault[3];
  34. }
  35. }
  36. }