Skip to content

Commit

Permalink
skip encoding null values altogether (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner authored Jun 4, 2021
1 parent 0adf420 commit d86c06f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,20 @@ 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
keycache[key] = keyIndex
}
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
Expand Down
2 changes: 1 addition & 1 deletion test/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d86c06f

Please sign in to comment.