Skip to content

Commit

Permalink
fix: getRoughCompassDirection regex used "contains" logic rather than…
Browse files Browse the repository at this point in the history
… exact matching resulting incorrect results
  • Loading branch information
ddarren committed May 30, 2023
1 parent 44b3557 commit 955937b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
40 changes: 40 additions & 0 deletions src/getRoughCompassDirection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import getRoughCompassDirection from './getRoughCompassDirection';

describe('getRoughCompassDirection', () => {
describe('when exact compass direction is Northern', () => {
it('returns N', () => {
['NNE', 'NE', 'NNW', 'N'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'N'
);
});
});
});
describe('when exact compass direction is Eastern', () => {
it('returns E', () => {
['ENE', 'E', 'ESE', 'SE'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'E'
);
});
});
});
describe('when exact compass direction is Southern', () => {
it('returns S', () => {
['SSE', 'S', 'SSW', 'SW'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'S'
);
});
});
});
describe('when exact compass direction is Western', () => {
it('returns W', () => {
['WSW', 'W', 'WNW', 'NW'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'W'
);
});
});
});
});
8 changes: 4 additions & 4 deletions src/getRoughCompassDirection.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Receives an exact compass direction (like WNW) and spits out a very rough
// and overly simplified direction (N|E|S|W). Use with caution!
const getRoughCompassDirection = (exact: string) => {
if (/^NNE|NE|NNW|N$/.test(exact)) {
if (/^(NNE|NE|NNW|N)$/.test(exact)) {
return 'N';
}

if (/^ENE|E|ESE|SE$/.test(exact)) {
if (/^(ENE|E|ESE|SE)$/.test(exact)) {
return 'E';
}

if (/^SSE|S|SSW|SW$/.test(exact)) {
if (/^(SSE|S|SSW|SW)$/.test(exact)) {
return 'S';
}

if (/^WSW|W|WNW|NW$/.test(exact)) {
if (/^(WSW|W|WNW|NW)$/.test(exact)) {
return 'W';
}
};
Expand Down

0 comments on commit 955937b

Please sign in to comment.