-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_path.mts
28 lines (25 loc) · 887 Bytes
/
url_path.mts
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
/**
* Represents the path segment of a web URL. The path always starts with a slash
* and is validated at construction.
*/
export class UrlPath {
/** The path represented as a pure string. */
public readonly path: string;
private constructor({ path }: { path: string }) {
this.path = path;
}
/**
* Validates the given path and returns a `UrlPath` object representing that
* path. Throws an error if the path is not considered valid.
*
* @param path The path to use for the resulting `UrlPath` object. **Must**
* start with a `/` character.
* @returns A `UrlPath` object for the given `path` input.
*/
public static of(path: string): UrlPath {
if (!path.startsWith('/')) {
throw new Error(`UrlPath (${path}) must start with a "/".`);
}
return new UrlPath({ path });
}
}