xml2o
Version:
Helps you convert XML into an object for easy reading.
177 lines (170 loc) • 6.08 kB
JavaScript
var __create = Object.create;
var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
return this[key];
}
var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => {
var canCache = mod != null && typeof mod === "object";
if (canCache) {
var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod);
if (cached)
return cached;
}
target = mod != null ? __create(__getProtoOf(mod)) : {};
let to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target;
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: __accessProp.bind(mod, key),
enumerable: !0
});
if (canCache)
cache.set(mod, to);
return to;
}, __toCommonJS = (from) => {
var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc;
if (entry)
return entry;
if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") {
for (var key of __getOwnPropNames(from))
if (!__hasOwnProp.call(entry, key))
__defProp(entry, key, {
get: __accessProp.bind(from, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return __moduleCache.set(from, entry), entry;
}, __moduleCache;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: !0,
configurable: !0,
set: __exportSetter.bind(all, name)
});
};
// src/index.ts
var exports_src = {};
__export(exports_src, {
convertString: () => convertString,
convertStream: () => convertStream,
Node: () => Node,
Attribute: () => Attribute
});
module.exports = __toCommonJS(exports_src);
var import_sax = __toESM(require("sax"));
// src/node/index.ts
var text = Symbol();
class Node extends Array {
parent;
[text];
name;
local;
path;
prefix;
uri;
attributes;
constructor(opt, parent) {
super();
this.parent = parent;
this.name = opt.name, this.local = opt.local, this.prefix = opt.prefix, this.uri = opt.uri || "", this.attributes = Object.assign({}, ...Object.keys(opt.attributes).map((key) => ({
[key]: new Attribute(opt.attributes[key])
}))), this[text] = [];
}
get root() {
return this.parent ? this.parent.root : this;
}
get text() {
return this[text].join("");
}
static pushText(node, value) {
if (node[text].push(value), node.parent)
node.parent[text].push(value);
}
static addNode(parent, opt) {
let node = new Node(opt, parent);
return parent.push(node), node;
}
getAttributes(uri) {
return Object.assign({}, ...Object.keys(this.attributes).filter((key) => {
let attribute = this.attributes[key];
return uri && attribute.uri === uri || !uri && !attribute.uri;
}).map((key) => ({ [this.attributes[key].local]: this.attributes[key].value })));
}
getAttribute(name, uri) {
let attribute = this.getAttributeNode(name, uri) || { value: void 0 };
if (attribute)
return attribute.value;
}
getAttributeNode(name, uri) {
return Object.entries(this.attributes).filter(([, attribute]) => {
if (name && uri)
return name === attribute.local && uri === attribute.uri;
return attribute.name === name;
}).map(([, attribute]) => attribute).shift();
}
hasAttribute(name, uri) {
return !!this.getAttributeNode(name, uri);
}
query(path, uri) {
let result = [], searchUri = uri || "", match;
if (path === "/")
return [this];
if (path.indexOf("/") === 0) {
let parts = path.split("/").slice(1);
if (parts.length === 1)
match = (node) => parts[0] === node.local && searchUri === node.uri ? [node] : [];
else if (parts.length > 1)
match = (node) => parts[0] === node.local ? [...node.query("/".concat(parts.slice(1).join("/")), uri)] : [];
else
return [];
} else if (path.indexOf("/") !== -1) {
let parts = path.split("/");
match = (node) => parts[0] === node.local ? [...node.query(parts.slice(1).join("/"), uri)] : [...node.query(path, uri)];
} else
match = (node) => path === node.local && searchUri === node.uri ? [node, ...node.query(path, uri)] : [...node.query(path, uri)];
for (let node of this)
result.push(...match(node));
return result;
}
}
class Attribute {
name;
local;
value;
prefix;
uri;
constructor(opt) {
this.value = opt.value, this.prefix = opt.prefix, this.name = opt.name, this.local = opt.local, this.uri = opt.uri;
}
toString() {
return this.value;
}
}
// src/index.ts
function convert(parser, ready) {
let pointer;
return parser.on("opentag", (node) => {
if (!pointer)
pointer = new Node(node, null);
else
pointer = Node.addNode(pointer, node);
}), parser.on("closetag", (_node) => pointer.parent ? pointer = pointer.parent : null), parser.on("text", (value) => pointer && Node.pushText(pointer, value)), parser.on("cdata", (value) => Node.pushText(pointer, value)), new Promise((resolve, reject) => {
parser.on("end", () => resolve(pointer)), parser.on("error", (error) => reject(error)), ready();
});
}
function convertStream(stream) {
let parser = import_sax.default.createStream(!0, { xmlns: !0 });
return convert(parser, () => stream.pipe(parser));
}
function convertString(XMLString) {
let parser = import_sax.default.createStream(!0, { xmlns: !0 });
return convert(parser, () => parser.end(XMLString));
}
//# debugId=8D31E78F3EF932FE64756E2164756E21
//# sourceMappingURL=index.cjs.map