|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license. |
2 | 2 | // This module is browser compatible. |
3 | 3 |
|
4 | | -/** |
5 | | - * Options for {@linkcode integerRange}. |
6 | | - */ |
| 4 | +/** Options for {@linkcode IntegerRange}. */ |
7 | 5 | export type IntegerRangeOptions = { |
8 | | - /** |
9 | | - * The step between each number in the range. |
10 | | - * @default {1} |
11 | | - */ |
12 | | - step?: number; |
13 | | - /** |
14 | | - * Whether to include the start value in the range. |
15 | | - * @default {true} |
16 | | - */ |
17 | | - includeStart?: boolean; |
18 | 6 | /** |
19 | 7 | * Whether to include the end value in the range. |
20 | 8 | * @default {false} |
21 | 9 | */ |
22 | 10 | includeEnd?: boolean; |
23 | 11 | }; |
24 | 12 |
|
25 | | -/** |
26 | | - * Creates a generator that yields integers in a range from `start` to `end`. |
27 | | - * |
28 | | - * Using the default options, yielded numbers are in the interval `[start, end)` with step size `1`. |
29 | | - * |
30 | | - * @param start The start of the range (inclusive by default) |
31 | | - * @param end The end of the range (exclusive by default) |
32 | | - * @param options Options for the range |
33 | | - * @returns A generator yielding integers in the specified range |
34 | | - * |
35 | | - * @example Usage |
36 | | - * ```ts |
37 | | - * import { integerRange } from "@std/math/integer-range"; |
38 | | - * import { assertEquals } from "@std/assert"; |
39 | | - * assertEquals([...integerRange(1, 5)], [1, 2, 3, 4]); |
40 | | - * assertEquals([...integerRange(1, 5, { step: 2 })], [1, 3]); |
41 | | - * assertEquals( |
42 | | - * [...integerRange(1, 5, { includeStart: false, includeEnd: true })], |
43 | | - * [2, 3, 4, 5], |
44 | | - * ); |
45 | | - * assertEquals([...integerRange(5, 1)], []); |
46 | | - * assertEquals([...integerRange(5, 1, { step: -1 })], [5, 4, 3, 2]); |
47 | | - * ``` |
48 | | - */ |
49 | | -export function integerRange( |
50 | | - start: number, |
51 | | - end: number, |
52 | | - options?: IntegerRangeOptions, |
53 | | -): Generator<number, undefined, undefined>; |
| 13 | +export class IntegerRange { |
| 14 | + readonly start: number; |
| 15 | + readonly end: number; |
| 16 | + readonly includeEnd: boolean; |
54 | 17 |
|
55 | | -/** |
56 | | - * Creates a generator that yields integers in a range from 0 to `end`. |
57 | | - * |
58 | | - * Using the default options, yielded numbers are in the interval `[0, end)` with step size `1`. |
59 | | - * |
60 | | - * @param end The end of the range (exclusive by default) |
61 | | - * @param options Options for the range |
62 | | - * @returns A generator yielding integers in the specified range |
63 | | - * |
64 | | - * @example Usage |
65 | | - * ```ts |
66 | | - * import { integerRange } from "@std/math/integer-range"; |
67 | | - * import { assertEquals } from "@std/assert"; |
68 | | - * assertEquals([...integerRange(5)], [0, 1, 2, 3, 4]); |
69 | | - * ``` |
70 | | - */ |
71 | | -export function integerRange( |
72 | | - end: number, |
73 | | - options?: IntegerRangeOptions, |
74 | | -): Generator<number, undefined, undefined>; |
75 | | -// deno-lint-ignore deno-style-guide/exported-function-args-maximum |
76 | | -export function* integerRange( |
77 | | - startOrEnd: number, |
78 | | - endOrOptions?: number | IntegerRangeOptions, |
79 | | - maybeOptions?: IntegerRangeOptions, |
80 | | -): Generator<number, undefined, undefined> { |
81 | | - const hasStart = typeof endOrOptions === "number"; |
82 | | - const [start, end, options] = [ |
83 | | - hasStart ? startOrEnd : 0, |
84 | | - hasStart ? endOrOptions : startOrEnd, |
85 | | - hasStart ? maybeOptions : endOrOptions, |
86 | | - ]; |
| 18 | + /** |
| 19 | + * Creates an iterable that yields integers in a range from `start` to `end`. |
| 20 | + * |
| 21 | + * Using the default options, yielded numbers are in the interval `[start, end)`. |
| 22 | + * |
| 23 | + * @param start The start of the range (inclusive by default) |
| 24 | + * @param end The end of the range (exclusive by default) |
| 25 | + * @param options Options for the range |
| 26 | + * |
| 27 | + * @example Usage |
| 28 | + * ```ts |
| 29 | + * import { IntegerRange } from "@std/math/integer-range"; |
| 30 | + * import { assertEquals } from "@std/assert"; |
| 31 | + * assertEquals([...new IntegerRange(1, 5)], [1, 2, 3, 4]); |
| 32 | + * ``` |
| 33 | + */ |
| 34 | + constructor( |
| 35 | + start: number, |
| 36 | + end: number, |
| 37 | + options?: IntegerRangeOptions, |
| 38 | + ); |
| 39 | + /** |
| 40 | + * Creates an iterable that yields integers in a range from `0` to `end`. |
| 41 | + * |
| 42 | + * Using the default options, yielded numbers are in the interval `[0, end)`. |
| 43 | + * |
| 44 | + * @param end The end of the range (exclusive by default) |
| 45 | + * @param options Options for the range |
| 46 | + * |
| 47 | + * @example Usage |
| 48 | + * ```ts |
| 49 | + * import { IntegerRange } from "@std/math/integer-range"; |
| 50 | + * import { assertEquals } from "@std/assert"; |
| 51 | + * assertEquals([...new IntegerRange(5)], [0, 1, 2, 3, 4]); |
| 52 | + * ``` |
| 53 | + */ |
| 54 | + constructor(end: number, options?: IntegerRangeOptions); |
| 55 | + constructor( |
| 56 | + startOrEnd: number, |
| 57 | + endOrOptions?: number | IntegerRangeOptions, |
| 58 | + maybeOptions?: IntegerRangeOptions, |
| 59 | + ) { |
| 60 | + const hasStart = typeof endOrOptions === "number"; |
| 61 | + this.start = hasStart ? startOrEnd : 0; |
| 62 | + this.end = hasStart ? endOrOptions : startOrEnd; |
| 63 | + const options = hasStart ? maybeOptions : endOrOptions; |
| 64 | + this.includeEnd = options?.includeEnd ?? false; |
87 | 65 |
|
88 | | - const { step = 1, includeStart = true, includeEnd = false } = options ?? {}; |
89 | | - if (step === 0) throw new RangeError("`step` must not be zero"); |
90 | | - for (const [k, v] of Object.entries({ start, end, step })) { |
91 | | - if (!Number.isSafeInteger(v)) { |
92 | | - throw new RangeError(`\`${k}\` must be a safe integer`); |
| 66 | + for (const k of ["start", "end"] as const) { |
| 67 | + if (!Number.isSafeInteger(this[k])) { |
| 68 | + throw new RangeError(`\`${k}\` must be a safe integer`); |
| 69 | + } |
93 | 70 | } |
94 | 71 | } |
95 | 72 |
|
96 | | - if (start === end && !(includeStart && includeEnd)) return; |
| 73 | + /** |
| 74 | + * Generates numbers in the range with the default step size of 1. |
| 75 | + * |
| 76 | + * @returns A generator yielding numbers in the specified range with step size 1. |
| 77 | + * |
| 78 | + * @example Usage |
| 79 | + * ```ts |
| 80 | + * import { IntegerRange } from "@std/math/integer-range"; |
| 81 | + * import { assertEquals } from "@std/assert"; |
| 82 | + * assertEquals([...new IntegerRange(1, 5)], [1, 2, 3, 4]); |
| 83 | + * assertEquals([...new IntegerRange(1, 5, { includeEnd: true })], [1, 2, 3, 4, 5]); |
| 84 | + * assertEquals([...new IntegerRange(5, 1)], []); |
| 85 | + * ``` |
| 86 | + */ |
| 87 | + *[Symbol.iterator](): Generator<number, undefined, undefined> { |
| 88 | + yield* this.step(1); |
| 89 | + } |
97 | 90 |
|
98 | | - const limitsSign = Math.sign(end - start); |
99 | | - const stepSign = Math.sign(step); |
100 | | - if (limitsSign !== 0 && limitsSign !== stepSign) return; |
| 91 | + /** |
| 92 | + * Generates numbers in the range with the specified step size. |
| 93 | + * |
| 94 | + * @param step The step size between yielded numbers. |
| 95 | + * @returns A generator yielding numbers in the specified range with the given step size. |
| 96 | + * |
| 97 | + * @example Usage |
| 98 | + * ```ts |
| 99 | + * import { IntegerRange } from "@std/math/integer-range"; |
| 100 | + * import { assertEquals } from "@std/assert"; |
| 101 | + * assertEquals([...new IntegerRange(1, 5).step(2)], [1, 3]); |
| 102 | + * assertEquals([...new IntegerRange(1, 5, { includeEnd: true }).step(2)], [1, 3, 5]); |
| 103 | + * assertEquals([...new IntegerRange(5, 1).step(-1)], [5, 4, 3, 2]); |
| 104 | + * ``` |
| 105 | + */ |
| 106 | + *step(step: number): Generator<number, undefined, undefined> { |
| 107 | + if (!Number.isSafeInteger(step) || step === 0) { |
| 108 | + throw new RangeError("`step` must be a safe, non-zero integer"); |
| 109 | + } |
101 | 110 |
|
102 | | - if (includeStart) yield start; |
| 111 | + const { start, end, includeEnd } = this; |
| 112 | + if (start === end && !includeEnd) return; |
103 | 113 |
|
104 | | - let i = 0; |
105 | | - const delta = Math.abs(step); |
106 | | - const maxDelta = Math.abs(end - start); |
107 | | - for (i += delta; i < maxDelta; i += delta) yield start + (i * stepSign); |
| 114 | + const limitsSign = Math.sign(end - start); |
| 115 | + const stepSign = Math.sign(step); |
| 116 | + if (limitsSign !== 0 && limitsSign !== stepSign) return; |
108 | 117 |
|
109 | | - if (includeEnd && (i * stepSign) + start === end) yield end; |
| 118 | + let i = 0; |
| 119 | + const delta = Math.abs(step); |
| 120 | + const maxDelta = Math.abs(end - start); |
| 121 | + for (; i < maxDelta; i += delta) yield start + (i * stepSign); |
| 122 | + if (includeEnd && i === maxDelta) yield end; |
| 123 | + } |
110 | 124 | } |
0 commit comments