-
Notifications
You must be signed in to change notification settings - Fork 6
/
Encoder.ts
184 lines (177 loc) · 5.54 KB
/
Encoder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @file Encoder combinators. */
import {checkLength} from "./helpers";
/**
* Describes a state object encoder that outputs into an array.
*
* @template TState State object type.
*/
export interface Encoder<TState> {
/**
* Encoder function.
*
* @param data Array to fill with the encoded data. Length should be at
* least {@link size}.
* @param args Data to encode.
*/
readonly encode: (data: Float32Array, args: TState) => void;
/**
* Minimum size of the data input array that's required for the
* {@link encode} method.
*/
readonly size: number;
}
/**
* Creates a no-op Encoder in order to make an assertion.
*
* @param assertion Assertion function to call. This function may throw if a
* precondition about its input was violated.
*/
export function assertEncoder<TState>(
assertion: (state: TState) => void,
): Encoder<TState> {
return {
encode(arr, state) {
checkLength(arr, 0);
assertion(state);
},
size: 0,
};
}
/**
* Augments an encoder to become compatible with a different input type.
*
* @param getter Function to transform the new input type into the old one.
* @param encoder Encoder that works with the old input type.
* @returns An Encoder that takes the new input type.
*/
export function augment<TState1, TState2>(
getter: (args: TState1) => TState2,
encoder: Encoder<TState2>,
): Encoder<TState1> {
return {
encode(arr, args) {
encoder.encode(arr, getter(args));
},
size: encoder.size,
};
}
/**
* Creates an Encoder that concatenates the results of each of the given
* Encoders using the same original input state.
*
* Since all the results are concatenated, the returned Encoder will require an
* array that spans the sum of each Encoder's required array size.
*/
export function concat<TState>(
...encoders: Encoder<TState>[]
): Encoder<TState> {
return {
encode(arr, args) {
checkLength(arr, this.size);
let nextByteOffset = arr.byteOffset;
const maxByteOffset = arr.byteLength + arr.byteOffset;
for (const encoder of encoders) {
const byteOffset = nextByteOffset;
nextByteOffset += encoder.size * arr.BYTES_PER_ELEMENT;
if (nextByteOffset > maxByteOffset) {
throw new Error(
"concat() encoder array was too small for the given " +
`encoders (${arr.byteLength} bytes vs at least ` +
`${nextByteOffset - arr.byteOffset})`,
);
}
const a = new Float32Array(
arr.buffer,
byteOffset,
encoder.size,
);
encoder.encode(a, args);
}
if (nextByteOffset !== maxByteOffset) {
throw new Error(
"concat() encoder didn't fill the given array (filled " +
`${nextByteOffset - arr.byteOffset} bytes, given ` +
`${arr.byteLength})`,
);
}
},
size: encoders.reduce((a, b) => a + b.size, 0),
};
}
/**
* Creates an Encoder that maps over a collection of states.
*
* @param length Number of state objects to encode.
* @param encoder Encoder to use for each state object.
*/
export function map<TState>(
length: number,
encoder: Encoder<TState>,
): Encoder<Readonly<ArrayLike<TState>>> {
return concat(
assertEncoder(states => checkLength(states, length)),
...Array.from({length}, (_, i) =>
augment((states: ArrayLike<TState>) => states[i], encoder),
),
);
}
/**
* Creates an encoder that defaults to another if the state input is
* `undefined`.
*
* @param encoder Encoder when the state is defined.
* @param alt Encoder when the state input is `undefined`.
*/
export function nullable<TState>(
encoder: Encoder<TState>,
alt: Encoder<undefined>,
): Encoder<TState | undefined> {
if (encoder.size !== alt.size) {
throw new Error(
"nullable() encoder arguments do not have the same size " +
`(${encoder.size} vs ${alt.size})`,
);
}
return {
encode(arr, state) {
if (state === undefined) {
alt.encode(arr, undefined);
} else {
encoder.encode(arr, state);
}
},
size: encoder.size,
};
}
/**
* Creates an Encoder that selects one of three given Encoders depending on
* whether the input is defined, `null`, or `undefined`.
*
* @param defined Encoder when the input is defined.
* @param unknown Encoder when the input is `null`.
* @param empty Encoder when the input is `undefined`.
*/
export function optional<TState>(
defined: Encoder<TState>,
unknown: Encoder<null>,
empty: Encoder<undefined>,
): Encoder<TState | null | undefined> {
if (defined.size !== unknown.size || defined.size !== empty.size) {
throw new Error(
"optional() encoder arguments do not have the same size " +
`(${defined.size} vs ${unknown.size} vs ${empty.size})`,
);
}
return {
encode(arr, state) {
if (state === undefined) {
empty.encode(arr, undefined);
} else if (state === null) {
unknown.encode(arr, null);
} else {
defined.encode(arr, state);
}
},
size: defined.size,
};
}