UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

118 lines (117 loc) 4.07 kB
"use strict"; /** * Time Formatting Utilities * * Provides human-readable time formatting for intervals and durations. * Used by audit logging and other time-based features. * * @example * ```typescript * import { formatTimeInterval } from './utils/time.js'; * * formatTimeInterval(86400000); // "daily" * formatTimeInterval(3600000); // "hourly" * formatTimeInterval(7200000); // "2-hourly" * formatTimeInterval(123456); // "123456ms" * ``` */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TIME_INTERVALS = void 0; exports.formatTimeInterval = formatTimeInterval; /** * Common time intervals in milliseconds * * These constants provide standard time intervals that can be used * throughout the codebase for consistency. * * @example * ```typescript * const dailyRotation = TIME_INTERVALS.DAY; // 86400000 * const hourlyCheck = TIME_INTERVALS.HOUR; // 3600000 * ``` */ exports.TIME_INTERVALS = { /** 1 second in milliseconds */ SECOND: 1000, /** 1 minute in milliseconds */ MINUTE: 60 * 1000, /** 1 hour in milliseconds */ HOUR: 60 * 60 * 1000, /** 1 day in milliseconds */ DAY: 24 * 60 * 60 * 1000, /** 1 week in milliseconds */ WEEK: 7 * 24 * 60 * 60 * 1000, }; /** * Format milliseconds into human-readable interval string * * Converts a time interval in milliseconds to a descriptive string * that's easier for humans to understand and for logging purposes. * * **Format Priority:** * 1. Weekly intervals (e.g., "weekly", "2-weekly") * 2. Daily intervals (e.g., "daily", "3-daily") * 3. Hourly intervals (e.g., "hourly", "12-hourly") * 4. Minute intervals (e.g., "minutely", "30-minutely") * 5. Second intervals (e.g., "every-second", "5-secondly") * 6. Custom milliseconds (e.g., "123456ms") * * @param ms - Milliseconds to format (undefined returns "unknown") * @returns Human-readable interval string * * @example Standard intervals * ```typescript * formatTimeInterval(86400000); // "daily" * formatTimeInterval(3600000); // "hourly" * formatTimeInterval(604800000); // "weekly" * formatTimeInterval(60000); // "minutely" * ``` * * @example Multiple intervals * ```typescript * formatTimeInterval(172800000); // "2-daily" (2 days) * formatTimeInterval(7200000); // "2-hourly" (2 hours) * formatTimeInterval(1800000); // "30-minutely" (30 minutes) * ``` * * @example Edge cases * ```typescript * formatTimeInterval(undefined); // "unknown" * formatTimeInterval(123456); // "123456ms" (custom) * formatTimeInterval(1000); // "every-second" * ``` */ function formatTimeInterval(ms) { if (ms === undefined || ms === null) return "unknown"; // Handle 0 explicitly (0 % anything === 0, so check first) if (ms === 0) return "0ms"; // Check for exact weekly intervals if (ms % exports.TIME_INTERVALS.WEEK === 0) { const weeks = ms / exports.TIME_INTERVALS.WEEK; return weeks === 1 ? "weekly" : `${weeks}-weekly`; } // Check for exact daily intervals if (ms % exports.TIME_INTERVALS.DAY === 0) { const days = ms / exports.TIME_INTERVALS.DAY; return days === 1 ? "daily" : `${days}-daily`; } // Check for exact hourly intervals if (ms % exports.TIME_INTERVALS.HOUR === 0) { const hours = ms / exports.TIME_INTERVALS.HOUR; return hours === 1 ? "hourly" : `${hours}-hourly`; } // Check for exact minute intervals if (ms % exports.TIME_INTERVALS.MINUTE === 0) { const minutes = ms / exports.TIME_INTERVALS.MINUTE; return minutes === 1 ? "minutely" : `${minutes}-minutely`; } // Check for exact second intervals if (ms % exports.TIME_INTERVALS.SECOND === 0) { const seconds = ms / exports.TIME_INTERVALS.SECOND; return seconds === 1 ? "every-second" : `${seconds}-secondly`; } // Fall back to milliseconds for custom intervals return `${ms}ms`; }