/
devided-page-path.js
52 lines (42 loc) · 1.46 KB
/
devided-page-path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as pathUtils from '../utils/path-utils';
// https://regex101.com/r/BahpKX/2
const PATTERN_INCLUDE_DATE = /^(.+\/[^/]+)\/(\d{4}|\d{4}\/\d{2}|\d{4}\/\d{2}\/\d{2})$/;
export class DevidedPagePath {
constructor(path, skipNormalize = false, evalDatePath = false) {
this.isRoot = false;
this.isFormerRoot = false;
this.former = null;
this.latter = null;
// root
if (path == null || path === '' || path === '/') {
this.isRoot = true;
this.latter = '/';
return;
}
const pagePath = skipNormalize ? path : pathUtils.normalizePath(path);
this.latter = pagePath;
// evaluate date path
if (evalDatePath) {
const matchDate = pagePath.match(PATTERN_INCLUDE_DATE);
if (matchDate != null) {
this.former = matchDate[1];
this.latter = matchDate[2];
return;
}
}
let PATTERN_DEFAULT = /^((.*)\/)?(.+)$/; // this will not ignore html end tags https://regex101.com/r/jpZwIe/1
try { // for non-chrome browsers
// eslint-disable-next-line regex/invalid
PATTERN_DEFAULT = new RegExp('^((.*)(?<!<)\\/)?(.+)$'); // https://regex101.com/r/HJNvMW/1
}
catch (err) {
// lookbehind regex is not supported on non-chrome browsers
}
const matchDefault = pagePath.match(PATTERN_DEFAULT);
if (matchDefault != null) {
this.isFormerRoot = matchDefault[1] === '/';
this.former = matchDefault[2];
this.latter = matchDefault[3];
}
}
}