From f590684c2a7a8b9c0d18e0ed33358904c0e3effe Mon Sep 17 00:00:00 2001 From: Jan Meischner Date: Mon, 4 Oct 2021 11:56:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20can=20handle=20invalid=20last=5F?= =?UTF-8?q?page=20in=20range=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helper.test.ts | 19 ++++++++++++++++++- src/helper.ts | 7 +++++++ src/utils.ts | 8 +------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/helper.test.ts b/src/helper.test.ts index 53b0fac..c5f77c7 100644 --- a/src/helper.test.ts +++ b/src/helper.test.ts @@ -1,4 +1,4 @@ -import { add, buildQueryParams, convertDateToWCTDate } from './helper'; +import { add, buildQueryParams, convertDateToWCTDate, range } from './helper'; describe('The wecantrack API helper', () => { describe('The datetime converter', () => { @@ -36,4 +36,21 @@ describe('The wecantrack API helper', () => { expect(result).toBe('b'); }); }); + + describe('Array range should return an array with number in given range', () => { + it('should return simple range from 1 to 5', () => { + const result = range(1, 5); + expect(result).toEqual([1, 2, 3, 4, 5]); + }); + + it('should handle 1 page result', () => { + const result = range(2, 1); + expect(result).toEqual([]); + }); + + it('can handle undefined', () => { + const result = range(2, undefined); + expect(result).toEqual([]); + }); + }); }); diff --git a/src/helper.ts b/src/helper.ts index 320a4be..ba981b3 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -39,3 +39,10 @@ export function convertDateToWCTDate(date: Date): WCTDate { export function add(element: string, to: string): string { return to ? `${to},${element}` : element; } + +export const range = (start: number, end: number): number[] => { + if (!end) return []; + return Array(end - start + 1) + .fill(0) + .map((_, idx) => start + idx); +}; diff --git a/src/utils.ts b/src/utils.ts index 03ffb78..f4b6241 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ import fetch from 'node-fetch'; import { BASE_URL, Endpoint } from './config'; -import { buildQueryParams } from './helper'; +import { buildQueryParams, range } from './helper'; import { error } from './logger'; import { GeneralResponseData } from './types'; @@ -77,12 +77,6 @@ export async function getAllPages( } } -const range = (start: number, end: number): number[] => { - return Array(end - start + 1) - .fill(0) - .map((_, idx) => start + idx); -}; - const isPageRequest = (request: object): boolean => { return Object.keys(request).includes('page'); };