Check sanity of inputs (#251)

* add sanity check

* add tests

* bump wkt-parser

* fix typo

* fix other typo

* reduce duplication

* consistent naming

* spelling
This commit is contained in:
Calvin Metcalf 2017-04-28 15:08:53 -04:00 committed by GitHub
parent 92f412fd16
commit 42fae04d12
4 changed files with 39 additions and 2 deletions

15
lib/checkSanity.js Normal file
View File

@ -0,0 +1,15 @@
export default function (point) {
checkCoord(point.x);
checkCoord(point.y);
}
function checkCoord(num) {
if (typeof Number.isFinite === 'function') {
if (Number.isFinite(num)) {
return;
}
throw new TypeError('coordinates must be finite numbers');
}
if (typeof num !== 'number' || num !== num || !isFinite(num)) {
throw new TypeError('coordinates must be finite numbers');
}
}

View File

@ -3,6 +3,8 @@ import datum_transform from './datum_transform';
import adjust_axis from './adjust_axis';
import proj from './Proj';
import toPoint from './common/toPoint';
import checkSanity from './checkSanity';
function checkNotWGS(source, dest) {
return ((source.datum.datum_type === PJD_3PARAM || source.datum.datum_type === PJD_7PARAM) && dest.datumCode !== 'WGS84') || ((dest.datum.datum_type === PJD_3PARAM || dest.datum.datum_type === PJD_7PARAM) && source.datumCode !== 'WGS84');
}
@ -12,7 +14,7 @@ export default function transform(source, dest, point) {
if (Array.isArray(point)) {
point = toPoint(point);
}
checkSanity(point);
// Workaround for datum shifts towgs84, if either source or destination projection is not wgs84
if (source.datum && dest.datum && checkNotWGS(source, dest)) {
wgs84 = new proj('WGS84');

View File

@ -38,6 +38,6 @@
},
"dependencies": {
"mgrs": "1.0.0",
"wkt-parser": "^1.1.3"
"wkt-parser": "^1.2.0"
}
}

View File

@ -225,6 +225,26 @@ function startTests(chai, proj4, testPoints) {
new proj4.Proj('fake one');
}, 'fake one', 'should work');
});
it('should throw when passed null', function() {
assert.throws(function() {
proj4('+proj=utm +zone=31', [null, 0]);
}, 'coordinates must be finite numbers', 'should work');
});
it('should throw when passed NaN', function() {
assert.throws(function() {
proj4('+proj=utm +zone=31', [0, NaN]);
}, 'coordinates must be finite numbers', 'should work');
});
it('should throw when passed Infinity', function() {
assert.throws(function() {
proj4('+proj=utm +zone=31', [Infinity, 0]);
}, 'coordinates must be finite numbers', 'should work');
});
it('should throw when passed -Infinity', function() {
assert.throws(function() {
proj4('+proj=utm +zone=31', [-Infinity, 0]);
}, 'coordinates must be finite numbers', 'should work');
});
});
describe('utility', function() {
it('should have MGRS available in the proj4.util namespace', function() {