Skip to content

🎥 Make videos programmatically with React

make interpolation input range more strict + improve testing

Jonas Brunner63d6e50072b3
diff --git a/packages/core/src/interpolate.ts b/packages/core/src/interpolate.ts
index 783f99ce2b..5596c5be2d 100644
--- a/packages/core/src/interpolate.ts
+++ b/packages/core/src/interpolate.ts
@@ -86,9 +86,9 @@ function checkValidInputRange(arr: number[]) {
throw new Error('inputRange must have at least 2 elements');
}
for (let i = 1; i < arr.length; ++i) {
- if (!(arr[i] >= arr[i - 1])) {
+ if (!(arr[i] > arr[i - 1])) {
throw new Error(
- `inputRange must be monotonically non-decreasing but got [${arr.join(
+ `inputRange must be strictly monotonically non-decreasing but got [${arr.join(
','
)}]`
);
diff --git a/packages/core/src/test/interpolate.test.ts b/packages/core/src/test/interpolate.test.ts
index 1647412179..dd2029e5ee 100644
--- a/packages/core/src/test/interpolate.test.ts
+++ b/packages/core/src/test/interpolate.test.ts
@@ -7,6 +7,8 @@ test('Basic interpolations', () => {
expect(interpolate(Math.PI, [0, 1, 4, 9], [0, 2, 1000, -1000])).toEqual(
714.4364894275378
);
+ expect(interpolate(Infinity, [0, 1], [0, 2])).toEqual(Infinity);
+ expect(interpolate(Infinity, [0, 1], [1, 0])).toEqual(-Infinity);
});
test('Must be the same length', () => {
@@ -15,28 +17,35 @@ test('Must be the same length', () => {
}, /inputRange \(2\) and outputRange \(3\) must have the same length/);
});
-test('Test against Infinity values', () => {
- expectToThrow(() => {
- interpolate(1, [-Infinity, Infinity], [0, 2]);
- }, /inputRange must contain only finite numbers, but got \[-Infinity,Infinity\]/);
-});
-
-test('Must pass at least 2 elements', () => {
+test('Must pass at least 2 elements for input range', () => {
expectToThrow(() => {
interpolate(1, [0], [9]);
}, /inputRange must have at least 2 elements/);
});
-test('Input range must be monotonically non-decreasing', () => {
+test('Input range must be strictly monotonically non-decreasing', () => {
expectToThrow(() => {
interpolate(1, [0, 1, 0.5], [0, 2, 0.2]);
- }, /inputRange must be monotonically non-decreasing/);
+ }, /inputRange must be strictly monotonically non-decreasing/);
+ expectToThrow(() => {
+ interpolate(0.75, [0, 1, 1], [0, 2, 0]);
+ }, /inputRange must be strictly monotonically non-decreasing/);
});
-test('Output range can be monotonically decreasing', () => {
+test('Output range strictly monotonically decreasing', () => {
expect(interpolate(0.75, [0, 0.5, 1], [0, 2, 0])).toEqual(1);
});
+test('Output range monotonically decreasing', () => {
+ expect(interpolate(0.75, [0, 0.5, 1], [0, 2, 2])).toEqual(2);
+});
+
+test('Cannot have Infinity in input range', () => {
+ expectToThrow(() => {
+ interpolate(1, [-Infinity, 0], [0, 2]);
+ }, /inputRange must contain only finite numbers, but got \[-Infinity,0\]/);
+});
+
test('Cannot have Infinity in output Range', () => {
expectToThrow(
() => interpolate(1, [0, 1], [Infinity, 2]),
@@ -44,13 +53,27 @@ test('Cannot have Infinity in output Range', () => {
);
});
-test('Should throw if passing 2x infinity', () => {
+test('Should throw if passing 2x infinity input range', () => {
expectToThrow(
() => interpolate(1, [Infinity, Infinity], [0, 2]),
/inputRange must contain only finite numbers, but got \[Infinity,Infinity\]/
);
});
+test('Should throw if passing 2x infinity output range', () => {
+ expectToThrow(
+ () => interpolate(1, [0, 1], [-Infinity, Infinity]),
+ /outputRange must contain only finite numbers, but got \[-Infinity,Infinity\]/
+ );
+});
+
+test('Should throw on Infinity as third argument', () => {
+ expectToThrow(
+ () => interpolate(1, [0, 1, Infinity], [0, 2, 3]),
+ /inputRange must contain only finite numbers, but got \[0,1,Infinity\]/
+ );
+});
+
test('Should throw on Infinity as third argument', () => {
expectToThrow(
() => interpolate(1, [0, 1, Infinity], [0, 2, 3]),
@@ -90,6 +113,11 @@ test('Extrapolation identity', () => {
extrapolateRight: 'identity',
})
).toBe(1000);
+ expect(
+ interpolate(-1000, [0, 1, 2], [0, 2, 4], {
+ extrapolateLeft: 'identity',
+ })
+ ).toBe(-1000);
});
test('Clamp right test', () => {
@@ -131,6 +159,10 @@ test('Handle bad types', () => {
/input or inputRange or outputRange can not be undefined/
);
// @ts-expect-error
+ expect(() => interpolate('1', [0, 1], [1, 0])).toThrowError(
+ /Cannot interpolation an input which is not a number/
+ );
+ // @ts-expect-error
expect(() => interpolate(1, 'string', 'string')).toThrowError(
/inputRange must contain only numbers/
);