-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPathMap.ts
31 lines (25 loc) · 869 Bytes
/
PathMap.ts
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
export type PathMap<T> = { [ path: string ]: T };
const pathMatch = (path: string, pattern: string): Record<string, string> | null => {
return null;
}
export function longestMatchingPath<T>(pathMap: PathMap<T>, path: string): string | undefined {
let exactPath = '/' + path + '.';
let item = pathMap[exactPath];
if (item) return exactPath;
const pathParts = path.split('/');
while (true) {
exactPath = '/' + pathParts.join('/');
item = pathMap[exactPath];
if (item) {
return exactPath;
} else {
if (pathParts.length === 0) break;
pathParts.pop();
}
}
return undefined;
}
export function getByPath<T>(pathMap: PathMap<T>, path: string) {
const matchPath = longestMatchingPath(pathMap, path);
return matchPath ? pathMap[matchPath] : undefined;
}