Skip to content

Commit

Permalink
fix: throwing an error on invalid input
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbieh committed May 24, 2019
1 parent c3b03b0 commit f5ff158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/getCoordinateKey.test.js
Expand Up @@ -16,4 +16,11 @@ describe('getCoordinateKey', () => {
it('should return an index of a GeoJSON array', () => {
expect(getCoordinateKey([1, 2], ['latitude', 'lat', 0])).toEqual(0);
});

it('should throw when an invalid coordinate is passed', () => {
expect(() => getCoordinateKey(null, ['latitude', 'lat', 0])).toThrow();
expect(() =>
getCoordinateKey(undefined, ['latitude', 'lat', 0])
).toThrow();
});
});
3 changes: 3 additions & 0 deletions src/getCoordinateKey.ts
Expand Up @@ -7,6 +7,9 @@ const getCoordinateKey = <Keys>(
return keysToLookup.reduce((foundKey: Keys | undefined, key: any):
| Keys
| undefined => {
if (typeof point === 'undefined' || point === null) {
throw new Error(`'${point}' is no valid coordinate.`);
}
if (
point.hasOwnProperty(key) &&
typeof key !== 'undefined' &&
Expand Down

0 comments on commit f5ff158

Please sign in to comment.