PagePath.js 777 B

1234567891011121314151617181920212223242526272829303132
  1. // https://regex101.com/r/BahpKX/2
  2. const PATTERN_INCLUDE_DATE = /^(.+\/[^/]+)\/(\d{4}|\d{4}\/\d{2}|\d{4}\/\d{2}\/\d{2})$/;
  3. // https://regex101.com/r/WVpPpY/1
  4. const PATTERN_DEFAULT = /^((.*)\/)?([^/]+)$/;
  5. export default class PagePath {
  6. constructor(pagePath, evalDatePath = false) {
  7. this.former = null;
  8. this.latter = null;
  9. // root
  10. if (pagePath === '/') {
  11. return;
  12. }
  13. // evaluate date path
  14. if (evalDatePath) {
  15. const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
  16. if (matchDate != null) {
  17. this.former = matchDate[1];
  18. this.latter = matchDate[2];
  19. return;
  20. }
  21. }
  22. const matchDefault = pagePath.match(PATTERN_DEFAULT);
  23. this.former = matchDefault[2];
  24. this.latter = matchDefault[3];
  25. }
  26. }