sec-edgar-api
Version:
Fetch and parse SEC earnings reports and other filings. Useful for financial analysis.
165 lines (164 loc) • 6.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLNode = void 0;
var XMLNode = /** @class */ (function () {
function XMLNode(args) {
this.parent = null;
this.previousSibling = null;
this.nextSibling = null;
this.children = [];
this.text = '';
this.attributesStr = '';
this.path = '';
var _a = args !== null && args !== void 0 ? args : {}, _b = _a.attributesStr, attributesStr = _b === void 0 ? '' : _b, _c = _a.path, path = _c === void 0 ? '' : _c;
var pathParts = path.split('.').filter(function (str) { return !str.includes(':'); });
this.attributesStr = attributesStr;
this.path = pathParts.join('.');
}
XMLNode.prototype.getSiblings = function (params) {
var _a = params.stopAtType, stopAtType = _a === void 0 ? XMLNode : _a, dir = params.dir, _b = params.includeStopAtType, includeStopAtType = _b === void 0 ? false : _b;
var isPrevious = dir === 'previous';
var siblings = [];
var nextSibling = isPrevious ? this.getPreviousSibling() : this.getNextSibling();
var i = 0;
while (nextSibling && !(nextSibling instanceof stopAtType)) {
siblings.push(nextSibling);
nextSibling = isPrevious
? nextSibling.getPreviousSibling()
: nextSibling.getNextSibling();
i++;
if (i > 1000)
throw new Error('infinite loop');
}
if (includeStopAtType && nextSibling instanceof stopAtType) {
siblings.push(nextSibling);
}
return siblings;
};
XMLNode.prototype.extractBold = function (str) {
var _a, _b;
if (str === void 0) { str = this.text; }
var boldText = (_b = (_a = str
.replace(/\n/g, '')
.replace(/\}\}\{\{/g, ' | ')
.replace(/ | /g, ' ')
.replace(/–/g, '- ')
.match(/(?<=\{\{).*?(?=\}\})/g)) === null || _a === void 0 ? void 0 : _a.join(' || ')) !== null && _b !== void 0 ? _b : '';
return boldText.replace(/\s+/g, ' ').trim();
};
XMLNode.prototype.parseValue = function (str) {
var _a;
if (str === void 0) { str = this.text; }
if (str === null)
return null;
var text = str
.replace(/ | |\n/g, ' ')
.replace(/®/g, '')
.replace(/–|—|—/g, '-')
.replace(/’|“|”|’/g, "'")
.replace(/(?<=\{\{).*(?=\}\})/g, function (match) { return "{{".concat(match.replace(/\{\{/g, '').replace(/\}\}/g, ''), "}}"); })
.replace(/\{\{+/g, '{{')
.replace(/\}\}+/g, '}}')
.replace(/\s+/, ' ')
.trim();
if (str.replace(/–|—|—/g, '-') === '-')
return '-';
if (text === '')
return null;
var colNum = text
.replace(/,|\(|\)|%/g, '')
.replace(/\{\{/g, '')
.replace(/\}\}/g, '')
.trim();
if (colNum === '-' || colNum === '$')
return null;
colNum = colNum.replace(/-|\$/g, '');
var hasNumBeforeParenthesis = Boolean(/\d+\s*(?=\()/.test(text));
colNum = hasNumBeforeParenthesis ? (_a = colNum.split(' ')[0]) === null || _a === void 0 ? void 0 : _a.trim() : colNum;
if (!isNaN(Number(colNum))) {
if (text.includes('%'))
return text.replace(/[^a-zA-Z\d\s:]/g, '') === '' ? null : text;
return (text.trim().includes('(') && !hasNumBeforeParenthesis) || text.includes('-')
? Number(colNum) * -1
: Number(colNum);
}
return text;
};
XMLNode.prototype.setPreviousSibling = function (node) {
var prevPreviousSibling = this.previousSibling;
this.previousSibling = node;
if ((prevPreviousSibling === null || prevPreviousSibling === void 0 ? void 0 : prevPreviousSibling.getNextSibling()) === this) {
prevPreviousSibling.setNextSibling(null);
}
if ((node === null || node === void 0 ? void 0 : node.getNextSibling()) !== this) {
node === null || node === void 0 ? void 0 : node.setNextSibling(this);
}
};
XMLNode.prototype.setNextSibling = function (node) {
var prevNextSibling = this.nextSibling;
this.nextSibling = node;
if ((prevNextSibling === null || prevNextSibling === void 0 ? void 0 : prevNextSibling.getPreviousSibling()) === this) {
prevNextSibling.setPreviousSibling(null);
}
if ((node === null || node === void 0 ? void 0 : node.getPreviousSibling()) !== this) {
node === null || node === void 0 ? void 0 : node.setPreviousSibling(this);
}
};
XMLNode.prototype.removeChild = function (node) {
this.children.splice(this.children.indexOf(node), 1);
if (node.getParent() === this) {
node.setParent(null);
}
};
XMLNode.prototype.setParent = function (node) {
var prevParent = this.parent;
this.parent = node;
if (prevParent === null || prevParent === void 0 ? void 0 : prevParent.getChildren().includes(this)) {
prevParent.removeChild(this);
}
if (!(node === null || node === void 0 ? void 0 : node.getChildren().includes(this))) {
node === null || node === void 0 ? void 0 : node.addChild(this);
}
};
XMLNode.prototype.getNextSibling = function () {
return this.nextSibling;
};
XMLNode.prototype.getPreviousSibling = function () {
return this.previousSibling;
};
XMLNode.prototype.getParent = function () {
return this.parent;
};
XMLNode.prototype.getAttributes = function () {
var _a;
var attributesObj = {};
(_a = this.attributesStr.match(/(\w+)=("[^"]*")/g)) === null || _a === void 0 ? void 0 : _a.forEach(function (attributeStr) {
var _a = attributeStr.split('='), key = _a[0], value = _a[1];
attributesObj[key.toLowerCase()] = value.replace(/"/g, '');
});
return attributesObj;
};
XMLNode.prototype.getAttributesStr = function () {
return this.attributesStr;
};
XMLNode.prototype.getChildren = function () {
return this.children;
};
XMLNode.prototype.addChild = function (node) {
this.children.push(node);
if (node.getParent() !== this) {
node.setParent(this);
}
};
XMLNode.prototype.getText = function () {
return this.text;
};
XMLNode.prototype.setText = function (text) {
this.text = text;
};
XMLNode.prototype.getPath = function () {
return this.path;
};
return XMLNode;
}());
exports.XMLNode = XMLNode;