PagePath.js 917 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { pathUtils } from 'growi-commons';
  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 default class PagePath {
  7. constructor(path, evalDatePath = false) {
  8. const pagePath = pathUtils.normalizePath(path);
  9. this.former = null;
  10. this.latter = pagePath;
  11. // root
  12. if (pagePath === '/') {
  13. return;
  14. }
  15. // evaluate date path
  16. if (evalDatePath) {
  17. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  18. if (matchDate != null) {
  19. this.former = matchDate[1];
  20. this.latter = matchDate[2];
  21. return;
  22. }
  23. }
  24. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  25. if (matchDefault != null) {
  26. this.former = matchDefault[2];
  27. this.latter = matchDefault[3];
  28. }
  29. }
  30. }