API reference@evolu/commonNumber › 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:123

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 {
  computeBalancedBuckets,
  NonNegativeInt,
  PositiveInt,
} from "@evolu/common";

expectOk(
  computeBalancedBuckets(
    NonNegativeInt.orThrow(10),
    PositiveInt.orThrow(3),
    PositiveInt.orThrow(2),
  ),
  [4, 7, 10],
);
expectErr(
  computeBalancedBuckets(
    NonNegativeInt.orThrow(5),
    PositiveInt.orThrow(3),
    PositiveInt.orThrow(2),
  ),
  6,
);