Skip to content

Commit

Permalink
🐛 can handle invalid last_page in range function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Meischner committed Oct 4, 2021
1 parent 45055cf commit f590684
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
19 changes: 18 additions & 1 deletion src/helper.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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([]);
});
});
});
7 changes: 7 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
8 changes: 1 addition & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -77,12 +77,6 @@ export async function getAllPages<T extends GeneralResponseData>(
}
}

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');
};

0 comments on commit f590684

Please sign in to comment.