1 | var util = require('../lib/util')
|
2 | var errors = require('../errors')
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function replacePlaceholderIds (changes, cb) {
|
13 | var idMap = {
|
14 | node: {},
|
15 | way: {},
|
16 | relation: {}
|
17 | }
|
18 | var dupIds = []
|
19 |
|
20 |
|
21 |
|
22 | var changesWithIds = changes.map(function (change) {
|
23 | var mapped = Object.assign({}, change)
|
24 | if (change.action === 'create') {
|
25 | if (idMap[change.type][change.id] || !change.id) {
|
26 | dupIds.push(change.id)
|
27 | }
|
28 | var id = util.generateId()
|
29 | idMap[change.type][change.id] = id
|
30 | mapped.id = id
|
31 | mapped.old_id = change.id
|
32 | }
|
33 | return mapped
|
34 | })
|
35 |
|
36 | .map(function (change) {
|
37 |
|
38 | if (change.type === 'way' && change.nodes) {
|
39 | change.nodes = change.nodes.map(function (ref) {
|
40 | return idMap.node[ref] || ref
|
41 | })
|
42 | }
|
43 | if (change.type === 'relation' && change.members) {
|
44 | change.members = change.members.map(function (member) {
|
45 | if (!idMap[member.type][member.ref]) return Object.assign({}, member)
|
46 | return Object.assign({}, member, {ref: idMap[member.type][member.ref]})
|
47 | })
|
48 | }
|
49 | return change
|
50 | })
|
51 | if (dupIds.length) {
|
52 | var errMsg = '#' + dupIds.join(', #')
|
53 | return cb(new errors.PlaceholderIdError(errMsg))
|
54 | }
|
55 | cb(null, changesWithIds)
|
56 | }
|