API reference › @evolu/common › Number › computeBalancedBuckets
function computeBalancedBuckets(
numberOfItems: number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative">,
numberOfBuckets?: number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">,
minNumberOfItemsPerBucket?: number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">,
): Result<
readonly [
number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">,
number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">,
],
number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">
>;
Defined in: packages/common/src/Number.ts:198
Divides items into buckets as evenly as possible, ensuring each bucket has at least the minimum number of items. Returns a success result if the minimum is met, or an error result with the required number of items if not.
Example
import {
assertErr,
assertOk,
computeBalancedBuckets,
NonNegativeInt,
PositiveInt,
} from "@evolu/common";
const balanced = computeBalancedBuckets(
NonNegativeInt.orThrow(10),
PositiveInt.orThrow(3),
PositiveInt.orThrow(2),
);
assertOk(balanced, [4, 7, 10]);
const insufficient = computeBalancedBuckets(
NonNegativeInt.orThrow(5),
PositiveInt.orThrow(3),
PositiveInt.orThrow(2),
);
assertErr(insufficient, 6);