File size: 1,593 Bytes
1e92f2d |
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 |
import { describe, expect, it } from "vitest";
import { linearTransform } from "../number";
describe("linearTransform", () => {
it("transforms values from one range to another", () => {
const value = 5;
const result = linearTransform(value, 0, 10, 0, 100);
expect(result).toBe(50);
});
it("handles negative ranges", () => {
const value = -5;
const result = linearTransform(value, -10, 0, 0, 100);
expect(result).toBe(50);
});
it("correctly transforms the minimum input value to the minimum output value", () => {
const value = 0;
const result = linearTransform(value, 0, 10, 0, 100);
expect(result).toBe(0);
});
it("correctly transforms the maximum input value to the maximum output value", () => {
const value = 10;
const result = linearTransform(value, 0, 10, 0, 100);
expect(result).toBe(100);
});
it("transforms values outside the input range", () => {
const value = 15;
const result = linearTransform(value, 0, 10, 0, 100);
expect(result).toBe(150);
});
it("handles inverted output ranges", () => {
const value = 5;
const result = linearTransform(value, 0, 10, 100, 0);
expect(result).toBe(50);
});
it("returns NaN if input maximum equals input minimum", () => {
const value = 5;
const result = linearTransform(value, 0, 0, 0, 100);
expect(result).toBe(Number.NaN);
});
it("returns NaN if input range is zero (avoids division by zero)", () => {
const value = 5;
const result = linearTransform(value, 10, 10, 0, 100);
expect(result).toBeNaN();
});
});
|