Move clamp function to lib directory (#250)
* clamp refactoring * pr template
This commit is contained in:
1 parent
562b4f6408
commit
f4c5132d7c
4 files changed
+45
-10
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
## Summary
|
||||||
|
|
||||||
|
-
|
||||||
|
|
||||||
|
## Closes #
|
||||||
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+1
-10
@@ -1,4 +1,5 @@
|
|||||||
import { proxy, subscribe } from "valtio"
|
import { proxy, subscribe } from "valtio"
|
||||||
|
import { clamp } from "@/lib/math"
|
||||||
|
|
||||||
export type ThemeName = "default" | "contrast"
|
export type ThemeName = "default" | "contrast"
|
||||||
export type ColorSchemeVariant = "black-on-white" | "white-on-black" | "blue-on-blue" | "brown-on-yellow" | "green-on-brown"
|
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 MAX_FONT_SIZE = 48
|
||||||
const CHANGE_FONT_SIZE_DELTA = 1
|
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 = {
|
export type ThemeOptions = {
|
||||||
theme: ThemeName
|
theme: ThemeName
|
||||||
fontType: FontTypeVariant
|
fontType: FontTypeVariant
|
||||||
|
|||||||
Reference in new issue
Block a user