Skip to content

Commit

Permalink
FIX: ensure property key strings are serialized in correct order (map…
Browse files Browse the repository at this point in the history
  • Loading branch information
ivorblockley authored and mourner committed Dec 10, 2017
1 parent afcbe2f commit 1f3fd41
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = encode;

var keys, keysNum, dim, e,
var keys, keysNum, keysArr, dim, e,
maxPrecision = 1e6;

var geometryTypes = {
Expand All @@ -17,6 +17,7 @@ var geometryTypes = {

function encode(obj, pbf) {
keys = {};
keysArr = [];
keysNum = 0;
dim = 0;
e = 1;
Expand All @@ -26,8 +27,6 @@ function encode(obj, pbf) {
e = Math.min(e, maxPrecision);
var precision = Math.ceil(Math.log(e) / Math.LN10);

var keysArr = Object.keys(keys);

for (var i = 0; i < keysArr.length; i++) pbf.writeStringField(1, keysArr[i]);
if (dim !== 2) pbf.writeVarintField(2, dim);
if (precision !== 6) pbf.writeVarintField(3, precision);
Expand Down Expand Up @@ -85,7 +84,10 @@ function analyzePoint(point) {
}

function saveKey(key) {
if (keys[key] === undefined) keys[key] = keysNum++;
if (keys[key] === undefined) {
keysArr.push(key);
keys[key] = keysNum++;
}
}

function writeFeatureCollection(obj, pbf) {
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/issue90.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "Feature",
"" : "",
"xyz" : null,
"!" : {},
"~" : [],
"2" : [null],
"properties": { "foo" : null, "" : null, "bar" : {} },
"geometry": {
"type": "Point",
"coordinates": [100.0, 0.0]
}
}
1 change: 1 addition & 0 deletions test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ for (var name in geojsonFixtures) {
test('roundtrip issue', roundtripTest(getJSON('issue62.json')));

test('roundtrip custom properties', roundtripTest(getJSON('props.json')));
test('roundtrip issue90', roundtripTest(getJSON('issue90.json')));
test('roundtrip single-ring MultiPolygon', roundtripTest(getJSON('single-multipoly.json')));

test('roundtrip valid closed polygon with high-precision coordinates', function (t) {
Expand Down

0 comments on commit 1f3fd41

Please sign in to comment.