Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

API: option ranges, and a clamp that falls back to the default

Lucas Marques8d985f21ab9a
diff --git a/packages/hairline/src/ranges.ts b/packages/hairline/src/ranges.ts
new file mode 100644
index 0000000..a06769b
--- /dev/null
+++ b/packages/hairline/src/ranges.ts
@@ -0,0 +1,29 @@
+/** One numeric option: its bounds, the step a slider should move in, its default and its unit. */
+export type Range = {
+ readonly min: number;
+ readonly max: number;
+ readonly step: number;
+ readonly default: number;
+ readonly unit: string;
+};
+
+/**
+ * Every numeric option of every figure. A figure clamps what it is given to
+ * `min`…`max` and falls back to `default`; `step` and `unit` are for whoever
+ * builds a slider or a table from this.
+ */
+export const ranges = {
+ riffle: { stagger: { min: 0, max: 90, step: 5, default: 40, unit: "ms" } },
+ terrain: { radius: { min: 1.5, max: 5, step: 0.25, default: 3, unit: "cells" } },
+ exploded: { gap: { min: 12, max: 40, step: 1, default: 28, unit: "u" } },
+ phosphor: { afterglow: { min: 150, max: 1500, step: 10, default: 520, unit: "ms" } },
+ slow: { rate: { min: 0.05, max: 0.6, step: 0.05, default: 0.2, unit: "×" } },
+ turntable: { coast: { min: 200, max: 1500, step: 50, default: 650, unit: "ms" } },
+} as const satisfies Record<string, Record<string, Range>>;
+
+/** A number from anything: numeric strings are read, what is not a finite number becomes the default, the rest is clamped. */
+export function number(value: unknown, range: Range): number {
+ const n = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
+ if (typeof n !== "number" || !Number.isFinite(n)) return range.default;
+ return Math.min(range.max, Math.max(range.min, n));
+}
diff --git a/packages/hairline/test/ranges.test.ts b/packages/hairline/test/ranges.test.ts
new file mode 100644
index 0000000..3014690
--- /dev/null
+++ b/packages/hairline/test/ranges.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from "vitest";
+import { number, ranges } from "../src/ranges";
+
+const r = ranges.riffle.stagger; // 0 to 90, default 40
+
+describe("number", () => {
+ it("keeps a number inside the range", () => {
+ expect(number(60, r)).toBe(60);
+ });
+ it("clamps to the range", () => {
+ expect(number(-5, r)).toBe(0);
+ expect(number(500, r)).toBe(90);
+ });
+ it("reads a numeric string, as an attribute or a form field would give it", () => {
+ expect(number("60", r)).toBe(60);
+ expect(number(" 500 ", r)).toBe(90);
+ });
+ it.each([undefined, null, NaN, Infinity, -Infinity, "", " ", "fast", {}, [], true])("falls back to the default for %o", (v) => {
+ expect(number(v, r)).toBe(40);
+ });
+});
+
+describe("ranges", () => {
+ it("has a default inside every range and a positive step", () => {
+ for (const figure of Object.values(ranges)) {
+ for (const range of Object.values(figure) as Array<(typeof ranges)["riffle"]["stagger"]>) {
+ expect(range.min).toBeLessThan(range.max);
+ expect(range.default).toBeGreaterThanOrEqual(range.min);
+ expect(range.default).toBeLessThanOrEqual(range.max);
+ expect(range.step).toBeGreaterThan(0);
+ }
+ }
+ });
+});