Skip to content

Commit

Permalink
Merge pull request #46 from molo-pl/main
Browse files Browse the repository at this point in the history
Throw error for unknown hiscores CSV format
  • Loading branch information
maxswa authored Jan 5, 2022
2 parents 8737b50 + f98cf8a commit 8daee5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion __tests__/hiscores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
getPlayerTableURL,
getSkillPageURL,
getStatsURL,
BOSSES
BOSSES,
INVALID_FORMAT_ERROR
} from '../src/index';

const B0ATY_NAME = 'B0ATY';
Expand Down Expand Up @@ -232,6 +233,16 @@ test('Parse CSV to json', () => {
expect(parseStats(csv)).toStrictEqual(expectedOutput);
});

test('Parse CSV with unknown activity', () => {
const statsWithUnknownActivity = lynxTitanStats + `
-1,-1`;
expect(() => parseStats(statsWithUnknownActivity)).toThrow(INVALID_FORMAT_ERROR);
});

test('Parse invalid CSV', () => {
expect(() => parseStats('invalid')).toThrow(INVALID_FORMAT_ERROR);
});

describe('Get name format', () => {
it('gets a name with a space', async () => {
const data = await getRSNFormat(LYNX_TITAN_SPACE_NAME);
Expand Down
7 changes: 6 additions & 1 deletion src/hiscores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
rsnFromElement,
getActivityPageURL,
httpGet,
BOSSES
BOSSES,
INVALID_FORMAT_ERROR
} from './utils';

/**
Expand Down Expand Up @@ -74,6 +75,10 @@ export function parseStats(csv: string): Stats {
.filter((entry) => !!entry)
.map((stat) => stat.split(','));

if (splitCSV.length !== SKILLS.length + BH_MODES.length + CLUES.length + BOSSES.length + 3) {
throw Error(INVALID_FORMAT_ERROR);
}

const skillObjects: Skill[] = splitCSV
.filter((stat) => stat.length === 3)
.map((stat) => {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,5 @@ export const FORMATTED_BH_NAMES: FormattedBHNames = {
export const FORMATTED_LMS = 'Last Man Standing';
export const FORMATTED_SOUL_WARS = 'Soul Wars Zeal';
export const FORMATTED_LEAGUE_POINTS = 'League Points';

export const INVALID_FORMAT_ERROR = 'Invalid hiscores format';

0 comments on commit 8daee5c

Please sign in to comment.