moon-cycle/src/helpers.ts
Aric Camarata 9b9abb99c8 refactor: code quality improvements across the board
- Add input validation (TypeError) to cycleMonth and cycleYear
- Convert tests to node:test runner with describe/it structure
- Add ESLint + Prettier with lint, format, and format:check scripts
- Update CI workflow with lint job, frozen-lockfile, and pack-check
- Add noImplicitReturns, noFallthroughCasesInSwitch, skipLibCheck to tsconfig
- Add invalid Date tests for both ESM and CJS suites
2026-03-08 11:32:47 -04:00

45 lines
1.5 KiB
TypeScript

import { ImageSet, ImageSize, ImageQuality } from './types.js';
/**
* Returns the folder name for a given image set, size, and quality.
*
* Folder names follow the pattern `{set}-{size}-{quality}`, matching the
* directory layout in the moon-cycle repository.
*
* @example
* imageFolder('mm', 256, 75) // => 'mm-256-75'
* imageFolder('my', 512, 85) // => 'my-512-85'
*/
export function imageFolder(set: ImageSet, size: ImageSize, quality: ImageQuality): string {
return `${set}-${size}-${quality}`;
}
/**
* Returns a jsDelivr CDN URL for a specific moon image.
*
* jsDelivr serves files directly from GitHub repositories, making it a
* practical option for web applications that need the images without
* bundling ~438 MB of assets locally.
*
* @param filename - Filename returned by `cycleMonth` or `cycleYear`, e.g. `"354.webp"`.
* @param set - Image set: `'mm'` (monthly) or `'my'` (yearly).
* @param size - Image dimension: `256` or `512`.
* @param quality - WebP quality: `75` or `85`.
* @param ref - Git ref (branch, tag, or commit SHA). Defaults to `'main'`.
* @returns A full CDN URL string.
*
* @example
* const file = cycleMonth();
* const url = cdnUrl(file, 'mm', 256, 75);
* // => 'https://cdn.jsdelivr.net/gh/acamarata/moon-cycle@main/mm-256-75/354.webp'
*/
export function cdnUrl(
filename: string,
set: ImageSet,
size: ImageSize,
quality: ImageQuality,
ref: string = 'main',
): string {
const folder = imageFolder(set, size, quality);
return `https://cdn.jsdelivr.net/gh/acamarata/moon-cycle@${ref}/${folder}/${filename}`;
}