From d86c06f9e0300cfc58e1dcee36f79098d8826a58 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Fri, 4 Jun 2021 23:25:37 +0300 Subject: [PATCH] skip encoding null values altogether (#43) --- index.js | 7 +++++-- test/properties.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 37deb24..64b67ec 100644 --- a/index.js +++ b/index.js @@ -94,7 +94,11 @@ function writeProperties (context, pbf) { var valuecache = context.valuecache for (var key in feature.properties) { + var value = feature.properties[key] + var keyIndex = keycache[key] + if (value === null) continue // don't encode null value properties + if (typeof keyIndex === 'undefined') { keys.push(key) keyIndex = keys.length - 1 @@ -102,9 +106,8 @@ function writeProperties (context, pbf) { } pbf.writeVarint(keyIndex) - var value = feature.properties[key] var type = typeof value - if (value !== null && type !== 'string' && type !== 'boolean' && type !== 'number') { + if (type !== 'string' && type !== 'boolean' && type !== 'number') { value = JSON.stringify(value) } var valueKey = type + ':' + value diff --git a/test/properties.js b/test/properties.js index ef79ff4..b38c6e3 100644 --- a/test/properties.js +++ b/test/properties.js @@ -55,7 +55,7 @@ test('property encoding', function (t) { var second = layer.feature(1).properties t.same(first.c, '{"hello":"world"}') t.same(first.d, '[1,2,3]') - t.equals(first.e, null) + t.equals(first.e, undefined) t.same(second.c, '{"goodbye":"planet"}') t.same(second.d, '{"hello":"world"}') t.end()