osm-p2p-server
Advanced tools
Comparing version 2.0.0-beta3 to 2.0.0-beta4
@@ -7,3 +7,3 @@ var util = require('../lib/util') | ||
* - Any `id` of a created object is considered a placeholder id | ||
* - Throws an error if placeholder ids are not unique | ||
* - Throws an error if placeholder ids are not unique within types | ||
* - Does not mutate arguments | ||
@@ -14,3 +14,7 @@ * - cb() with array of changes with ids replaced and an additional | ||
module.exports = function replacePlaceholderIds (changes, cb) { | ||
var idMap = {} | ||
var idMap = { | ||
node: {}, | ||
way: {}, | ||
relation: {} | ||
} | ||
var dupIds = [] | ||
@@ -20,7 +24,7 @@ var changesWithIds = changes.map(function (change) { | ||
if (change.action === 'create') { | ||
if (idMap[change.id] || !change.id) { | ||
if (idMap[change.type][change.id] || !change.id) { | ||
dupIds.push(change.id) | ||
} | ||
var id = util.generateId() | ||
idMap[change.id] = id | ||
idMap[change.type][change.id] = id | ||
mapped.id = id | ||
@@ -31,3 +35,3 @@ mapped.old_id = change.id | ||
mapped.nodes = change.nodes.map(function (ref) { | ||
return idMap[ref] || ref | ||
return idMap.node[ref] || ref | ||
}) | ||
@@ -37,4 +41,4 @@ } | ||
mapped.members = change.members.map(function (member) { | ||
if (!idMap[member.ref]) return Object.assign({}, member) | ||
return Object.assign({}, member, {ref: idMap[member.ref]}) | ||
if (!idMap[member.type][member.ref]) return Object.assign({}, member) | ||
return Object.assign({}, member, {ref: idMap[member.type][member.ref]}) | ||
}) | ||
@@ -41,0 +45,0 @@ } |
{ | ||
"name": "osm-p2p-server", | ||
"version": "2.0.0-beta3", | ||
"version": "2.0.0-beta4", | ||
"description": "Peer-to-peer OpenStreetMap API v0.6 Server", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,4 @@ var test = require('tape') | ||
// TODO: Tests for relations | ||
test('replacePlaceholderIds', t => { | ||
@@ -25,3 +27,3 @@ var input = [ | ||
test('replacePlaceholderIds: duplicate ids', t => { | ||
test('replacePlaceholderIds: duplicate node ids', t => { | ||
var input = [ | ||
@@ -37,1 +39,44 @@ {action: 'create', type: 'node', id: '-123'}, | ||
}) | ||
test('replacePlaceholderIds: duplicate way ids', t => { | ||
var input = [ | ||
{action: 'create', type: 'way', id: '-123'}, | ||
{action: 'create', type: 'way', id: '-123'} | ||
] | ||
replacePlaceholderIds(input, function (err) { | ||
t.true(err instanceof Error, 'returns error') | ||
t.true(/#-123/.test(err.message), 'error references dup id') | ||
t.end() | ||
}) | ||
}) | ||
test('replacePlaceholderIds: duplicate relation ids', t => { | ||
var input = [ | ||
{action: 'create', type: 'relation', id: '-123'}, | ||
{action: 'create', type: 'relation', id: '-123'} | ||
] | ||
replacePlaceholderIds(input, function (err) { | ||
t.true(err instanceof Error, 'returns error') | ||
t.true(/#-123/.test(err.message), 'error references dup id') | ||
t.end() | ||
}) | ||
}) | ||
test('replacePlaceholderIds: placeholder ids can be duplicated between types', t => { | ||
var input = [ | ||
{action: 'create', type: 'node', id: 'A'}, | ||
{action: 'create', type: 'way', id: 'A', nodes: ['A']}, | ||
{action: 'modify', type: 'way', id: 'B', nodes: ['A', 'X']} | ||
] | ||
replacePlaceholderIds(input, function (err, result) { | ||
t.error(err) | ||
t.notEqual(result[0].id, input[0].id, 'id replaced') | ||
t.equal(result[0].old_id, input[0].id, 'old_id prop is placeholder id') | ||
t.notEqual(result[1].id, input[1].id, 'id replaced') | ||
t.equal(result[1].old_id, input[1].id, 'old_id prop is placeholder id') | ||
t.equal(result[1].nodes[0], result[0].id, 'created way ref updated') | ||
t.deepEqual(result[2].nodes, [result[0].id, 'X'], 'modified node ref updated') | ||
t.equal(result[2].old_id, undefined, 'old_id not set on modified way') | ||
t.end() | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169556
4813