Skip to content

Commit b83febe

Browse files
committed
Don't infer filenames as hostnames
1 parent 0c97bcd commit b83febe

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [URI strings](#uri-strings)
1616
- [Email address strings](#email-address-strings)
1717
- [Other formats](#other-formats)
18+
- [Roadmap](#roadmap)
1819

1920
## 🚀 Features
2021

@@ -269,3 +270,9 @@ The following table illustrates the rest of the formats JSON Infer Types support
269270
| `'{ foo: 1, }'` | json | json5 |
270271

271272
Please feel free to request additional formats by opening a [Github issue](https://github.com/jsonhero-io/json-infer-types/issues)
273+
274+
## Roadmap
275+
276+
- Infer Firestore timestamps
277+
- Infer semver string
278+
- Infer Emoji

src/formats/hostname.ts

+89-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,91 @@
1+
import path from "path";
2+
3+
const filenameExtensions = [
4+
".7z",
5+
".aac",
6+
".au",
7+
".avi",
8+
".bmp",
9+
".bz2",
10+
".css",
11+
".csv",
12+
".dmg",
13+
".doc",
14+
".docx",
15+
".eot",
16+
".epub",
17+
".flac",
18+
".flv",
19+
".gif",
20+
".gz",
21+
".htm",
22+
".html",
23+
".ico",
24+
".jpeg",
25+
".jpg",
26+
".js",
27+
".json",
28+
".key",
29+
".m2v",
30+
".m4a",
31+
".m4v",
32+
".m4v",
33+
".md",
34+
".mov",
35+
".mp2",
36+
".mp3",
37+
".mp4",
38+
".mpe",
39+
".mpeg",
40+
".mpeg",
41+
".mpg",
42+
".mpg",
43+
".mpv",
44+
".mxf",
45+
".numbers",
46+
".odt",
47+
".ogg",
48+
".ogv",
49+
".pages",
50+
".pdf",
51+
".png",
52+
".ppt",
53+
".pptx",
54+
".psd",
55+
".rar",
56+
".raw",
57+
".rtf",
58+
".svg",
59+
".swf",
60+
".tar",
61+
".tgz",
62+
".tif",
63+
".tiff",
64+
".ts",
65+
".tsv",
66+
".ttf",
67+
".txt",
68+
".wav",
69+
".webp",
70+
".wmv",
71+
".woff",
72+
".woff2",
73+
".xls",
74+
".xlsx",
75+
".xml",
76+
".xz",
77+
".yaml",
78+
".yml",
79+
".z",
80+
".zip",
81+
];
82+
83+
function isNotFilename(value: string): boolean {
84+
const extname = path.extname(value);
85+
86+
return extname === "" || !filenameExtensions.includes(extname);
87+
}
88+
189
export type JSONHostnameFormat = {
290
name: "hostname";
391
variant: "rfc1123" | "rfc5890";
@@ -42,7 +130,7 @@ function isValidHostname(value: string, allowUnderscore = false): boolean {
42130
return validLabel;
43131
});
44132

45-
return isValid;
133+
return isValid && isNotFilename(value);
46134
}
47135

48136
export function inferHostname(value: string): JSONHostnameFormat | undefined {

tests/stringFormats.test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,15 @@ describe("hostnames", () => {
476476
});
477477
});
478478

479-
test.each([`${"example".repeat(36)}.com`])("%p should NOT be inferred as a hostname", (value) => {
480-
expect(inferType(value)).toEqual({
481-
name: "string",
482-
value,
483-
});
484-
});
479+
test.each([`${"example".repeat(36)}.com`, "Screen_Shot_2021-08-16_at_11_12_27_AM.png"])(
480+
"%p should NOT be inferred as a hostname",
481+
(value) => {
482+
expect(inferType(value)).toEqual({
483+
name: "string",
484+
value,
485+
});
486+
},
487+
);
485488
});
486489

487490
describe("file sizes", () => {

0 commit comments

Comments
 (0)