Skip to content

Commit

Permalink
Skip INF/NAN
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonAndre committed Jan 17, 2022
1 parent 8f6f509 commit 91511be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ if (typeof Map == 'undefined' || !Object.entries) {
return;
}

var isFinite = Number.isFinite || function isFinite(num) {
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;
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 91511be

Please sign in to comment.