UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

56 lines (55 loc) 2.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Algo_1 = __importDefault(require("../../core/Algo")); const Helper_1 = __importDefault(require("../../helper/Helper")); class DataframeManagerClass { fill(points, from, to, onlyLastValue, maintainLastValue) { const min = from !== null && from !== void 0 ? from : this.getMinDate(points); const max = to !== null && to !== void 0 ? to : this.getMaxDate(points); const orderPoints = points.length > 0 ? Algo_1.default.orderBy(points, 'x') : []; const filledPoints = []; const currentDate = new Date(min); while (currentDate <= max) { const monthKey = Helper_1.default.formatDateToYYYYMM(currentDate); filledPoints.push({ x: monthKey, y: 0 }); currentDate.setMonth(currentDate.getMonth() + 1); } for (let i = 0; i < orderPoints.length; i++) { const point = orderPoints[i]; const date = new Date(point.x); const filledPoint = filledPoints.find(x => x.x === Helper_1.default.formatDateToYYYYMM(date)); if (filledPoint) { if (!onlyLastValue) filledPoint.y += point.y; else filledPoint.y = point.y; if (maintainLastValue) { const index = filledPoints.findIndex(x => x.x === Helper_1.default.formatDateToYYYYMM(date)); for (let k = index; k < filledPoints.length; k++) { const nextFilledPoint = filledPoints[k]; nextFilledPoint.y = filledPoint.y; } } } } return filledPoints; } getMinDate(points) { if (!points || points.length === 0) { const currentDate = new Date(); return new Date(currentDate.getFullYear() - 1, currentDate.getMonth(), currentDate.getDate()); } return points.reduce((min, point) => (new Date(point.x) < min ? new Date(point === null || point === void 0 ? void 0 : point.x) : min), new Date(points[0].x)); } getMaxDate(points) { if (!points || points.length === 0) { return new Date(); } return points.reduce((max, point) => (new Date(point.x) > max ? new Date(point.x) : max), new Date(points[0].x)); } } const DataframeManager = new DataframeManagerClass(); exports.default = DataframeManager;