Functional Weave
Code in TypeScript

time.round-to-increment@1.0.0

impl/typescript.ts

673 bytes · the TypeScript implementation · view 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, type RoundingMode } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune

/**
 * Round a duration to a whole number of billing increments. The rounding is
 * math.round-div's, so "half-up" here is the same rule as for money.
 */
export function roundToIncrement(minutes: number, increment: number, mode: RoundingMode): number {
  if (!Number.isInteger(minutes) || minutes < 0) {
    throw new RangeError(`minutes must be a non-negative integer, received ${minutes}`);
  }
  if (!Number.isInteger(increment) || increment < 1) {
    throw new RangeError(`increment must be a positive integer, received ${increment}`);
  }
  return roundDiv(minutes, increment, mode) * increment;
}