UNPKG

partition-interval

Version:
36 lines 1.71 kB
//#region src/index.d.ts /** * Partitions the given closed `interval` into the given number of `partitions` * as evenly as possible. * * Returns a lazy iterable of closed intervals. * * @throws if the start or end of `interval` is not an integer, the start of * `interval` is greater than its end, or `partitions` is not a positive * integer. * * @example * ```js * // Lazily iterate over the returned iterable. * for (const interval of partitionInterval([0, 99], 4)) { * console.log(interval) * } * //=> [ 0, 24 ] * //=> [ 25, 49 ] * //=> [ 50, 74 ] * //=> [ 75, 99 ] * * const intervals = [...partitionInterval([-31, 89], 5)] * console.log(intervals) * //=> [ [-31, -8], [-7, 16], [17, 40], [41, 64], [65, 89] ] * ``` */ declare const partitionInterval: <Start extends number, End extends number, Partitions extends number>(interval: readonly [Integer<Start>, Integer<End>], partitions: PositiveInteger<Partitions>) => Iterable<[number, number]>; type StrictNegative<Numeric extends number> = Numeric extends 0 ? never : `${Numeric}` extends `-${string}` ? Numeric : never; type StrictPositive<Numeric extends number> = Numeric extends 0 ? never : StrictNegative<Numeric> extends never ? Numeric : never; type StrictInteger<Numeric extends number> = `${Numeric}` extends `${bigint}` ? Numeric : never; type CheckNumericLiteral<Numeric extends number, LiteralNumber extends number> = number extends Numeric ? Numeric : LiteralNumber; type Integer<Numeric extends number> = CheckNumericLiteral<Numeric, StrictInteger<Numeric>>; type PositiveInteger<Numeric extends number> = CheckNumericLiteral<Numeric, StrictPositive<Numeric>>; //#endregion export { partitionInterval as default };