UNPKG

playwright-sniff

Version:

Monitoring library for Playwright that measures action times, catches showstoppers and generates reports

103 lines 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VERSION = void 0; exports.cleanErrorMessage = cleanErrorMessage; exports.calculateAverage = calculateAverage; exports.calculatePercentile = calculatePercentile; exports.formatDuration = formatDuration; exports.formatDate = formatDate; exports.createDirectory = createDirectory; exports.defaultLogger = defaultLogger; exports.generateTimestamp = generateTimestamp; const fs_1 = __importDefault(require("fs")); const types_1 = require("./types"); /** * Current version of the library */ exports.VERSION = '1.0.0'; /** * Clean an error message by removing ANSI codes and truncating */ function cleanErrorMessage(raw) { const message = raw instanceof Error ? raw.message : String(raw); // Remove ANSI color codes const stripped = message.replace(/\u001b\[[0-9;]*m/g, ''); return stripped; // Take only the first line return stripped.split('\n')[0]; } /** * Calculate the average of an array of numbers */ function calculateAverage(values) { if (!values.length) return null; const total = values.reduce((acc, val) => acc + val, 0); return (total / values.length).toFixed(2); } /** * Calculate percentile value from an array of numbers */ function calculatePercentile(values, percentile) { if (!values.length) return 0; const sorted = [...values].sort((a, b) => a - b); const index = Math.ceil((percentile / 100) * sorted.length) - 1; return sorted[Math.max(0, index)]; } /** * Format a duration in milliseconds to a readable string */ function formatDuration(ms) { if (ms < 1000) return `${ms}ms`; return `${(ms / 1000).toFixed(2)}s`; } /** * Format a date to a readable string */ function formatDate(date) { return date.toISOString().replace('T', ' ').substring(0, 19); } /** * Create a directory if it doesn't exist */ function createDirectory(dirPath) { if (!fs_1.default.existsSync(dirPath)) { fs_1.default.mkdirSync(dirPath, { recursive: true }); } } /** * Default logger function */ function defaultLogger(message, level) { const timestamp = new Date().toISOString(); switch (level) { case types_1.LogLevel.ERROR: console.error(`[${timestamp}] ERROR: ${message}`); break; case types_1.LogLevel.WARN: console.warn(`[${timestamp}] WARN: ${message}`); break; case types_1.LogLevel.INFO: console.info(`[${timestamp}] INFO: ${message}`); break; case types_1.LogLevel.DEBUG: console.debug(`[${timestamp}] DEBUG: ${message}`); break; } } /** * Generate a timestamp string for filenames */ function generateTimestamp() { return new Date().toISOString() .replace(/:/g, '-') .replace(/\./g, '-') .replace('T', '_') .replace('Z', ''); } //# sourceMappingURL=utils.js.map