impl/typescript.ts
1,250 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 { addDays } from "./dates_add_days.ts"; ← from dates.add-days ^1.0.0 · built alongside by fune
import { BANK_RATE, BANK_RATE_HISTORY } from "./finance_uk_bank_rate_data.ts"; ← this capability’s own data, compiled from data/bank-rate.json into the same file by fune build
import { type BankRate } from "./finance_uk_bank_rate_types.ts";
/** Bank Rate in force on a date: the latest change on or before it. */
export function ukBankRate(onDate: string): BankRate {
addDays(onDate, 0); // validates the date; ISO dates then compare as strings
for (const row of BANK_RATE) {
if (onDate >= row.validFrom && (row.validTo === null || onDate <= row.validTo)) {
return { basisPoints: row.basisPoints, validFrom: row.validFrom, validTo: row.validTo };
}
}
// A history=current build keeps only the rate in force; answering an older
// date with it would be exactly the wrong answer this data exists to prevent.
if (BANK_RATE_HISTORY !== "full" && BANK_RATE.length > 0) {
const earliest = BANK_RATE.map((r) => r.validFrom).reduce((a, b) => (b < a ? b : a));
if (onDate < earliest) {
throw new RangeError(
`no Bank Rate on ${onDate}: this build was installed with history=${BANK_RATE_HISTORY}, so it only carries rates from ${earliest}. Reinstall with history=full for older dates.`,
);
}
}
throw new RangeError(`no Bank Rate on ${onDate}`);
}