UNPKG

radius-read

Version:

Realtime Dashboard

99 lines 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UtilService = void 0; const tslib_1 = require("tslib"); const moment_1 = tslib_1.__importDefault(require("moment")); /** * UtilService is a utility service class that provides various helper methods for data manipulation and formatting. */ class UtilService { /** * Creates an instance of UtilService. */ constructor() { } /** * Gets a unique array of objects based on a specified property. * @param arrayObject * @param property * @returns */ static getUniqueArray(arrayObject = [], property) { return [...new Map(arrayObject.map((item) => [item[property], item])).values()]; } /** * Sorts an array of objects by a specified property in ascending order. * @param arrayObject * @param property * @returns */ static sortByProperty(arrayObject = [], property) { return arrayObject.sort((a, b) => (a[property] > b[property]) ? 1 : ((b[property] > a[property]) ? -1 : 0)); } ; /** * Sorts an array of objects by date in ascending order based on a specified property. * @param arrayObject * @param property * @returns */ static sortAscByDate(arrayObject = [], property) { return arrayObject.sort((a, b) => (0, moment_1.default)(a[property], 'MMM DD, YYYY, hh:mm:ss A').valueOf() - (0, moment_1.default)(b[property], 'MMM DD, YYYY, hh:mm:ss A').valueOf()); } ; /** * Sorts an array of objects by date in descending order based on a specified property. * @param arrayObject * @param property * @returns */ static sortDescByDate(arrayObject = [], property) { return arrayObject.sort((a, b) => (0, moment_1.default)(b[property], 'MMM DD, YYYY, hh:mm:ss A').valueOf() - (0, moment_1.default)(a[property], 'MMM DD, YYYY, hh:mm:ss A').valueOf()); } ; /** * Gets the duration between two dates in the format "HH:mm:ss" or "mm:ss" if hours are zero. * @param fromDate * @param toDate * @returns */ static getDuration(fromDate, toDate) { if (fromDate && toDate) { var startDate = new Date(fromDate); var endDate = new Date(toDate); var milisecondsDiff = endDate - startDate; var hours = Math.floor(milisecondsDiff / (1000 * 60 * 60)); var minutes = Math.floor(milisecondsDiff / (1000 * 60)) % 60; var seconds = Math.floor(milisecondsDiff / 1000) % 60; var duration = `${minutes.toLocaleString(undefined, { minimumIntegerDigits: 2 })}:${seconds.toLocaleString(undefined, { minimumIntegerDigits: 2 })}`; if (hours > 0) duration = `${hours.toLocaleString(undefined, { minimumIntegerDigits: 2 })}:${minutes.toLocaleString(undefined, { minimumIntegerDigits: 2 })}:${seconds.toLocaleString(undefined, { minimumIntegerDigits: 2 })}`; return duration; } return '---'; } ; /** * Gets the base URL for Socket.IO connections based on the provided port and SSL status. * @param port * @param isSSL * @returns */ static getSocketIoBaseURL(port, isSSL) { var protocol = isSSL ? 'https:' : 'http:'; return `${protocol}//127.0.0.1:${port}`; } /** * Gets the Bearer token from the Authorization header of an Express request. * @param req * @returns */ static getBearerToken(req) { const authHeader = req.headers['authorization']; if (authHeader && typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) { return authHeader.slice(7); // Remove "Bearer " prefix } return null; } } exports.UtilService = UtilService; //# sourceMappingURL=util.service.js.map