Skip to content

Commit

Permalink
Handle 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 3fe4c68
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
21 changes: 17 additions & 4 deletions compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ if (typeof Map == 'undefined' || !Object.entries) {
* @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') {
return false;
}
}
return true;
}

/**
* @param {string[]} array, possibly including Infinity/NaN
* @return {string} cache key identifying an array with those numbers.
*/
function createCacheKey(array) {
var parts = [];
for (var i = 0; i < array.length; i++) {
parts.push(String(array[i]));
}
return parts.join(',');
}

/**
* Compress data returned by geobuf's decode function.
* Objects are modified in place.
Expand Down Expand Up @@ -55,7 +68,7 @@ function compress(value, cache = new Map(), numericArrayCache = null) {
// and experimentally appears to reduce capacity used.
var result = value.slice();
if (numericArrayCache && isNumericArray(result)) {
var cacheKey = JSON.stringify(result);
var cacheKey = createCacheKey(result);
var cachedEntry = numericArrayCache.get(cacheKey);
if (cachedEntry) {
cache.set(value, cachedEntry);
Expand Down
20 changes: 20 additions & 0 deletions test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,27 @@ test('can compress memory and deduplicate points', function (t) {
t.same(polygon[0], [1, 0], 'should preserve value');
t.end();
});
test('compress should handle infinite 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.strictEqual(compressedData[0], compressedData[4]);
t.end();
});
test('compress should handle NaN', function (t) {
var original = [[0, Number.NaN], [0, Number.NaN], [0, null]];
var compressedData = geobuf.compress(original, new Map(), new Map());
t.strictEqual(compressedData[0][0], 0);
t.strictEqual(compressedData[0], compressedData[1]);
t.same(compressedData[2], [0, null]);
t.ok(Number.isNaN(compressedData[0][1])); // Note that NaN !== NaN
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 3fe4c68

Please sign in to comment.