Skip to content

Commit da5f3e5

Browse files
committed
Improve type-checking for sound bundle
1 parent 33a6466 commit da5f3e5

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/bundles/sound/src/__tests__/sound.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ import type { Sound } from '../types';
55
describe(funcs.make_sound, () => {
66
it('Should error gracefully when duration is negative', () => {
77
expect(() => funcs.make_sound(() => 0, -1))
8-
.toThrow('Sound duration must be greater than or equal to 0');
8+
.toThrow('make_sound: Sound duration must be greater than or equal to 0');
99
});
1010

1111
it('Should not error when duration is zero', () => {
1212
expect(() => funcs.make_sound(() => 0, 0)).not.toThrow();
1313
});
14+
15+
it('Should error gracefully when wave is not a function', () => {
16+
expect(() => funcs.make_sound(true as any, 1))
17+
.toThrow('make_sound expects a wave, got true');
18+
});
1419
});
1520

1621
describe(funcs.play, () => {
1722
it('Should error gracefully when duration is negative', () => {
18-
const sound = [() => 0, -1];
19-
expect(() => funcs.play(sound as any))
23+
const sound: Sound = [() => 0, -1];
24+
expect(() => funcs.play(sound))
2025
.toThrow('play: duration of sound is negative');
2126
});
2227

@@ -30,6 +35,23 @@ describe(funcs.play, () => {
3035
});
3136
});
3237

38+
describe(funcs.play_wave, () => {
39+
it('Should error gracefully when duration is negative', () => {
40+
expect(() => funcs.play_wave(() => 0, -1))
41+
.toThrow('play_wave: Sound duration must be greater than or equal to 0');
42+
});
43+
44+
it('Should error gracefully when duration is not a number', () => {
45+
expect(() => funcs.play_wave(() => 0, true as any))
46+
.toThrow('play_wave expects a number for duration, got true');
47+
});
48+
49+
it('Should error gracefully when wave is not a function', () => {
50+
expect(() => funcs.play_wave(true as any, 0))
51+
.toThrow('play_wave expects a wave, got true');
52+
});
53+
});
54+
3355
describe(funcs.play_in_tab, () => {
3456
it('Should error gracefully when duration is negative', () => {
3557
const sound = [() => 0, -1];

src/bundles/sound/src/functions.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ function save(audioBuffer: AudioBuffer) {
132132
}, duration);
133133
}
134134

135+
/**
136+
* Throws an error if any of the provided parameters are outside of their
137+
* acceptable values.
138+
*/
139+
function validateSound(func_name: string, wave: Wave, duration: number) {
140+
if (typeof duration !== 'number') {
141+
throw new Error(`${func_name} expects a number for duration, got ${duration}`);
142+
}
143+
144+
if (duration < 0) {
145+
throw new Error(`${func_name}: Sound duration must be greater than or equal to 0`);
146+
}
147+
148+
if (typeof wave !== 'function') {
149+
throw new Error(`${func_name} expects a wave, got ${wave}`);
150+
}
151+
}
152+
135153
/**
136154
* Initialize recording by obtaining permission
137155
* to use the default device microphone
@@ -248,10 +266,7 @@ export function record_for(duration: number, buffer: number): () => Sound {
248266
* @example const s = make_sound(t => Math_sin(2 * Math_PI * 440 * t), 5);
249267
*/
250268
export function make_sound(wave: Wave, duration: number): Sound {
251-
if (duration < 0) {
252-
throw new Error('Sound duration must be greater than or equal to 0');
253-
}
254-
269+
validateSound(make_sound.name, wave, duration);
255270
return pair((t: number) => (t >= duration ? 0 : wave(t)), duration);
256271
}
257272

@@ -284,7 +299,7 @@ export function get_duration(sound: Sound): number {
284299
* @return true if x is a Sound, false otherwise
285300
* @example is_sound(make_sound(t => 0, 2)); // Returns true
286301
*/
287-
export function is_sound(x: any): x is Sound {
302+
export function is_sound(x: unknown): x is Sound {
288303
return (
289304
is_pair(x)
290305
&& typeof get_wave(x) === 'function'
@@ -301,6 +316,7 @@ export function is_sound(x: any): x is Sound {
301316
* @example play_wave(t => math_sin(t * 3000), 5);
302317
*/
303318
export function play_wave(wave: Wave, duration: number): Sound {
319+
validateSound(play_wave.name, wave, duration);
304320
return play(make_sound(wave, duration));
305321
}
306322

0 commit comments

Comments
 (0)