property.service-charge-apportion
Apportion a service charge across units by lease fraction, floor area or fixed percentage, exact to the penny.
1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra
Pinned by 19 tests, run in TypeScript, Python and Rust.
Not professional advice. This capability calculates property figures from published rules. It is a software component for developers, not legal or 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 conveyancer or tax adviser review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.
What it does
Shares a building's service charge out between its units the way their leases say, and makes the pennies add up: the lines always total the service charge exactly, never a penny over or under.
Leases state a unit's share in one of three ways, chosen by `basis`:
For example
apportionServiceCharge(£10,000.00, units ×3, fraction)→ ×3 £10,000 in thirds: the odd penny goes to the first flatapportionServiceCharge(£100.01, units ×3, fraction)→ ×3 1/3, 1/6, 1/2 of £100.01: £33.34, £16.67, £50.00, not £100.02 by separate roundingapportionServiceCharge(£100.00, units ×3, fraction)→ ×3 a half and two quarters
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 apportionServiceCharge(total: Money, units: readonly ServiceChargeUnit[], basis: ApportionBasis): readonly ServiceChargeLine[]
| total | Money | the service charge to share out; negative to share out a surplus or credit |
| units | ServiceChargeUnit[] | every unit that pays, in the order the lines come back |
| basis | ApportionBasis | how each unit's share is written |
| returns | ServiceChargeLine[] | one line per unit, in the same order; the charges add up to total exactly |
The types it declares, generated into your project
export type ApportionBasis = "fraction" | "floor-area" | "percentage";
/** One unit and its share, as its lease states it. */
export interface ServiceChargeUnit {
/** the flat or unit's name */
readonly unit: string;
/** fraction: the numerator; floor-area: the area in any one integer unit; percentage: basis points */
readonly share: number;
/** fraction: the denominator; null for floor-area and percentage */
readonly of: number | null;
}
/** What one unit pays. */
export interface ServiceChargeLine {
readonly unit: string;
readonly charge: Money;
}
Your code names it in one line, in the file that uses it
import { apportionServiceCharge } from "#fune/property.service-charge-apportion@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { lcm } from "./math_gcd_lcm.ts"; ← from math.gcd-lcm ^1.0.0 · built alongside by fune
import { allocate } from "./money_allocate.ts"; ← from money.allocate ^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 ApportionBasis, type ServiceChargeLine, type ServiceChargeUnit } from "./property_service_charge_apportion_types.ts";
/**
* Share a service charge between units by lease fraction, floor area or fixed
* percentage. Every share becomes an integer weight, and money.allocate does
* the one split, so the lines always add up to the total exactly.
*/
export function apportionServiceCharge(
total: Money,
units: readonly ServiceChargeUnit[],
basis: ApportionBasis,
): readonly ServiceChargeLine[] {
if (basis !== "fraction" && basis !== "floor-area" && basis !== "percentage") {
throw new RangeError(`unknown basis "${basis}": use fraction, floor-area or percentage`);
}
if (units.length === 0) {
throw new RangeError("units must not be empty");
}
for (const u of units) {
if (!Number.isSafeInteger(u.share) || u.share < 0) {
throw new RangeError(`unit "${u.unit}": share must be a whole number, zero or more, received ${u.share}`);
}
if (basis === "fraction") {
if (u.of === null || !Number.isSafeInteger(u.of) || u.of <= 0) {
throw new RangeError(`unit "${u.unit}": a fraction needs a denominator "of" greater than zero`);
}
} else if (u.of !== null) {
throw new RangeError(`unit "${u.unit}": "of" is only for the fraction basis`);
}
}
let weights: number[];
if (basis === "fraction") {
let common = 1;
for (const u of units) common = lcm(common, u.of as number);
weights = units.map((u) => u.share * (common / (u.of as number)));
const sum = weights.reduce((a, b) => a + b, 0);
if (!Number.isSafeInteger(sum)) {
throw new RangeError("the fractions are too fine to apportion exactly");
}
if (sum !== common) {
throw new RangeError(`the fractions add up to ${sum}/${common}, not exactly 1`);
}
} else {
weights = units.map((u) => u.share);
const sum = weights.reduce((a, b) => a + b, 0);
if (basis === "percentage" && sum !== 10000) {
throw new RangeError(`the percentages add up to ${sum} basis points, not 10000`);
}
if (sum === 0) {
throw new RangeError("the floor areas add up to zero");
}
}
const sum = weights.reduce((a, b) => a + b, 0);
if (!Number.isSafeInteger(sum) || !Number.isSafeInteger(Math.abs(total.minor) * sum)) {
throw new RangeError("the fractions are too fine to apportion exactly");
}
const parts = allocate(total, weights);
return units.map((u, i) => ({ unit: u.unit, charge: parts[i] }));
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 3 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 property.service-charge-apportion
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./property.service-charge-apportion-1.0.0-typescript.fune, or fetch it from a terminal with fune pull property.service-charge-apportion@1.0.0:typescript.
The whole function, every language, is one file too: property.service-charge-apportion-1.0.0.fune, 25,609 bytes, sha256 d3707eea6034faf42e1f93d8e5f7f70fb9c2b3309aa32d52fce942729d6f5c39. 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 property.service-charge-apportion
after — your function gets the result and the arguments, and returns the final result.
// fune: after property.service-charge-apportion
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.gcd-lcm in property.service-charge-apportion
// fune: replace money.allocate in property.service-charge-apportion
// fune: replace money.amount in property.service-charge-apportion
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 property.service-charge-apportion --steps.
// fune: step property.service-charge-apportion 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.
| Case | Arguments | Expected | |
|---|---|---|---|
| £10,000 in thirds: the odd penny goes to the first flat | £10,000.00, units ×3, fraction | → | ×3 |
| 1/3, 1/6, 1/2 of £100.01: £33.34, £16.67, £50.00, not £100.02 by separate rounding | £100.01, units ×3, fraction | → | ×3 |
| a half and two quarters | £100.00, units ×3, fraction | → | ×3 |
| twentieths and quarters mixed: 3/20, 7/20, 1/4, 1/4 of £4,000 | £4,000.00, units ×4, fraction | → | ×4 |
| floor areas 50, 70 and 80 m² share £1,000 | £1,000.00, units ×3, floor-area | → | ×3 |
| areas in hundredths of a m²: the spare penny goes to the largest remainder | £2,345.67, units ×3, floor-area | → | ×3 |
| fixed percentages 33.33%, 33.33%, 33.34% of £100 | £100.00, units ×3, percentage | → | ×3 |
| a unit with a zero share pays nothing but keeps its line | £500.00, units ×2, floor-area | → | ×2 |
| a surplus is shared out the same way | -£100.00, units ×3, fraction | → | ×3 |
| one unit takes it all | £1,234.56, units ×1, fraction | → | ×1 |
Show the other 9 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| fractions that do not make a whole are refused | £100.00, units ×2, fraction | → | error: the fractions add up to 2/3, not exactly 1 |
| fractions over a whole are refused | £100.00, units ×2, fraction | → | error: the fractions add up to 7/6, not exactly 1 |
| percentages short of 100% are refused | £100.00, units ×2, percentage | → | error: the percentages add up to 9999 basis points, not 10000 |
| a fraction without a denominator is refused | £100.00, units ×2, fraction | → | error: unit "A": a fraction needs a denominator |
| a denominator on a floor area is refused | £100.00, units ×1, floor-area | → | error: unit "A": "of" is only for the fraction basis |
| a negative share is refused | £100.00, units ×2, floor-area | → | error: unit "A": share must be a whole number |
| no units is refused | £100.00, , floor-area | → | error: units must not be empty |
| all-zero floor areas are refused | £100.00, units ×2, floor-area | → | error: the floor areas add up to zero |
| an unknown basis is refused | £100.00, units ×1, rateable-value | → | error: unknown basis |
More from the author
- **fraction**: "one-third", "3/20ths". Each unit gives `share` over `of`. The fractions must add up to exactly 1, since a lease scheme that does not recover the whole cost (or recovers more) is something to raise with the landlord, not to paper over. - **floor-area**: each unit's area, in any one integer unit (square feet, square metres, or square metres × 100 for 0.01 m² precision). The share is the unit's area over the total area. - **percentage**: basis points (2500 = 25%), adding up to exactly 10,000.
## Exact to the penny
Each unit's exact share is total × share, and the pennies left over after rounding every share down are given one at a time to the units with the largest remainders (ties to the earlier unit), by `money.allocate`. So a £100.01 charge split 1/3, 1/6, 1/2 is £33.34, £16.67 and £50.00. Rounding each share to the nearest penny separately would give £33.34, £16.67 and £50.01: £100.02, a penny that was never spent.
Fractions are brought to a common denominator exactly (with `math.gcd-lcm`), never converted to decimals.
## Edge cases
- A unit with a zero share pays nothing but still gets a line. - A negative total (a surplus refunded, a credit) is shared the same way. - `of` must be null unless the basis is fraction, and must be given (and be positive) when it is. - The shares' total, times the service charge in minor units, must stay below 2^53, so that every language computes the same split exactly. Denominators whose lowest common multiple is too large to share a charge that precisely are refused rather than approximated.
## What it does not do
It does not weight different cost heads differently (a lift schedule that excludes ground-floor flats is a second call with its own units), cap any unit's contribution, or handle reserve fund contributions separately.
Files
| Path | Bytes |
|---|---|
| README.md | 2,146 |
| impl/python.py | 2,628 |
| impl/rust.rs | 4,061 |
| impl/typescript.ts | 2,647 |
| vectors.json | 9,079 |