Skip to content

@jsvision/ui / apportion

Function: apportion()

apportion(total, weights): number[]

Defined in: ui/src/layout/apportion.ts:43

Distribute total integer cells across weighted shares so the result sums to total exactly. Uses the largest-remainder method: floor each ideal share, then give the leftover cells to the items with the largest remainders, so no cell is lost to rounding. Ties break toward the earlier item, so the result is deterministic.

Parameters

total

number

Integer cells to distribute (≤ 0 → all zeros).

weights

readonly number[]

Per-item grow weights; treated as non-negative integers (fractional weights are rounded, keeping the apportionment exact).

Returns

number[]

One integer size per weight, summing to total when any weight is positive; all zeros otherwise.

Example

ts
import { apportion } from '@jsvision/ui';

// 10 cells split three equal ways — the odd cell goes to the first item.
apportion(10, [1, 1, 1]); // → [4, 3, 3]  (sums to exactly 10)

// Weighted split: a 2:1:1 ratio of 100 cells.
apportion(100, [2, 1, 1]); // → [50, 25, 25]