-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathtoon.ts
More file actions
76 lines (60 loc) · 2.08 KB
/
Copy pathtoon.ts
File metadata and controls
76 lines (60 loc) · 2.08 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { FormatDefinition } from "../FormatHandler.ts";
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { encode, decode } from "@toon-format/toon";
const toonFormat = new FormatDefinition(
"Token-Oriented Object Notation",
"toon",
"toon",
"text/toon",
Category.DATA
);
class toonHandler implements FormatHandler {
public name: string = "toon";
public supportedFormats?: FileFormat[] = [
CommonFormats.JSON.supported("json", true, true, true),
toonFormat.supported("toon", true, true, true)
];
public ready: boolean = false;
async init() {
this.ready = true;
}
async doConvert(
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
switch (inputFormat.mime) {
case CommonFormats.JSON.mime:
if (outputFormat.mime !== toonFormat.mime) {
throw new TypeError(`Unsupported output format MIME: ${outputFormat.mime}`);
}
return inputFiles.map(file => {
const text = new TextDecoder().decode(file.bytes);
let jsonData = JSON.parse(text);
const toonData = encode(jsonData);
const name = file.name.split(".").slice(0, -1).join(".") + ".toon";
return {
name,
bytes: new TextEncoder().encode(toonData)
};
});
case toonFormat.mime:
if (outputFormat.mime !== CommonFormats.JSON.mime) {
throw new TypeError(`Unsupported output format MIME: ${outputFormat.mime}`);
}
return inputFiles.map(file => {
const toonData = new TextDecoder().decode(file.bytes);
const jsonData = JSON.stringify(decode(toonData));
const name = file.name.split(".").slice(0, -1).join(".") + ".json";
return {
name,
bytes: new TextEncoder().encode(jsonData)
};
});
default:
throw new TypeError(`Unsupported input format: ${inputFormat.internal}`);
}
}
}
export default toonHandler;