-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ajith Ranka
committed
Mar 9, 2017
1 parent
2251d00
commit 4fc3acb
Showing
11 changed files
with
1,106,394 additions
and
93 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,23 @@ | ||
var R = require('ramda'); | ||
var sax = require('sax'); | ||
|
||
// Returns elements grouped by changeset ID. | ||
|
||
function AugmentedDiffParser (xmlData, changesetsFilter, callback) { | ||
var xmlParser = sax.parser(true, { lowercase: true }); | ||
var currentAction = ''; | ||
var currentElement = {}; | ||
var oldElement = {}; | ||
var currentMember = {}; | ||
var currentMode = ''; | ||
var changesetMap = {}; | ||
|
||
function isElement (symbol) { | ||
return (symbol === 'node' || symbol === 'way' || symbol === 'relation'); | ||
} | ||
|
||
function endTag (symbol) { | ||
if (symbol === 'action') { | ||
var changeset = currentElement.changeset; | ||
if (changesetsFilter && changesetsFilter.length) { | ||
if (changesetsFilter.indexOf(changeset) !== -1) { | ||
if (changesetMap[changeset]) { | ||
changesetMap[changeset].push(currentElement); | ||
} else { | ||
changesetMap[changeset] = [currentElement]; | ||
} | ||
} | ||
} else { | ||
if (changesetMap[changeset]) { | ||
changesetMap[changeset].push(currentElement); | ||
} else { | ||
changesetMap[changeset] = [currentElement]; | ||
} | ||
} | ||
} | ||
if (symbol === 'osm') { | ||
callback(null, changesetMap); | ||
} | ||
} | ||
|
||
function startTag (node) { | ||
var symbol = node.name; | ||
var attrs = node.attributes; | ||
|
||
if (symbol === 'action') { | ||
currentAction = attrs.type; | ||
} | ||
if (symbol === 'new' || symbol === 'old') { | ||
currentMode = symbol; | ||
} | ||
if (isElement(symbol)) { | ||
if (currentMode === 'new' && (currentAction === 'modify' || | ||
currentAction === 'delete')) { | ||
oldElement = R.clone(currentElement); | ||
currentElement = attrs; | ||
currentElement.old = oldElement; | ||
} else { | ||
currentElement = attrs; | ||
} | ||
currentElement.action = currentAction; | ||
currentElement.type = symbol; | ||
currentElement.tags = {}; | ||
if (symbol === 'way') {currentElement.nodes = []; } | ||
if (symbol === 'relation') {currentElement.members = []; currentMember = {};} | ||
} | ||
if (symbol === 'tag' && currentElement) { | ||
currentElement.tags[attrs.k] = attrs.v; | ||
} | ||
|
||
if (symbol === 'nd' && currentElement && currentElement.type === 'way') { | ||
currentElement.nodes.push(attrs); | ||
} | ||
|
||
if (symbol === 'nd' && currentElement && currentElement.type === 'relation') { | ||
currentMember.nodes.push(attrs); | ||
} | ||
|
||
if (symbol === 'member' && currentElement && currentElement.type === 'relation') { | ||
currentMember = R.clone(attrs); | ||
currentMember.nodes = []; | ||
currentElement.members.push(currentMember); | ||
} | ||
} | ||
|
||
xmlParser.onopentag = startTag; | ||
xmlParser.onclosetag = endTag; | ||
xmlParser.onerror = function(err) { callback(err, null); }; | ||
xmlParser.write(xmlData); | ||
} | ||
|
||
module.exports = AugmentedDiffParser; | ||
const fs = require('fs'); | ||
const tape = require('tape'); | ||
const parser = require('../index.js'); | ||
|
||
const isOSCFile = (filename) => (/^.*\.osc$/).test(filename); | ||
const stripExtension = (filename) => filename.split('.')[0]; | ||
|
||
const filenames = | ||
fs.readdirSync('tests/data', { encoding: 'utf-8' }) | ||
.filter(isOSCFile) | ||
.map(stripExtension); | ||
|
||
filenames.forEach(function(filename) { | ||
tape(`testing file: ${filename}`, function(t) { | ||
const xml = fs.readFileSync(`tests/data/${filename}.osc`, { encoding: 'utf-8' }); | ||
const expectedJSON = JSON.parse(fs.readFileSync(`tests/data/${filename}.json`)); | ||
|
||
parser(xml, null, function(error, actualJSON) { | ||
t.deepEqual(actualJSON, expectedJSON, 'parsed correctly'); | ||
t.end(); | ||
}); | ||
}); | ||
}); |