refactor(media): share image resize side grid and quality steps

This commit is contained in:
Peter Steinberger
2026-02-18 18:04:01 +00:00
parent 85ebdf88b0
commit 4e7182c4af
4 changed files with 43 additions and 14 deletions

View File

@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { buildImageResizeSideGrid, IMAGE_REDUCE_QUALITY_STEPS } from "./image-ops.js";
describe("buildImageResizeSideGrid", () => {
it("returns descending unique sides capped by maxSide", () => {
expect(buildImageResizeSideGrid(1200, 900)).toEqual([1200, 1000, 900, 800]);
});
it("keeps only positive side values", () => {
expect(buildImageResizeSideGrid(0, 0)).toEqual([]);
});
});
describe("IMAGE_REDUCE_QUALITY_STEPS", () => {
it("keeps expected quality ladder", () => {
expect([...IMAGE_REDUCE_QUALITY_STEPS]).toEqual([85, 75, 65, 55, 45, 35]);
});
});

View File

@@ -10,6 +10,15 @@ export type ImageMetadata = {
height: number;
};
export const IMAGE_REDUCE_QUALITY_STEPS = [85, 75, 65, 55, 45, 35] as const;
export function buildImageResizeSideGrid(maxSide: number, sideStart: number): number[] {
return [sideStart, 1800, 1600, 1400, 1200, 1000, 800]
.map((value) => Math.min(maxSide, value))
.filter((value, idx, arr) => value > 0 && arr.indexOf(value) === idx)
.toSorted((a, b) => b - a);
}
function isBun(): boolean {
return typeof (process.versions as { bun?: unknown }).bun === "string";
}