-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
28 lines (27 loc) · 1.1 KB
/
index.js
File metadata and controls
28 lines (27 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const flatten = require('ut-function.flatten');
const template = require('ut-function.template');
const set = require('lodash.set');
const get = require('lodash.get');
const xml2js = require('xml2js');
const xmlParser = new xml2js.Parser({
charkey: 'text',
mergeAttrs: true,
explicitArray: false,
tagNameProcessors: [xml2js.processors.stripPrefix]
});
module.exports = (xmlTemplate, maxDepth = 50) => {
const fn = (async() => template(
JSON.stringify(
Object
.entries(flatten(await xmlParser.parseStringPromise(xmlTemplate), maxDepth))
.reduce((prev, [name, value]) => {
const path = (typeof value.match === 'function') && value.match(/^\${(.*)}$/);
if (path) set(prev, path[1], '$' + `{ut.get(xml, '${name}')}`);
return prev;
}, {})
), ['xml'], {get}, 'json'))();
return async(xml, json) => {
const result = (await fn)(typeof xml === 'string' ? await xmlParser.parseStringPromise(xml) : await xml);
return json ? result : JSON.parse(result);
};
};