manufacturing.lot-number@1.0.0
impl/typescript.ts
1,065 bytes · the TypeScript implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { daysFromCivil, parseIsoDate } from "./dates_add_days.ts"; ← from dates.add-days ^1.0.0 · built alongside by fune
function pad(value: number, width: number): string {
return String(value).padStart(width, "0");
}
/**
* Lot code YYDDD-LL-SSS: two-digit year, day of the year, line, sequence.
*
* The day of the year counts from the epoch-day kernel in dates.add-days, so
* leap years move every date after 28 February without a month table.
*/
export function lotNumber(productionDate: string, line: number, sequence: number): string {
if (!Number.isInteger(line) || line < 1 || line > 99) {
throw new RangeError(`line must be a whole number from 1 to 99, received ${line}`);
}
if (!Number.isInteger(sequence) || sequence < 1 || sequence > 999) {
throw new RangeError(`sequence must be a whole number from 1 to 999, received ${sequence}`);
}
const date = parseIsoDate(productionDate);
const dayOfYear = daysFromCivil(date.year, date.month, date.day) - daysFromCivil(date.year, 1, 1) + 1;
return `${pad(date.year % 100, 2)}${pad(dayOfYear, 3)}-${pad(line, 2)}-${pad(sequence, 3)}`;
}