-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
33 lines (26 loc) · 883 Bytes
/
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
'use strict'
const convert = require('xml-js')
const validate = require('./src/validate')
const _declaration = require('./src/xml/declaration')
const FatturaElettronicaAttributes = require('./src/xml/FatturaElettronicaAttributes')
const { sanitizeObject } = require('./src/xml/sanitize')
const options = {
compact: true,
spaces: 2
}
module.exports = (invoice = {}, opt = {}) => {
const { returnXmlWithErrors = false } = opt
const result = validate(invoice, opt)
if (result.error && !returnXmlWithErrors) return result
const sanitizedInvoice = sanitizeObject(invoice)
const json = {
_declaration,
'p:FatturaElettronica': {
_attributes: FatturaElettronicaAttributes(invoice),
...sanitizedInvoice
}
}
return !returnXmlWithErrors
? convert.json2xml(json, options)
: { error: result.error, xml: convert.json2xml(json, options) }
}