@flatfile/plugin-xml-extractor
Version:
A plugin for parsing .xml files in Flatfile.
107 lines (95 loc) • 3.6 kB
JavaScript
var $4PdbT$flatfileutilextractor = require("@flatfile/util-extractor");
var $4PdbT$remeda = require("remeda");
var $4PdbT$xmljsonformat = require("xml-json-format");
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
function $parcel$interopDefault(a) {
return a && a.__esModule ? a.default : a;
}
$parcel$export(module.exports, "XMLExtractor", () => $ddaaf84924067df1$export$f58147e05c4028bc);
$parcel$export(module.exports, "xmlParser", () => $ddaaf84924067df1$export$c7c816202c9450cf);
function $331a7cf52166f1a0$export$9df12741220b4999(buffer, options) {
const transform = options?.transform || ((value)=>value);
const data = $331a7cf52166f1a0$export$ddc9d26d0b3d592d(buffer.toString());
const headers = $331a7cf52166f1a0$export$33b1e1c1f48cebf6(data);
const sheetName = "Sheet1";
return {
[sheetName]: {
headers: headers,
data: data.map((row)=>{
return (0, $4PdbT$remeda.mapValues)(transform(row), (value)=>({
value: value
}));
})
}
};
}
function $331a7cf52166f1a0$export$29a2424249978a8f(obj) {
return obj.map((obj)=>{
return $331a7cf52166f1a0$var$mapObject(obj, (k, value)=>[
k,
{
value: value
}
]);
});
}
function $331a7cf52166f1a0$export$ddc9d26d0b3d592d(xml) {
const json = $331a7cf52166f1a0$export$6b2117be12a0a0bc((0, ($parcel$interopDefault($4PdbT$xmljsonformat)))(xml));
return json.map((obj)=>$331a7cf52166f1a0$var$flattenObject(obj));
}
function $331a7cf52166f1a0$export$33b1e1c1f48cebf6(arr) {
const keys = {};
arr.forEach((obj)=>{
Object.keys(obj).forEach((key)=>{
keys[key] = true;
});
});
return Object.keys(keys);
}
function $331a7cf52166f1a0$var$flattenAttributes(obj) {
if (obj && "_attributes" in obj) {
const attributes = $331a7cf52166f1a0$var$mapObject(obj._attributes, (k, v)=>[
`#${k}`,
v
]);
delete obj._attributes;
return {
...obj,
...attributes
};
}
return obj;
}
function $331a7cf52166f1a0$var$mapObject(obj, fn) {
const result = {};
Object.keys(obj).forEach((k)=>{
const [key, value] = fn(k, obj[k]);
result[key] = value;
});
return result;
}
function $331a7cf52166f1a0$var$flattenObject(input, prefix = "") {
const obj = $331a7cf52166f1a0$var$flattenAttributes(input);
if (!obj) return {};
const result = {};
Object.keys(obj).forEach((k)=>{
const pre = prefix ? prefix + (k.startsWith("#") || k === "_text" ? "" : "/") : "";
if (typeof obj[k] === "object") Object.assign(result, $331a7cf52166f1a0$var$flattenObject(obj[k], pre + k));
else result[pre + (k === "_text" && pre ? "" : k)] = obj[k];
});
return result;
}
function $331a7cf52166f1a0$export$6b2117be12a0a0bc(json) {
const key = Object.keys(json).find((k)=>k !== "_declaration");
if (!key) throw new Error("No root xml object found");
return Array.isArray(json[key]) ? json[key] : [
json[key]
];
}
const $ddaaf84924067df1$export$f58147e05c4028bc = (options)=>{
return (0, $4PdbT$flatfileutilextractor.Extractor)(".xml", "xml", (0, $331a7cf52166f1a0$export$9df12741220b4999), options);
};
const $ddaaf84924067df1$export$c7c816202c9450cf = (0, $331a7cf52166f1a0$export$9df12741220b4999);
//# sourceMappingURL=index.js.map