Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { PRICE_CAP, PRICE_CAP_HISTORY } from "./energy_price_cap_data.ts"; ← this capability’s own data, compiled from data/price-cap.json into the same file by fune build
import { type EnergyFuel, type PaymentMethod, type PriceCap } from "./energy_price_cap_types.ts";
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
const FUELS = ["electricity", "gas"];
const METHODS = ["direct-debit", "standard-credit", "prepayment"];
/** The Ofgem price cap unit rate and standing charge in force on `onDate`. */
export function priceCap(fuel: EnergyFuel, paymentMethod: PaymentMethod, onDate: string): PriceCap {
if (!ISO_DATE.test(onDate)) {
throw new RangeError(`onDate must be an ISO date (YYYY-MM-DD), received "${onDate}"`);
}
if (!FUELS.includes(fuel)) {
throw new RangeError(`unknown fuel "${fuel}": expected electricity or gas`);
}
if (!METHODS.includes(paymentMethod)) {
throw new RangeError(`unknown payment method "${paymentMethod}": expected direct-debit, standard-credit or prepayment`);
}
const rows = PRICE_CAP.filter((r) => r.fuel === fuel && r.paymentMethod === paymentMethod);
if (rows.length === 0) {
throw new RangeError(`no price cap figures for ${fuel} paid by ${paymentMethod}: this version carries GB-average Direct Debit figures only`);
}
const row = rows.find((r) => r.validFrom <= onDate && onDate <= r.validTo);
if (row === undefined) {
// A pruned build must not answer an old date with a newer cap.
if (PRICE_CAP_HISTORY !== "full") {
const earliest = rows.reduce((e, r) => (r.validFrom < e ? r.validFrom : e), rows[0].validFrom);
if (onDate < earliest) {
throw new RangeError(
`no price cap for ${fuel}/${paymentMethod} on ${onDate}: this build was installed with history=${PRICE_CAP_HISTORY}, ` +
`so it only carries periods from ${earliest}. Reinstall with history=full for older dates.`
);
}
}
throw new RangeError(`no price cap for ${fuel}/${paymentMethod} on ${onDate}`);
}
return {
fuel,
paymentMethod,
region: row.region,
unitRate: row.unitRate,
standingCharge: row.standingCharge,
vatBasisPoints: row.vatBasisPoints,
validFrom: row.validFrom,
validTo: row.validTo,
};
}