UNPKG

geoshell

Version:

A CLI to fetch real-time geo-data from your terminal

181 lines (158 loc) 4.23 kB
/** * Helper utilities */ /** * Get country code from name * * @param {string} country - Country name * @returns {string} Country code */ function getCountryCode(country) { const countryCodes = { 'usa': 'US', 'united states': 'US', 'united states of america': 'US', 'america': 'US', 'canada': 'CA', 'germany': 'DE', 'deutschland': 'DE', 'france': 'FR', 'japan': 'JP', 'nippon': 'JP', 'brazil': 'BR', 'brasil': 'BR', 'uk': 'GB', 'united kingdom': 'GB', 'great britain': 'GB', 'britain': 'GB', 'england': 'GB', 'italy': 'IT', 'italia': 'IT', 'spain': 'ES', 'españa': 'ES', 'australia': 'AU', 'india': 'IN', 'china': 'CN', 'russia': 'RU', 'mexico': 'MX', 'netherlands': 'NL', 'holland': 'NL' }; return countryCodes[country.toLowerCase()] || country.toUpperCase().substring(0, 2); } /** * Validate and normalize country name * * @param {string} country - Country name * @returns {string} Normalized country name */ function validateCountryName(country) { if (!country || typeof country !== 'string') { throw new Error('Country name must be a non-empty string'); } return country.trim(); } /** * Validate year parameter * * @param {number} year - Year to validate * @returns {number} Validated year */ function validateYear(year) { const currentYear = new Date().getFullYear(); if (!Number.isInteger(year)) { throw new Error('Year must be an integer'); } if (year < 1900 || year > currentYear + 10) { throw new Error(`Year must be between 1900 and ${currentYear + 10}`); } return year; } /** * Validate forecast days parameter * * @param {number} days - Days to validate * @returns {number} Validated days */ function validateForecastDays(days) { if (!Number.isInteger(days)) { throw new Error('Forecast days must be an integer'); } if (days < 0 || days > 7) { throw new Error('Forecast days must be between 0 and 7'); } return days; } /** * Parse coordinate string into latitude and longitude * * @param {string} coordString - Coordinate string (lat,lon) * @returns {Array} [latitude, longitude] */ function parseCoordinates(coordString) { try { const parts = coordString.split(','); if (parts.length !== 2) { throw new Error('Coordinates must be in format "lat,lon"'); } const lat = parseFloat(parts[0].trim()); const lon = parseFloat(parts[1].trim()); if (isNaN(lat) || isNaN(lon)) { throw new Error('Coordinates must be numbers'); } if (lat < -90 || lat > 90) { throw new Error('Latitude must be between -90 and 90'); } if (lon < -180 || lon > 180) { throw new Error('Longitude must be between -180 and 180'); } return [lat, lon]; } catch (error) { throw new Error(`Invalid coordinates format: ${error.message}`); } } /** * Humanize number (e.g., 1500 -> 1.5K) * * @param {number} number - Number to humanize * @returns {string} Humanized number */ function humanizeNumber(number) { if (typeof number !== 'number') { return String(number); } if (number >= 1_000_000_000) { return `${(number / 1_000_000_000).toFixed(1)}B`; } else if (number >= 1_000_000) { return `${(number / 1_000_000).toFixed(1)}M`; } else if (number >= 1_000) { return `${(number / 1_000).toFixed(1)}K`; } else { return String(number); } } /** * Truncate string with ellipsis if too long * * @param {string} text - Text to truncate * @param {number} maxLength - Maximum length * @returns {string} Truncated text */ function truncateString(text, maxLength = 50) { if (typeof text !== 'string') { text = String(text); } if (text.length <= maxLength) { return text; } return text.substring(0, maxLength - 3) + '...'; } module.exports = { getCountryCode, validateCountryName, validateYear, validateForecastDays, parseCoordinates, humanizeNumber, truncateString };