-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExtension.ts
More file actions
46 lines (38 loc) · 999 Bytes
/
FileExtension.ts
File metadata and controls
46 lines (38 loc) · 999 Bytes
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
import { FSName } from "./FSName";
/**
* File extension is a string that starts with dot and doesn't contain slash
*
* Examples:
* - .txt
* - .png
*
* File extension can be empty string
*
*/
export class FileExtension implements FSName {
constructor(name?: string)
{
this.value = name === undefined
? ""
: FileExtension.parse(name);
}
readonly value: string;
get valueWithoutDot(): string {
return this.isEmpty
? ""
: this.value.substring(1);
}
get isEmpty(): boolean {
return this.value === "";
}
static from(source: string): FileExtension {
return new FileExtension(source);
}
static parse = (name: string): string => {
if (name.indexOf("/") > -1)
throw new Error("FileExtension cannot contain slash");
if (!name.startsWith("."))
throw new Error("FileExtension must start with dot");
return name;
}
}