-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (109 loc) · 3.21 KB
/
index.ts
File metadata and controls
135 lines (109 loc) · 3.21 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as _ from './lib';
import type {
ParthOptions,
ParthResult,
PathOpt,
RegexEntry,
ParthRegex,
} from './types';
export type { ParthOptions, ParthResult };
const qshRE = /[?#][^/\s]*/g;
const depthRE = /((^|[/?#.\s]+)[(:\w])/g;
const paramRE = /:([-\w]+)(\([^\s]+?[)][?)]*)?/g;
const noParamRE = /(^|[/.=\s]+)(\(.+?\)+)/g;
export class Parth {
#store: Record<string, PathOpt> = {};
#regex: ParthRegex;
constructor(options?: ParthOptions) {
this.#regex = [] as unknown as ParthRegex;
this.#regex.master = new RegExp('(?:[])');
this.#regex.defaultRE = /[^?#./\s]+/;
if (options?.defaultRE) {
this.#regex.defaultRE = options.defaultRE;
}
}
set(path: string, opt?: Partial<PathOpt>): this {
if (typeof path !== 'string') {
return this;
}
const o = _.cloneDeep(opt || {}) as PathOpt;
o.path = path.replace(/\s+/g, ' ').trim();
if (this.#store[o.path]) {
Object.assign(this.#store[o.path], o);
return this;
}
this.#regex.push((this.#store[o.path] = o) as RegexEntry);
let index = -1;
const defaultRE = '(' + this.#regex.defaultRE.source + ')';
o.stem = o.path.replace(noParamRE, function ($0, $1: string, $2: string) {
return $1 + ':' + (++index) + $2;
});
const url = (o.stem!.match(/[^\s]*\/\S*/) || [null]).pop();
const qsh = _.getQueryString(url);
if (url && !qsh) {
o.stem = o.stem!.replace(
url,
url.replace(/\/$/, '$1') + ':qs(?:\\/)?(' + qshRE.source + ')?',
);
}
o.depth = o.stem!.split(depthRE).length || -1;
o.regex = new RegExp(
'^' +
o
.stem!.replace(/\S+/g, function (s) {
return s.replace(paramRE, function ($0, $1, $2) {
return $2 || defaultRE;
});
})
.replace(/[^?( )+*$]+(?=\(|$)/g, function escapeRegExp($0) {
return $0.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
}),
);
this.#regex.sort(function (x, y) {
return y.depth - x.depth || y.stem.localeCompare(x.stem);
});
this.#regex.master = new RegExp(
'(' +
this.#regex
.map(function (el) {
return el.regex.source.replace(/\((?=[^?])/g, '(?:');
})
.join(')|(') +
')',
);
return this;
}
get(path: string): ParthResult | null {
if (typeof path !== 'string') {
return null;
}
path = path.replace(/\s+/g, ' ').trim();
if (this.#store[path]) {
return Object.assign(_.cloneDeep(this.#store[path]), {
match: path,
notFound: '',
params: {},
});
}
const found = this.#regex.master.exec(path);
if (!found) {
return null;
}
const o: ParthResult = {
match: found.shift()!,
params: {},
notFound: '',
};
const matchIndex = found.indexOf(o.match);
const foundEntry = this.#regex[matchIndex];
o.notFound = path.slice(o.match.length);
let paramIndex = -1;
const params = foundEntry.regex.exec(path)!.slice(1);
foundEntry.stem.replace(paramRE, function ($0, $1) {
o.params[$1] = params[++paramIndex];
return $0;
});
return Object.assign(_.cloneDeep(foundEntry), o);
}
}
export default Parth;