Skip to content

🎥 Make videos programmatically with React

validate parametes

Jonny Burgerca4c4b5cb745
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.ts
index 5c42f9c188..83b5b3a638 100644
--- a/packages/core/src/spring/index.ts
+++ b/packages/core/src/spring/index.ts
@@ -1,3 +1,4 @@
+import {validateSpringDuration} from '../validation/validation-spring-duration';
import {measureSpring} from './measure-spring';
import type {SpringConfig} from './spring-utils';
import {springCalculation} from './spring-utils';
@@ -14,6 +15,8 @@ import {springCalculation} from './spring-utils';
* @param {boolean} [config.overshootClamping=false] Whether to prevent the animation going beyond the target value.
* @param {?number} [config.from] The initial value of the animation. Default `0`
* @param {?number} [config.to] The end value of the animation. Default `1`
+ * @param {?number} [config.duration] Stretch the duration of an animation to a set value.. Default `undefined`
+ * @param {?number} [config.durationThreshold] How close to the end the animation is considered to be done. Default `0.005`
*/
export function spring({
frame,
@@ -32,6 +35,7 @@ export function spring({
duration?: number;
durationThreshold?: number;
}): number {
+ validateSpringDuration(duration);
const durationRatio = duration
? duration /
measureSpring({fps, config, from, to, threshold: durationThreshold})
diff --git a/packages/core/src/validation/validation-spring-duration.ts b/packages/core/src/validation/validation-spring-duration.ts
new file mode 100644
index 0000000000..093a94414f
--- /dev/null
+++ b/packages/core/src/validation/validation-spring-duration.ts
@@ -0,0 +1,29 @@
+export const validateSpringDuration = (dur: unknown) => {
+ if (typeof dur === 'undefined') {
+ return;
+ }
+
+ if (typeof dur !== 'number') {
+ throw new TypeError(
+ `A "duration" of a spring must be a "number" but is "${typeof dur}"`
+ );
+ }
+
+ if (Number.isNaN(dur)) {
+ throw new TypeError(
+ 'A "duration" of a spring is NaN, which it must not be'
+ );
+ }
+
+ if (!Number.isFinite(dur)) {
+ throw new TypeError(
+ 'A "duration" of a spring must be finite, but is ' + dur
+ );
+ }
+
+ if (dur <= 0) {
+ throw new TypeError(
+ 'A "duration" of a spring must be positive, but is ' + dur
+ );
+ }
+};