-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
87 lines (74 loc) · 2.36 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
//dependencies
// var fs = require('fs');
var xml2js = require('xml2js');
var path = require('path');
var async = require('async');
var xform = require(path.join(__dirname, 'src', 'xform'));
/**
* @module xformjs
* @type {Object}
* @version 0.1.0
* @public
*/
var xformjs = {};
/**
* @description convert xForm xml definition to JSON definition
* @param {String} xForm valid xForm definition
* @param {Function} done function to invoke on success or failure
* @return {Object} error object with errors found during conversion or
* JSON representaion of xForm
*
*/
xformjs.xform2json = function(xForm, done) {
//prepare xml2js convertor
var xmljs = new xml2js.Parser({
tagNameProcessors: [
xml2js.processors.stripPrefix //strip prefixes from tag names
],
attrNameProcessors: [
xml2js.processors.stripPrefix //strip prefixes from attribute names
],
valueProcessors: [
xml2js.processors.stripPrefix, //strip prefixes from value
xml2js.processors.parseBooleans // parse boolean value
],
explicitArray: false
});
//parse form
xmljs.parseString(xForm, function(error, xformJson) {
//backoff
if (error) {
done(error);
}
//continue parsing form
async.parallel({
title: function(next) {
next(null, xform.parseTitle(xformJson));
},
id: function(next) {
next(null, xform.parseId(xformJson));
},
version: function(next) {
next(null, xform.parseVersion(xformJson));
},
instanceName: function(next) {
next(null, xform.parseInstanceName(xformJson));
},
instance: function(next) {
next(null, xform.parseInstance(xformJson));
},
translations: function(next) {
next(null, xform.parseTranslations(xformJson));
},
questions: function(next) {
next(null, xform.parseQuestions(xformJson));
},
meta: function(next) {
next(null, xform.parseMeta());
}
}, done);
});
};
//export xformjs
module.exports = exports = xformjs;