-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.ts
128 lines (109 loc) · 3.36 KB
/
types.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
enum LoadingState {
ready,
submitting,
loading
}
// https://www.everythingfrontend.com/posts/newtype-in-typescript.html
type TimeInBuffer = { value: number; readonly __tag: unique symbol }
type TimeInSegment = { value: number; readonly __tag: unique symbol }
type TimeInMovie = { value: number; readonly __tag: unique symbol }
type PositionInSpectrogram = { value: number; readonly __tag: unique symbol }
function add<T extends { readonly __tag: symbol; value: number }>(t1: T, t2: T): T {
return lift2<T>(t1, t2, (a, b) => a + b)
}
function sub<T extends { readonly __tag: symbol; value: number }>(t1: T, t2: T): T {
return lift2<T>(t1, t2, (a, b) => a - b)
}
function addConst<T extends { readonly __tag: symbol; value: number }>(t: T, c: number): T {
return lift<T>(t, a => a + c)
}
function subConst<T extends { readonly __tag: symbol; value: number }>(t: T, c: number): T {
return lift<T>(t, a => a - c)
}
function addMin<T extends { readonly __tag: symbol; value: number }>(t1: T, t2: T, t3: T): T {
return lift3<T>(t1, t2, t3, (a, b, c) => Math.min(a + b, c))
}
function subMax<T extends { readonly __tag: symbol; value: number }>(t1: T, t2: T, t3: T): T {
return lift3<T>(t1, t2, t3, (a, b, c) => Math.max(a - b, c))
}
function to<T extends { readonly __tag: symbol; value: any } = { readonly __tag: unique symbol; value: never }>(
value: T['value']
): T {
return (value as any) as T
}
function from<T extends { readonly __tag: symbol; value: any }>(value: T): T['value'] {
return (value as any) as T['value']
}
function lift<T extends { readonly __tag: symbol; value: any }>(
value: T,
callback: (value: T['value']) => T['value']
): T {
return callback(value)
}
function lift2<T extends { readonly __tag: symbol; value: any }>(
x: T,
y: T,
callback: (x: T['value'], y: T['value']) => T['value']
): T {
return callback(x, y)
}
function lift3<T extends { readonly __tag: symbol; value: any }>(
x: T,
y: T,
z: T,
callback: (x: T['value'], y: T['value'], z: T['value']) => T['value']
): T {
return callback(x, y, z)
}
interface Annotation {
word: string
index: number
startTime?: TimeInMovie
endTime?: TimeInMovie
lastClickTimestamp?: number
id?: string | number
visuals?: Visuals
}
interface Visuals {
group: d3.Selection<SVGElement>
text: d3.Selection<SVGElement>
startLine: d3.Selection<SVGElement>
startLineHandle: d3.Selection<SVGElement>
endLine: d3.Selection<SVGElement>
endLineHandle: d3.Selection<SVGElement>
filler: d3.Selection<SVGElement>
topLine: d3.Selection<SVGElement>
}
interface Buffers {
normal: null | AudioBuffer
half: null | AudioBuffer
}
enum BufferType {
normal = 'normal',
half = 'half',
}
enum DragPosition {
start = 'startTime',
end = 'endTime',
both = 'both',
}
// This clones without the UI elements
function cloneAnnotation(a: Annotation): Annotation {
return {
startTime: a.startTime,
endTime: a.endTime,
lastClickTimestamp: a.lastClickTimestamp,
word: a.word,
index: a.index,
}
}
function isValidAnnotation(a: Annotation) {
return (
_.has(a, 'startTime') &&
!_.isUndefined(a.startTime) &&
!_.isNull(a.startTime) &&
_.has(a, 'endTime') &&
!_.isUndefined(a.endTime) &&
!_.isNull(a.endTime)
)
}