-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (51 loc) · 1.73 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
type Chapter = { start_offset_ms: number, title: string }
type ParentChapter = Chapter & { chapters: Chapter[] }
function isParent(node: Chapter): node is ParentChapter {
return ("chapters" in node)
}
function zeroPad(num: number, places: number): string {
return String(num).padStart(places, '0')
}
function startTimestamp(c: Chapter): string {
return timestamp(c.start_offset_ms)
}
function isolateMs(time: number, seconds: number,
minutes: number, hours: number): string {
const msCovered = seconds * 1e3 + minutes * 6e4 + hours * 36e5
return zeroPad(Math.trunc(time - msCovered), 3)
}
function serializer(depth = 0) {
return (c: Chapter) => serializeChapter(c, depth)
}
function serializeParent(chapters: Chapter[], depth = 0) {
return chapters.map(serializer(depth)).join('\n')
}
function timestamp(ms: number): string {
const hours = Math.trunc(ms / (36e5))
const minutes = Math.trunc((ms / (6e4)) % 60)
const seconds = Math.trunc((ms / 1e3) % 60)
const msCovered = seconds * 1e3 + minutes * 6e4 + hours * 36e5
const remainder = zeroPad(Math.trunc(ms - msCovered), 3)
const h = zeroPad(hours, 2)
const m = zeroPad(minutes, 2)
const s = zeroPad(seconds, 2)
return `${h}:${m}:${s}.${remainder}`
}
function serializeChapter(c: Chapter, depth = 0): string {
const indent: string = (Array<string>(depth)).fill(' ').join('')
const line: string = `${startTimestamp(c)} ${indent}${c.title}`
if (isParent(c)) {
const childLines = serializeParent(c.chapters, depth + 1)
return [line, childLines].join('\n')
}
return line
}
/**
* Main function
*/
(async function main() {
await Deno.readTextFile('./chapters.json')
.then(JSON.parse)
.then(serializeParent)
.then(console.log)
})()