diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 00000000..234a435b --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,5 @@ +## Summary + +- + +## Closes # diff --git a/src/lib/math.test.ts b/src/lib/math.test.ts new file mode 100644 index 00000000..acc851ab --- /dev/null +++ b/src/lib/math.test.ts @@ -0,0 +1,30 @@ +import { describe, it, expect } from "vitest" +import { clamp } from "./math" + +describe("clamp", () => { + it("should return value when within range", () => { + expect(clamp(5, 0, 10)).toBe(5) + }) + + it("should return min when value is below range", () => { + expect(clamp(-1, 0, 10)).toBe(0) + }) + + it("should return max when value is above range", () => { + expect(clamp(15, 0, 10)).toBe(10) + }) + + it("should return min when value equals min", () => { + expect(clamp(0, 0, 10)).toBe(0) + }) + + it("should return max when value equals max", () => { + expect(clamp(10, 0, 10)).toBe(10) + }) + + it("should work with negative ranges", () => { + expect(clamp(-5, -10, -1)).toBe(-5) + expect(clamp(-15, -10, -1)).toBe(-10) + expect(clamp(0, -10, -1)).toBe(-1) + }) +}) diff --git a/src/lib/math.ts b/src/lib/math.ts new file mode 100644 index 00000000..c0f94dd5 --- /dev/null +++ b/src/lib/math.ts @@ -0,0 +1,9 @@ +export function clamp(val: number, min: number, max: number): number { + if (val < min) { + return min + } + if (val > max) { + return max + } + return val +} diff --git a/src/store/theme.ts b/src/store/theme.ts index 69b5384f..3b40d10c 100644 --- a/src/store/theme.ts +++ b/src/store/theme.ts @@ -1,4 +1,5 @@ import { proxy, subscribe } from "valtio" +import { clamp } from "@/lib/math" export type ThemeName = "default" | "contrast" export type ColorSchemeVariant = "black-on-white" | "white-on-black" | "blue-on-blue" | "brown-on-yellow" | "green-on-brown" @@ -8,16 +9,6 @@ const MIN_FONT_SIZE = 8 const MAX_FONT_SIZE = 48 const CHANGE_FONT_SIZE_DELTA = 1 -function clamp(val: number, min: number, max: number): number { - if (val < min) { - return min - } - if (val > max) { - return max - } - return val -} - export type ThemeOptions = { theme: ThemeName fontType: FontTypeVariant