Skip to content

Commit 1714a03

Browse files
committed
Skip INF/NAN
1 parent 8f6f509 commit 1714a03

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

compress.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ if (typeof Map == 'undefined' || !Object.entries) {
77
return;
88
}
99

10+
var isFinite = Number.isFinite || function isFinite() {
11+
return false;
12+
};
13+
1014
/**
1115
* @param {array} value
1216
* @returns {Boolean} is this an array where all fields are numbers (including the empty array).
1317
*/
14-
function isNumericArray(value) {
15-
for (var i = 0; i < value.length; i++) {
16-
if (typeof (value[i]) !== 'number') {
18+
function isNumericArray(array) {
19+
for (var i = 0; i < array.length; i++) {
20+
var v = array[i];
21+
if (typeof (v) !== 'number' || !isFinite(v)) {
1722
return false;
1823
}
1924
}

test/validate.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,26 @@ test('can compress memory and deduplicate points', function (t) {
139139
t.same(polygon[0], [1, 0], 'should preserve value');
140140
t.end();
141141
});
142+
test('compress should skip invalid numbers', function (t) {
143+
var INF = 1 / 0;
144+
// JSON.stringify doesn't support INF
145+
var original = [[INF], [-INF], [0], [0], [INF]];
146+
var compressedData = geobuf.compress(original, new Map(), new Map());
147+
t.same([[INF], [-INF], [0], [0], [INF]], compressedData);
148+
t.strictEqual(compressedData[2], compressedData[3]);
149+
t.notStrictEqual(compressedData[0], compressedData[4]);
150+
t.end();
151+
});
152+
test('compress should skip NAN', function (t) {
153+
var original = [[0, Number.NaN], [0, null]];
154+
var compressedData = geobuf.compress(original, new Map(), new Map());
155+
t.strictEqual(compressedData[0][0], 0);
156+
t.same(compressedData[1], [0, null]);
157+
t.ok(Number.isNaN(compressedData[0][1]));
158+
t.end();
159+
});
142160
function roundtripTest(geojson) {
161+
143162
return function (t) {
144163
var buf = geobuf.encode(geojson, new Pbf());
145164
var geojson2 = geobuf.decode(new Pbf(buf));

0 commit comments

Comments
 (0)