property.holding-deposit-cap@1.0.1
impl/typescript.ts
1,150 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 { type Money, money } from "./money_amount.ts"; ← from money.amount ^1.0.0 · built alongside by fune
import { type RentPeriod } from "./property_deposit_cap.ts"; ← from property.deposit-cap ^1.0.0 · built alongside by fune
// Keeps annual rent exact as a JavaScript number, with room to spare.
const MAX_MINOR = 1_000_000_000_000;
/**
* One week's rent, the statutory maximum holding deposit: the annual rent
* divided by 52, rounded down once so the deposit can never exceed the cap.
*/
export function holdingDepositCap(rent: Money, rentPeriod: RentPeriod): Money {
if (rent.currency !== "GBP") {
throw new RangeError(`rent must be in GBP, received ${rent.currency}`);
}
if (!Number.isInteger(rent.minor) || rent.minor < 0) {
throw new RangeError(`rent must not be negative, received ${rent.minor}`);
}
let annual: number;
if (rentPeriod === "weekly") annual = rent.minor * 52;
else if (rentPeriod === "monthly") annual = rent.minor * 12;
else if (rentPeriod === "annual") annual = rent.minor;
else throw new RangeError(`unknown rent period "${rentPeriod}"`);
if (annual > MAX_MINOR) {
throw new RangeError(`annual rent must be at most ${MAX_MINOR} minor units`);
}
return money(Math.floor(annual / 52), "GBP");
}