Skip to content

Commit 7af44d4

Browse files
committed
refactor: add ciaplu for pattern matching in language detection and MIME type resolution
1 parent 0479e53 commit 7af44d4

File tree

4 files changed

+47
-56
lines changed

4 files changed

+47
-56
lines changed

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"babel-loader": "~8.2.3",
132132
"better-sqlite3": "~10.0.0",
133133
"chalk": "~4.1.2",
134+
"ciaplu": "^2.2.0",
134135
"cpu-features": "^0.0.10",
135136
"cross-env": "~7.0.2",
136137
"css-loader": "~6.5.0",

src/common/libs/langDetector.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { match } from 'ciaplu';
2+
13
function isJSON (str: string) {
24
try {
35
if (!['{', '['].includes(str.trim()[0]))
@@ -176,17 +178,13 @@ function isMD (str: string) {
176178
}
177179

178180
export function langDetector (str: string) {
179-
if (!str || !str.trim().length)
180-
return 'text';
181-
if (isJSON(str))
182-
return 'json';
183-
if (isHTML(str))
184-
return 'html';
185-
if (isSVG(str))
186-
return 'svg';
187-
if (isXML(str))
188-
return 'xml';
189-
if (isMD(str))
190-
return 'markdown';
191-
return 'text';
181+
return match(str)
182+
.when(() => !str || !str.trim().length, () => 'text')
183+
.when(isJSON, () => 'json')
184+
.when(isHTML, () => 'html')
185+
.when(isSVG, () => 'svg')
186+
.when(isXML, () => 'xml')
187+
.when(isMD, () => 'markdown')
188+
.otherwise(() => 'text')
189+
.return();
192190
}

src/common/libs/mimeFromHex.ts

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,25 @@
1+
import { match } from 'ciaplu';
2+
13
export function mimeFromHex (hex: string) {
2-
switch (hex.substring(0, 4)) { // 2 bytes
3-
case '424D':
4-
return { ext: 'bmp', mime: 'image/bmp' };
5-
case '1F8B':
6-
return { ext: 'tar.gz', mime: 'application/gzip' };
7-
case '0B77':
8-
return { ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' };
9-
case '7801':
10-
return { ext: 'dmg', mime: 'application/x-apple-diskimage' };
11-
case '4D5A':
12-
return { ext: 'exe', mime: 'application/x-msdownload' };
13-
case '1FA0':
14-
case '1F9D':
15-
return { ext: 'Z', mime: 'application/x-compress' };
16-
default:
17-
switch (hex.substring(0, 6)) { // 3 bytes
18-
case 'FFD8FF':
19-
return { ext: 'jpg', mime: 'image/jpeg' };
20-
case '4949BC':
21-
return { ext: 'jxr', mime: 'image/vnd.ms-photo' };
22-
case '425A68':
23-
return { ext: 'bz2', mime: 'application/x-bzip2' };
24-
default:
25-
switch (hex) { // 4 bites
26-
case '89504E47':
27-
return { ext: 'png', mime: 'image/png' };
28-
case '47494638':
29-
return { ext: 'gif', mime: 'image/gif' };
30-
case '25504446':
31-
return { ext: 'pdf', mime: 'application/pdf' };
32-
case '504B0304':
33-
return { ext: 'zip', mime: 'application/zip' };
34-
case '425047FB':
35-
return { ext: 'bpg', mime: 'image/bpg' };
36-
case '4D4D002A':
37-
return { ext: 'tif', mime: 'image/tiff' };
38-
case '00000100':
39-
return { ext: 'ico', mime: 'image/x-icon' };
40-
default:
41-
return { ext: '', mime: 'unknown ' + hex };
42-
}
43-
}
44-
}
4+
return match(hex.substring(0, 4)) // 2 bytes
5+
.with('424D', () => ({ ext: 'bmp', mime: 'image/bmp' }))
6+
.with('1F8B', () => ({ ext: 'tar.gz', mime: 'application/gzip' }))
7+
.with('0B77', () => ({ ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' }))
8+
.with('7801', () => ({ ext: 'dmg', mime: 'application/x-apple-diskimage' }))
9+
.with('4D5A', () => ({ ext: 'exe', mime: 'application/x-msdownload' }))
10+
.when((val) => ['1FA0', '1F9D'].includes(val), () => ({ ext: 'Z', mime: 'application/x-compress' }))
11+
.extracting(() => hex.substring(0, 6)) // 3 bytes
12+
.with('FFD8FF', () => ({ ext: 'jpg', mime: 'image/jpeg' }))
13+
.with('4949BC', () => ({ ext: 'jxr', mime: 'image/vnd.ms-photo' }))
14+
.with('425A68', () => ({ ext: 'bz2', mime: 'application/x-bzip2' }))
15+
.extracting(() => hex) // 4 bytes
16+
.with('89504E47', () => ({ ext: 'png', mime: 'image/png' }))
17+
.with('47494638', () => ({ ext: 'gif', mime: 'image/gif' }))
18+
.with('25504446', () => ({ ext: 'pdf', mime: 'application/pdf' }))
19+
.with('504B0304', () => ({ ext: 'zip', mime: 'application/zip' }))
20+
.with('425047FB', () => ({ ext: 'bpg', mime: 'image/bpg' }))
21+
.with('4D4D002A', () => ({ ext: 'tif', mime: 'image/tiff' }))
22+
.with('00000100', () => ({ ext: 'ico', mime: 'image/x-icon' }))
23+
.otherwise(() => ({ ext: '', mime: 'unknown ' + hex }))
24+
.return();
4525
}

0 commit comments

Comments
 (0)