diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-10 21:15:12 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-10 21:15:12 +0100 |
| commit | 4513442e8aae54452f43a1d434a4c16728e77386 (patch) | |
| tree | d0c5deee61e45510756e369ba0936244e2315c2d /lib/xmlreader.js | |
| parent | 9a4c6832d7f3cd64744ec525ab763617aa675bad (diff) | |
Map namespaces of attributes
Diffstat (limited to 'lib/xmlreader.js')
| -rw-r--r-- | lib/xmlreader.js | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/xmlreader.js b/lib/xmlreader.js index f5a43ae..ef16f7b 100644 --- a/lib/xmlreader.js +++ b/lib/xmlreader.js @@ -23,17 +23,17 @@ function read(xmlString, namespaceMap) { var deferred = q.defer(); parser.onopentag = function(node) { - var attributes = mapValues(node.attributes, function(attribute) { + var attributes = mapObject(node.attributes, function(attribute) { return attribute.value; - }); + }, mapName); - var element = new Element(generateTagName(node), attributes); + var element = new Element(mapName(node), attributes); currentElement.children.push(element); stack.push(currentElement); currentElement = element }; - function generateTagName(node) { + function mapName(node) { if (node.uri) { var mappedPrefix = namespaceMap[node.uri]; var prefix; @@ -72,11 +72,12 @@ function read(xmlString, namespaceMap) { return deferred.promise; } -function mapValues(input, mapper, context) { - return _.reduce(input, function (obj, v, k) { - obj[k] = mapper.call(context, v, k, input); - return obj; - }, {}, context); +function mapObject(input, valueFunc, keyFunc) { + return _.reduce(input, function(result, value, key) { + var mappedKey = keyFunc.call(null, value, key, input); + result[mappedKey] = valueFunc.call(null, value, key, input); + return result; + }, {}); } function Element(name, attributes, children) { |
