From 1ddf3df0832070ea13bee67e615edfc4842fb3d5 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 15 Feb 2023 11:21:14 +0000 Subject: [PATCH] init --- package.json | 3 +++ src/index.full.js | 0 src/index.js | 0 src/lib/xml2js.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 package.json create mode 100644 src/index.full.js create mode 100644 src/index.js create mode 100644 src/lib/xml2js.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..e151ae0 --- /dev/null +++ b/package.json @@ -0,0 +1,3 @@ +{ + "name": "@bytedo/alioss" +} \ No newline at end of file diff --git a/src/index.full.js b/src/index.full.js new file mode 100644 index 0000000..e69de29 diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/xml2js.js b/src/lib/xml2js.js new file mode 100644 index 0000000..dab9fe3 --- /dev/null +++ b/src/lib/xml2js.js @@ -0,0 +1,55 @@ +/** + * {alioss专用} + * @author yutent + * @date 2023/02/15 16:16:08 + */ + +function parseDOM(dom) { + let dict = {} + if (dom.children.length) { + dict[dom.tagName] = [...dom.children].map(it => parseDOM(it)) + } else { + dict[dom.tagName] = dom.textContent.trim() + } + return dict +} + +function getKey(obj) { + return Object.keys(obj)[0] +} + +function fixDict(dict) { + let k = getKey(dict) + let newDict = {} + + if (Array.isArray(dict[k])) { + for (let it of dict[k]) { + let _k = getKey(it) + let tmp = fixDict(it) + + if (newDict[_k]) { + if (!Array.isArray(newDict[_k])) { + newDict[_k] = [{ ...newDict[_k] }] + } + newDict[_k].push(tmp) + } else { + newDict[_k] = tmp + } + } + } else { + if (dict[k] === '') { + return '' + } else { + let v = +dict[k] + return v === v ? v : dict[k] + } + } + return newDict +} + +export default function (xml) { + let parser = new DOMParser() + let doc = parser.parseFromString(xml, 'application/xml').children[0] + + return fixDict(parseDOM(doc)) +}