From df0d293dc9861b7c9d3260468610d039f468f9f7 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Tue, 30 Apr 2024 13:56:57 +0300 Subject: [PATCH] Make primeRange start parameter optional Fix broken documentation. --- src/__tests__/primes.spec.ts | 4 ++++ src/primes.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/__tests__/primes.spec.ts b/src/__tests__/primes.spec.ts index 79b9e95..02ab00a 100644 --- a/src/__tests__/primes.spec.ts +++ b/src/__tests__/primes.spec.ts @@ -130,6 +130,10 @@ describe('Prime range generator', () => { expect(primeRange(0, 4)).toEqual([2, 3, 5, 7]); }); + it('produces the first 4 primes (start is optional)', () => { + expect(primeRange(4)).toEqual([2, 3, 5, 7]); + }); + it('produces 5 primes starting from the 999th odd prime', () => { expect(primeRange(999, 999 + 5)).toEqual([7919, 7927, 7933, 7937, 7949]); }); diff --git a/src/primes.ts b/src/primes.ts index c82b4f3..1a70fe9 100644 --- a/src/primes.ts +++ b/src/primes.ts @@ -206,12 +206,16 @@ export function primes(start: number, end?: number) { } /** - * Obtain a range of odd primes starting at ordinal the given ordinal. + * Obtain a range of primes starting at the given ordinal. Prime 2 has ordinal 0. * @param start 1-based ordinal of the nth odd prime to start from, or zero to include prime two. * @param end Range end. `end - start` elements are returned. * @returns The primes in the range. */ -export function primeRange(start: number, end: number) { +export function primeRange(start: number, end?: number) { + if (end === undefined) { + end = start; + start = 0; + } start = Math.round(Math.max(0, start)); end = Math.round(end); if (end <= start) {