Functional Weave
Code in TypeScript

lending.ltv

Loan-to-value ratio in basis points, and the band it falls in from a band table you supply.

1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra

Pinned by 17 tests, run in TypeScript, Python and Rust.

Not professional advice. This capability calculates lending figures from published rules. It is a software component for developers, not financial advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed in its README, and have a consumer-credit compliance specialist review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.

What it does

Loan-to-value: the loan as a share of the property's value, in basis points (7500 = 75%), and which band of the lender's table it falls in. Mortgage pricing is set by LTV band, so the band is what a rate lookup needs.

## Bands are yours, and inclusive

For example

  • loanToValue(£225,000.00, £300,000.00, bands ×5) → rate 75%, band 75% LTV £225,000 on a £300,000 house is exactly 75%
  • loanToValue(£225,000.01, £300,000.00, bands ×5) → rate 75.01%, band 85% LTV a penny over 75% is the 85% band
  • loanToValue(£180,000.00, £300,000.00, bands ×5) → rate 60%, band 60% LTV £180,000 on £300,000 is exactly 60%

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

export function loanToValue(loan: Money, propertyValue: Money, bands: readonly LtvBand[]): LtvResult
loanMoneythe amount borrowed or outstanding, zero or more
propertyValueMoneythe valuation or price, whichever is lower by the lender's policy; greater than zero
bandsLtvBand[]ceilings in strictly ascending order; a loan belongs to the first band it fits under
returnsLtvResult

The types it declares, generated into your project

/** One row of a lender's LTV table. */
export interface LtvBand {
  /** the band's ceiling, inclusive: 7500 = up to and including 75% */
  readonly maxBasisPoints: number;
  /** e.g. "75% LTV" */
  readonly label: string;
}

/** The ratio and its band. */
export interface LtvResult {
  /** loan / value, rounded up to the next basis point */
  readonly basisPoints: number;
  /** the first band whose ceiling the exact ratio is within; null above the top band */
  readonly band: string | null;
}

Your code names it in one line, in the file that uses it

import { loanToValue } from "#fune/lending.ltv@^1";
impl/typescript.ts · 43 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { roundDiv } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type Money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { type LtvBand, type LtvResult } from "./lending_ltv_types.ts";

/** Above this, loan × 10000 is no longer exact as a JavaScript number. */
const MAX_LOAN = 900719925474;

/**
 * Loan-to-value, and the band from the lender's table. The band is chosen on
 * the exact ratio, loan × 10000 <= ceiling × value, never on a rounded
 * percentage, so 75.004% is not "75%". The reported basis points are rounded
 * up, which agrees: a whole ceiling is met exactly when the rounded-up ratio
 * is within it.
 */
export function loanToValue(loan: Money, propertyValue: Money, bands: readonly LtvBand[]): LtvResult {
  if (loan.currency !== propertyValue.currency) {
    throw new RangeError(`currency mismatch: ${loan.currency} and ${propertyValue.currency}`);
  }
  if (!Number.isInteger(propertyValue.minor) || propertyValue.minor <= 0) {
    throw new RangeError(`propertyValue must be greater than zero, received ${propertyValue.minor}`);
  }
  if (!Number.isInteger(loan.minor) || loan.minor < 0) {
    throw new RangeError(`loan must not be negative, received ${loan.minor}`);
  }
  if (loan.minor > MAX_LOAN) {
    throw new RangeError(`loan must be at most ${MAX_LOAN} minor units`);
  }
  if (bands.length === 0) {
    throw new RangeError("bands must not be empty");
  }
  for (let i = 0; i < bands.length; i += 1) {
    const max = bands[i].maxBasisPoints;
    if (!Number.isInteger(max) || max < 1) {
      throw new RangeError(`band ceilings must be whole basis points of 1 or more, received ${max}`);
    }
    if (i > 0 && max <= bands[i - 1].maxBasisPoints) {
      throw new RangeError("band ceilings must be in strictly ascending order");
    }
  }
  const basisPoints = roundDiv(loan.minor * 10000, propertyValue.minor, "up");
  const found = bands.find((band) => basisPoints <= band.maxBasisPoints);
  return { basisPoints, band: found === undefined ? null : found.label };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add lending.ltv
Download for TypeScript lending.ltv-1.0.0-typescript.fune · 16,145 bytes sha256 51fcd2461dd2b1a0fb3f680a6dd356cbdefd014c72c71629f44da24fd646aae5

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./lending.ltv-1.0.0-typescript.fune, or fetch it from a terminal with fune pull lending.ltv@1.0.0:typescript.

The whole function, every language, is one file too: lending.ltv-1.0.0.fune, 21,068 bytes, sha256 cb7c32d332a34dba94a79558c5d054979d4706d697ed194156541da2a1c07e3f. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before lending.ltv

after — your function gets the result and the arguments, and returns the final result.

// fune: after lending.ltv

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace math.round-div in lending.ltv
// fune: replace money.amount in lending.ltv

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show lending.ltv --steps.

// fune: step lending.ltv after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
£225,000 on a £300,000 house is exactly 75% £225,000.00, £300,000.00, bands ×5 → rate 75%, band 75% LTV
a penny over 75% is the 85% band £225,000.01, £300,000.00, bands ×5 → rate 75.01%, band 85% LTV
£180,000 on £300,000 is exactly 60% £180,000.00, £300,000.00, bands ×5 → rate 60%, band 60% LTV
a small loan is in the lowest band £50,000.00, £400,000.00, bands ×5 → rate 12.5%, band 60% LTV
a ratio with a fraction of a basis point rounds up £100,000.00, £300,000.00, bands ×5 → rate 33.34%, band 60% LTV
95% exactly £285,000.00, £300,000.00, bands ×5 → rate 95%, band 95% LTV
above the top band has no band £290,000.00, £300,000.00, bands ×5 → rate 96.67%, band —
negative equity: more than 100% £330,000.00, £300,000.00, bands ×5 → rate 110%, band —
a zero loan is 0% £0.00, £300,000.00, bands ×5 → rate 0%, band 60% LTV
one band table £90,000.00, £100,000.00, bands ×1 → rate 90%, band all
Show the other 7 tests
CaseArgumentsExpected
a zero value is refused £1.00, £0.00, bands ×5 → error: propertyValue must be greater than zero
a negative loan is refused -£0.01, £1.00, bands ×5 → error: loan must not be negative
an empty band table is refused £1.00, £10.00, → error: bands must not be empty
bands out of order are refused £1.00, £10.00, bands ×2 → error: strictly ascending
a zero ceiling is refused £1.00, £10.00, bands ×1 → error: band ceilings must be whole basis points
mixed currencies are refused £1.00, €10.00, bands ×5 → error: currency mismatch
a loan beyond exact arithmetic is refused £9,007,199,254.75, £10,000,000,000.00, bands ×5 → error: loan must be at most

More from the author

The band table is an argument, because every lender's differs. Each band is a ceiling, inclusive: a band of 7500 holds everything up to and including 75%. Bands must be in strictly ascending order, and the loan goes in the first band whose ceiling it is within. Above the top band, `band` is null: the loan is outside the table (for example negative equity above 100%), which the caller must handle rather than silently getting the top band.

## Exact at the boundary

The band is decided on the exact ratio, never on a rounded percentage: £225,000 on £300,000 is exactly 75% and is in the 75% band; one penny more is 75.0000033% and is not. The reported `basisPoints` is rounded up (math.round-div "up"), so it never understates the ratio, and it always agrees with the band: a whole ceiling holds the exact ratio exactly when it holds the rounded-up one.

## What it does not do

It does not decide which value to use (valuation or purchase price, usually the lower) or add fees to the loan; pass the figures your policy uses. A zero loan is 0%. The value must be positive, and the loan at most about £9 billion in pence (900,719,925,474 minor units), where loan × 10000 stays exact in every language.

Files

PathBytes
README.md1,477
impl/python.py1,965
impl/rust.rs2,770
impl/typescript.ts1,992
vectors.json8,641