2
0

devided-page-path.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. export class DevidedPagePath {
  5. constructor(path, skipNormalize = false, evalDatePath = false) {
  6. this.isRoot = false;
  7. this.isFormerRoot = false;
  8. this.former = null;
  9. this.latter = null;
  10. // root
  11. if (path == null || path === '' || path === '/') {
  12. this.isRoot = true;
  13. this.latter = '/';
  14. return;
  15. }
  16. const pagePath = skipNormalize ? path : pathUtils.normalizePath(path);
  17. this.latter = pagePath;
  18. // evaluate date path
  19. if (evalDatePath) {
  20. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  21. if (matchDate != null) {
  22. this.former = matchDate[1];
  23. this.latter = matchDate[2];
  24. return;
  25. }
  26. }
  27. let PATTERN_DEFAULT = /^((.*)\/)?(.+)$/; // this will not ignore html end tags https://regex101.com/r/jpZwIe/1
  28. try { // for non-chrome browsers
  29. PATTERN_DEFAULT = new RegExp('^((.*)(?<!<)\\/)?(.+)$'); // https://regex101.com/r/HJNvMW/1
  30. }
  31. catch (err) {
  32. // lookbehind regex is not supported on non-chrome browsers
  33. }
  34. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  35. if (matchDefault != null) {
  36. this.isFormerRoot = matchDefault[1] === '/';
  37. this.former = matchDefault[2];
  38. this.latter = matchDefault[3];
  39. }
  40. }
  41. }