sec-edgar-api
Version:
Fetch and parse SEC earnings reports and other filings. Useful for financial analysis.
50 lines (49 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitValueBetweenReports = exports.getRoundToPlaces = exports.round = exports.getSingleNullKey = void 0;
function getSingleNullKey(obj) {
var singleNullKey = null;
for (var key in obj) {
if (obj[key] !== null)
continue;
if (singleNullKey === null)
singleNullKey = key;
else
return null;
}
return singleNullKey;
}
exports.getSingleNullKey = getSingleNullKey;
function round(num, places) {
return Math.round(num / places) * places;
}
exports.round = round;
/**
* returns the number of places the number is rounded to
*/
function getRoundToPlaces(num) {
if (num.toString().endsWith('000000'))
return 1000000;
if (num.toString().endsWith('000'))
return 1000;
return 1;
}
exports.getRoundToPlaces = getRoundToPlaces;
/**
* distributes a value between each report evenly, and rounds to the nearest roundToPlaces. gives remainder to last report
*/
function splitValueBetweenReports(params) {
var _a;
var key = params.key, value = params.value, roundToPlaces = params.roundToPlaces;
var reports = params.reports;
var remainder = ((value / roundToPlaces) % reports.length) * roundToPlaces;
var valueToDistribute = (value - remainder) / reports.length;
reports.forEach(function (report) {
var _a;
var diffAdded = round(((_a = report[key]) !== null && _a !== void 0 ? _a : 0) + valueToDistribute, roundToPlaces);
report[key] = diffAdded;
});
var reportLast = reports[reports.length - 1];
reportLast[key] = ((_a = reportLast[key]) !== null && _a !== void 0 ? _a : 0) + remainder;
}
exports.splitValueBetweenReports = splitValueBetweenReports;