From dfd7c4183fc471da39c20b415c04e4db31deb802 Mon Sep 17 00:00:00 2001 From: Renato Pereira Back Date: Tue, 30 Mar 2021 09:57:11 -0300 Subject: [PATCH 1/2] Added option to close tags when serializing. --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++++- ofx.js | 33 ++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d1686ce..96b1262 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,50 @@ const body = { } }; -const ofx_string = ofx.serialize(header, body); +// By default, the serializer won't close the end node tags. To change that, pass { closeTags: true } as an option. + +const options = {closeTags: true}; +const ofx_string = ofx.serialize(header, body, options); console.log(ofx_string); + +``` + +Sample result with default `options`: + +```xml + + + + 0 + INFO + + 20210330 + POR + + Some Bank Institution + 012 + + + +``` + +Sample result with `options = {closeTags: true}`: + +```xml + + + + 0 + INFO + + 20210330 + POR + + Some Bank Institution + 012 + + + ``` ## Data ## diff --git a/ofx.js b/ofx.js index 64de7dc..4f79bd6 100644 --- a/ofx.js +++ b/ofx.js @@ -30,7 +30,7 @@ function parse(data) { // Parse the XML/SGML portion of the file into an object // Try as XML first, and if that fails do the SGML->XML mangling - let dataParsed = null; + let dataParsed; try { dataParsed = parseXml(content); } catch (e) { @@ -43,7 +43,12 @@ function parse(data) { return dataParsed; } -function serialize(header, body) { +const DEFAULT_OPTIONS = { + closeTags: false +} + +function serialize(header, body, options = {}) { + const normalizedOptions = {...DEFAULT_OPTIONS, ...options}; let out = ''; // header order could matter const headers = ['OFXHEADER', 'DATA', 'VERSION', 'SECURITY', 'ENCODING', 'CHARSET', @@ -54,11 +59,11 @@ function serialize(header, body) { }); out += '\n'; - out += objToOfx({ OFX: body }); + out += objToOfx({ OFX: body }, normalizedOptions); return out; } -const objToOfx = (obj) => { +const objToOfx = (obj, options) => { let out = ''; Object.keys(obj).forEach((name) => { @@ -66,16 +71,18 @@ const objToOfx = (obj) => { const start = `<${name}>`; const end = ``; - if (item instanceof Object) { - if (item instanceof Array) { - item.forEach((it) => { - out += `${start}\n${objToOfx(it)}${end}\n`; - }); - return; - } - return out += `${start}\n${objToOfx(item)}${end}\n` + if (!(item instanceof Object)) { + out += `${start}${item}${options.closeTags ? end : ''}\n`; + return; + } + + if (!(item instanceof Array)) { + return out += `${start}\n${objToOfx(item)}${end}\n` } - out += start + item + '\n'; + + item.forEach((it) => { + out += `${start}\n${objToOfx(it)}${end}\n`; + }); }); return out; From 5f99e4c8fb4477fd2333cc77e14be89f18f4ff8f Mon Sep 17 00:00:00 2001 From: Renato Pereira Back Date: Tue, 30 Mar 2021 10:07:27 -0300 Subject: [PATCH 2/2] Added options to inner calls of objToOfx. --- ofx.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ofx.js b/ofx.js index 4f79bd6..43d5316 100644 --- a/ofx.js +++ b/ofx.js @@ -77,11 +77,11 @@ const objToOfx = (obj, options) => { } if (!(item instanceof Array)) { - return out += `${start}\n${objToOfx(item)}${end}\n` + return out += `${start}\n${objToOfx(item, options)}${end}\n` } item.forEach((it) => { - out += `${start}\n${objToOfx(it)}${end}\n`; + out += `${start}\n${objToOfx(it, options)}${end}\n`; }); });