UNPKG

sec-edgar-api

Version:

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

136 lines (135 loc) 6.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ReportResolvable = /** @class */ (function () { function ReportResolvable(args) { this.calculationMap = {}; this.keyStack = new Set(); this.calculatedValues = new Map(); this.usedGroupIndexesByKey = new Map(); this.depth = 0; var report = args.report, calculationMap = args.calculationMap; this.calculationMap = calculationMap !== null && calculationMap !== void 0 ? calculationMap : {}; this.report = report; this.usedGroupIndexesByKey = new Map(); } ReportResolvable.prototype.getMap = function () { return this.calculationMap; }; ReportResolvable.prototype.clearCache = function () { this.calculatedValues.clear(); }; /** * Used when testing to remove groups that are not being used. */ ReportResolvable.prototype.getUsedGroupIndexesByKey = function () { var obj = {}; this.usedGroupIndexesByKey.forEach(function (indexes, key) { return (obj[key] = Array.from(indexes)); }); return obj; }; /** * Gets value from report, or calculates it using calculationMap. */ ReportResolvable.prototype.get = function (key) { this.depth = 0; return this._get(key); }; /** * Returns 0 for non-numeric values */ ReportResolvable.prototype.getNumber = function (key) { return Number(this.get(key)) || 0; }; ReportResolvable.prototype.getCalculatedFromGroup = function (params) { this.depth = 0; return this._getCalculatedFromGroup(params); }; ReportResolvable.prototype._get = function (key, parentKey) { var _a, _b; return (_a = this.report[key]) !== null && _a !== void 0 ? _a : ((_b = this._getCalculated(key, parentKey)) !== null && _b !== void 0 ? _b : {}).value; }; ReportResolvable.prototype._getCalculatedFromGroup = function (params, parentKey, skipSingleMatch) { var _a; if (skipSingleMatch === void 0) { skipSingleMatch = false; } var group = params.group, _b = params.breakIfMissingRequiredKey, breakIfMissingRequiredKey = _b === void 0 ? true : _b; var calculation = group.calculation; var hasAtLeastOneKey = false; var isMissingRequiredKey = false; var missingKeys = []; var finalSum = 0; for (var _i = 0, calculation_1 = calculation; _i < calculation_1.length; _i++) { var item = calculation_1[_i]; if (skipSingleMatch && calculation.length < 2) continue; var childKey = item.key, weight = item.weight, isRequired = item.isRequired; var value = (_a = this._get(childKey, parentKey)) !== null && _a !== void 0 ? _a : null; var hasKey = typeof value === 'number'; hasAtLeastOneKey = hasAtLeastOneKey || hasKey; isMissingRequiredKey = isMissingRequiredKey || (isRequired && !hasKey); if (!hasKey) { missingKeys.push(childKey); } if (isMissingRequiredKey && breakIfMissingRequiredKey) { break; } var valueAdjusted = (Number(value) || 0) * weight; finalSum += valueAdjusted; } return { hasAtLeastOneKey: hasAtLeastOneKey, isMissingRequiredKey: isMissingRequiredKey, missingKeys: missingKeys, value: hasAtLeastOneKey ? finalSum : null }; }; /** * @param skipSingleMatch When true, skips groups that match only one key. Used for testing groups. */ ReportResolvable.prototype.getCalculated = function (key, skipSingleMatch) { var _a; if (skipSingleMatch === void 0) { skipSingleMatch = false; } this.depth = 0; return (_a = this._getCalculated(key, undefined, skipSingleMatch)) !== null && _a !== void 0 ? _a : { value: null, groupIndex: -1 }; }; ReportResolvable.prototype._getCalculated = function (key, parentKey, skipSingleMatch) { var _a, _b, _c, _d, _e; if (skipSingleMatch === void 0) { skipSingleMatch = false; } this.depth++; if (this.report[key] !== undefined && !skipSingleMatch) { return { value: this.report[key], groupIndex: -1 }; } if (this.calculatedValues.has(key)) { var calculatedValue = this.calculatedValues.get(key); var calculatedLength = (_b = (_a = this.calculationMap[key]) === null || _a === void 0 ? void 0 : _a.groups[calculatedValue === null || calculatedValue === void 0 ? void 0 : calculatedValue.groupIndex]) === null || _b === void 0 ? void 0 : _b.calculation.length; if (!skipSingleMatch || calculatedLength > 1) return calculatedValue; } if (this.keyStack.has(key) || this.depth >= 50) { return null; } this.keyStack.add(key); var calculationOptions = (_c = this.calculationMap[key]) !== null && _c !== void 0 ? _c : { isTranslation: true, groups: [] }; var result = null; var calculationGroups = (_d = calculationOptions.groups) !== null && _d !== void 0 ? _d : []; for (var i = 0; i < calculationGroups.length; i++) { var group = calculationGroups[i]; var _f = this._getCalculatedFromGroup({ group: group, breakIfMissingRequiredKey: true, }, key, skipSingleMatch), hasAtLeastOneKey = _f.hasAtLeastOneKey, isMissingRequiredKey = _f.isMissingRequiredKey, value = _f.value; if (!hasAtLeastOneKey || isMissingRequiredKey || typeof value !== 'number') { continue; } var groupResult = { value: value, groupIndex: i }; // used for dev purposes to see which groups are being used. var indexBucket = ((_e = this.usedGroupIndexesByKey.get(key)) !== null && _e !== void 0 ? _e : this.usedGroupIndexesByKey.set(key, new Set()).get(key)); indexBucket.add(i); result = groupResult; break; } if (parentKey === undefined && !skipSingleMatch) { this.calculatedValues.set(key, result); } this.keyStack.delete(key); return result; }; ReportResolvable.prototype.toJSON = function () { return this.report; }; return ReportResolvable; }()); exports.default = ReportResolvable;