UNPKG

sec-edgar-api

Version:

Fetch and parse SEC earnings reports and other filings. Useful for financial analysis.

102 lines (101 loc) 5.36 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentNode = void 0; var HRNode_1 = require("./HRNode"); var NonTableNode_1 = require("./NonTableNode"); var TableNode_1 = require("./TableNode"); var XMLNode_1 = require("./XMLNode"); var DocumentNode = /** @class */ (function (_super) { __extends(DocumentNode, _super); function DocumentNode() { return _super !== null && _super.apply(this, arguments) || this; } DocumentNode.prototype.parseTables = function () { var _a, _b, _c, _d; var nodes = this.getChildren(); var tables = nodes.filter(function (child) { return child instanceof TableNode_1.TableNode; }); var curSectionIndex = 0; var sectionIndexByTable = new Map(); var tableDataArr = []; // map the section index of each table. new section starts for each <hr> tag. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) { var node = nodes_1[_i]; if (node instanceof TableNode_1.TableNode) { sectionIndexByTable.set(node, curSectionIndex); } else if (node instanceof HRNode_1.HRNode) { curSectionIndex++; } } var _loop_1 = function (table) { table.mergeHeader(); table.removeEmptyTopRows(); var siblingsPrev = table.getSiblings({ dir: 'previous', stopAtType: NonTableNode_1.NonTableNode, includeStopAtType: true, }); var siblingsNext = table.getSiblings({ dir: 'next', stopAtType: NonTableNode_1.NonTableNode, includeStopAtType: true }); // set the title based on the bold text in the previous siblings var title = siblingsPrev.map(function (s) { return s.extractBold(); }).join(' | '); var textBefore = siblingsPrev.map(function (s) { return s.getText(); }).join('\n'); var textAfter = siblingsNext.map(function (s) { return s.getText(); }).join('\n'); var header = table.getHeaderRow(); var headerTable = header === null || header === void 0 ? void 0 : header.toTable(); var topRow = table.getChildren()[0]; var topRowTable = !header ? topRow === null || topRow === void 0 ? void 0 : topRow.toTable() : null; var prevTable = tableDataArr[tableDataArr.length - 1]; var isSameTableSize = prevTable ? ((_a = prevTable === null || prevTable === void 0 ? void 0 : prevTable.rows[0]) === null || _a === void 0 ? void 0 : _a.length) === ((_b = topRowTable === null || topRowTable === void 0 ? void 0 : topRowTable[0]) === null || _b === void 0 ? void 0 : _b.length) : false; var isSameSection = prevTable ? prevTable.sectionIndex === sectionIndexByTable.get(table) : false; var isSharedHeader = !!topRowTable && table.getTitle() === '' && isSameTableSize && isSameSection; // some tables will be in the same section directly below another table and expected to have the same header. if (isSharedHeader) { topRowTable.unshift(__spreadArray([], prevTable.rows[0], true)); table.setTitle(prevTable.title); } var rows = (_c = headerTable !== null && headerTable !== void 0 ? headerTable : topRowTable) !== null && _c !== void 0 ? _c : []; if (rows.length === 0) return "continue"; tableDataArr.push({ title: title.trim(), sectionIndex: (_d = sectionIndexByTable.get(table)) !== null && _d !== void 0 ? _d : -1, hasHeader: Boolean(header) || isSharedHeader, textBefore: textBefore, textAfter: textAfter, rows: rows.filter(function (r) { return r.length === rows[0].length; }), }); }; // create table arrays for (var _e = 0, tables_1 = tables; _e < tables_1.length; _e++) { var table = tables_1[_e]; _loop_1(table); } return tableDataArr; }; return DocumentNode; }(XMLNode_1.XMLNode)); exports.DocumentNode = DocumentNode;