diff --git a/compress.js b/compress.js index 9ecc194..28cf24c 100644 --- a/compress.js +++ b/compress.js @@ -7,13 +7,18 @@ if (typeof Map == 'undefined' || !Object.entries) { return; } +var isFinite = Number.isFinite || function isFinite() { + return false; +}; + /** * @param {array} value * @returns {Boolean} is this an array where all fields are numbers (including the empty array). */ -function isNumericArray(value) { - for (var i = 0; i < value.length; i++) { - if (typeof (value[i]) !== 'number') { +function isNumericArray(array) { + for (var i = 0; i < array.length; i++) { + var v = array[i]; + if (typeof (v) !== 'number' || !isFinite(v)) { return false; } } diff --git a/test/validate.test.js b/test/validate.test.js index 0975f13..4ff2633 100644 --- a/test/validate.test.js +++ b/test/validate.test.js @@ -139,7 +139,26 @@ test('can compress memory and deduplicate points', function (t) { t.same(polygon[0], [1, 0], 'should preserve value'); t.end(); }); +test('compress should skip invalid numbers', function (t) { + var INF = 1 / 0; + // JSON.stringify doesn't support INF + var original = [[INF], [-INF], [0], [0], [INF]]; + var compressedData = geobuf.compress(original, new Map(), new Map()); + t.same([[INF], [-INF], [0], [0], [INF]], compressedData); + t.strictEqual(compressedData[2], compressedData[3]); + t.notStrictEqual(compressedData[0], compressedData[4]); + t.end(); +}); +test('compress should skip NAN', function (t) { + var original = [[0, Number.NaN], [0, null]]; + var compressedData = geobuf.compress(original, new Map(), new Map()); + t.strictEqual(compressedData[0][0], 0); + t.same(compressedData[1], [0, null]); + t.ok(Number.isNaN(compressedData[0][1])); + t.end(); +}); function roundtripTest(geojson) { + return function (t) { var buf = geobuf.encode(geojson, new Pbf()); var geojson2 = geobuf.decode(new Pbf(buf));