-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.ts
42 lines (36 loc) · 979 Bytes
/
decode.ts
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
import { Chunk } from "./extract.ts"
export interface DecodedChunk {
keyword: string
text: string
}
export const decodeChunk = (chunk: Chunk): DecodedChunk => {
let data!: Uint8Array
if (chunk.data && chunk.name) {
data = chunk.data
}
let naming = true
let text = ""
let name = ""
for (let i = 0; i < data.length; i++) {
const code = data[i]
if (naming) {
if (code) {
name += String.fromCharCode(code)
} else {
naming = false
}
} else {
if (code) {
// text += String.fromCharCode(code)
text = new TextDecoder("utf-8").decode(data.slice(i))
break
} else {
// throw new Error("Invalid NULL character found. 0x00 character is not permitted in tEXt content")
}
}
}
return {
keyword: name,
text: text,
}
}