Convert iD.format.XML to member functions

This commit is contained in:
John Firebaugh
2013-01-26 21:26:07 -05:00
parent 646c746991
commit 47ef222f38
6 changed files with 67 additions and 89 deletions
+3 -57
View File
@@ -1,19 +1,8 @@
iD.format.XML = {
mapping: function(entity) {
if (iD.format.XML.mappings[entity.type]) {
return iD.format.XML.mappings[entity.type](entity);
}
},
rep: function(entity, changeset_id) {
if (iD.format.XML.reps[entity.type]) {
return iD.format.XML.reps[entity.type](entity, changeset_id);
} else {
if (typeof console !== 'undefined') console.log(entity.type);
}
},
decode: function(s) {
return s.replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;');
},
// Generate Changeset XML. Returns a string.
changeset: function(tags) {
return (new XMLSerializer()).serializeToString(
@@ -29,6 +18,7 @@ iD.format.XML = {
}
}));
},
// Generate [osmChange](http://wiki.openstreetmap.org/wiki/OsmChange)
// XML. Returns a string.
osmChange: function(userid, changeset_id, changes) {
@@ -48,7 +38,7 @@ iD.format.XML = {
}
function rep(entity) {
return iD.format.XML.rep(entity, changeset_id);
return entity.asJXON(changeset_id);
}
return (new XMLSerializer()).serializeToString(JXON.unbuild({
@@ -65,49 +55,5 @@ iD.format.XML = {
})
}
}));
},
reps: {
node: function(entity, changeset_id) {
var r = {
node: {
'@id': entity.id.replace('n', ''),
'@lon': entity.loc[0],
'@lat': entity.loc[1],
'@version': (entity.version || 0),
tag: _.map(entity.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}
};
if (changeset_id) r.node['@changeset'] = changeset_id;
return r;
},
way: function(entity, changeset_id) {
var r = {
way: {
'@id': entity.id.replace('w', ''),
nd: entity.nodes.map(function(id) {
return { keyAttributes: { ref: id.replace('n', '') } };
}),
'@version': (entity.version || 0),
tag: _.map(entity.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}
};
if (changeset_id) r.way['@changeset'] = changeset_id;
return r;
}
},
mappings: {
node: function(entity) {
return iD.format.XML.decode((new XMLSerializer()).serializeToString(
JXON.unbuild(iD.format.XML.reps.node(entity))
));
},
way: function(entity) {
return iD.format.XML.decode((new XMLSerializer()).serializeToString(
JXON.unbuild(iD.format.XML.reps.way(entity))));
}
}
};
+16
View File
@@ -23,6 +23,22 @@ _.extend(iD.Node.prototype, {
return this.update({loc: loc});
},
asJXON: function(changeset_id) {
var r = {
node: {
'@id': this.id.replace('n', ''),
'@lon': this.loc[0],
'@lat': this.loc[1],
'@version': (this.version || 0),
tag: _.map(this.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}
};
if (changeset_id) r.node['@changeset'] = changeset_id;
return r;
},
asGeoJSON: function() {
return {
type: 'Feature',
+17
View File
@@ -85,6 +85,23 @@ _.extend(iD.Way.prototype, {
return this.update({nodes: nodes});
},
asJXON: function(changeset_id) {
var r = {
way: {
'@id': this.id.replace('w', ''),
'@version': this.version || 0,
nd: _.map(this.nodes, function(id) {
return { keyAttributes: { ref: id.replace('n', '') } };
}),
tag: _.map(this.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}
};
if (changeset_id) r.way['@changeset'] = changeset_id;
return r;
},
asGeoJSON: function(resolver) {
return {
type: 'Feature',
-32
View File
@@ -8,42 +8,10 @@ describe('iD.format.XML', function() {
});
});
describe('#rep', function() {
it('converts a node to jxon', function() {
expect(iD.format.XML.rep(node))
.to.eql({ node : { '@id': '-1', '@lat': 38, '@lon': -77, '@version': 0, tag: [ ] } });
});
it('converts a way to jxon', function() {
expect(iD.format.XML.rep(way))
.to.eql({ way : { '@id' : '-1', nd : [ ], '@version': 0, tag: [ ] } });
});
it('includes changeset if provided', function() {
expect(iD.format.XML.rep(node, '1234'))
.to.eql({ node : { '@id': '-1', '@lat': 38, '@lon': -77, '@version': 0, '@changeset': '1234', tag: [ ] } });
expect(iD.format.XML.rep(way, '1234'))
.to.eql({ way : { '@id' : '-1', nd : [ ], '@version': 0, '@changeset': '1234', tag: [ ] } });
});
});
describe('#osmChange', function() {
it('converts change data to XML', function() {
var jxon = iD.format.XML.osmChange('jfire', '1234', {created: [node], modified: [way], deleted: []});
expect(jxon).to.eql('<osmChange version="0.3" generator="iD"><create><node id="-1" lon="-77" lat="38" version="0" changeset="1234"/></create><modify><way id="-1" version="0" changeset="1234"/></modify></osmChange>');
});
});
describe('#mapping', function() {
it('serializes a node to xml', function() {
expect(iD.format.XML.mapping({ id: 'n-1', type: 'node', loc: [-77, 38] }))
.to.equal('&lt;node id=&quot;-1&quot; lon=&quot;-77&quot; lat=&quot;38&quot; version=&quot;0&quot;/&gt;');
});
it('serializes a way to xml', function() {
expect(iD.format.XML.mapping({ type: 'way', nodes: [], id: 'w-1' }))
.to.equal('&lt;way id=&quot;-1&quot; version=&quot;0&quot;/&gt;');
});
});
});
+16
View File
@@ -52,6 +52,22 @@ describe('iD.Node', function () {
});
});
describe("#asJXON", function () {
it('converts a node to jxon', function() {
var node = iD.Node({id: 'n-1', loc: [-77, 38], tags: {amenity: 'cafe'}});
expect(node.asJXON()).to.eql({node: {
'@id': '-1',
'@lon': -77,
'@lat': 38,
'@version': 0,
tag: [{keyAttributes: {k: 'amenity', v: 'cafe'}}]}});
});
it('includes changeset if provided', function() {
expect(iD.Node({loc: [0, 0]}).asJXON('1234').node['@changeset']).to.equal('1234');
});
});
describe("#asGeoJSON", function () {
it("converts to a GeoJSON Point features", function () {
var node = iD.Node({tags: {amenity: 'cafe'}, loc: [1, 2]}),
+15
View File
@@ -200,6 +200,21 @@ describe('iD.Way', function() {
});
});
describe("#asJXON", function () {
it('converts a way to jxon', function() {
var node = iD.Way({id: 'w-1', nodes: ['n1', 'n2'], tags: {highway: 'residential'}});
expect(node.asJXON()).to.eql({way: {
'@id': '-1',
'@version': 0,
nd: [{keyAttributes: {ref: '1'}}, {keyAttributes: {ref: '2'}}],
tag: [{keyAttributes: {k: 'highway', v: 'residential'}}]}});
});
it('includes changeset if provided', function() {
expect(iD.Way().asJXON('1234').way['@changeset']).to.equal('1234');
});
});
describe("#asGeoJSON", function () {
it("converts to a GeoJSON LineString features", function () {
var a = iD.Node({loc: [1, 2]}),