-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.js
61 lines (51 loc) · 1.57 KB
/
parse.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const util = require('util');
const xml4js = require('xml4js');
const camtNameMap = require('./name-map');
const versions = [
'001.01',
'001.02',
'001.03',
'001.04',
'001.05',
'001.06',
'001.08',
];
class CamtParser {
constructor(options = {}) {
this.parser = new xml4js.Parser(options);
this.options = options;
this.map = camtNameMap;
}
async init() {
await Promise.all(versions.map(version => {
const schemaFileName = __dirname + `/xsd/camt.053.${version}.xsd`;
return util.promisify(fs.readFile)(schemaFileName, 'utf-8')
.then(schema => {
const namespace = `urn:iso:std:iso:20022:tech:xsd:camt.053.${version}`;
return util.promisify(this.parser.addSchema).bind(this.parser)(namespace, schema);
});
}));
}
async parseString(xml) {
const result = await util.promisify(this.parser.parseString)(xml);
if (this.options.skipNameMapping) {
return result;
} else {
return this.mapNames(result);
}
}
mapNames(object) {
const copy = object.length ? [] : {};
for (let key in object) {
const newKey = this.map[key] || key;
if (typeof object[key] === 'object' && !util.isDate(object[key])) {
copy[newKey] = this.mapNames(object[key]);
} else {
copy[newKey] = object[key];
}
}
return copy;
}
}
module.exports = CamtParser;