UNPKG

houser-js-utils

Version:

A comprehensive collection of TypeScript utility functions for common development tasks including array manipulation, string processing, date handling, random number generation, validation, and much more.

153 lines (152 loc) 3.97 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const DeviceUtils = { /** * Adds a listener for online/offline status changes * @param callback - Function to call when status changes * @returns Function to remove the listener */ addOnlineStatusListener(callback) { const handleChange = () => callback(navigator.onLine); window.addEventListener("online", handleChange); window.addEventListener("offline", handleChange); return () => { window.removeEventListener("online", handleChange); window.removeEventListener("offline", handleChange); }; }, /** * Gets the battery level of the device * @returns Promise resolving to battery level (0-1) or null if not available */ async getBatteryLevel() { if (!("getBattery" in navigator)) return null; try { const battery = await navigator.getBattery(); return battery.level; } catch { return null; } }, /** * Gets the connection type of the device * @returns Connection type or null if not available */ getConnectionType() { if (!("connection" in navigator)) return null; return navigator.connection?.effectiveType || null; }, /** * Gets the device type (mobile, tablet, desktop) * @returns Device type */ getDeviceType() { const ua = navigator.userAgent; if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) { return "tablet"; } if (/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test( ua )) { return "mobile"; } return "desktop"; }, /** * Gets the number of CPU cores available * @returns Number of CPU cores */ getHardwareConcurrency() { return navigator.hardwareConcurrency || 1; }, /** * Gets the device language * @returns Device language */ getLanguage() { return navigator.language; }, /** * Gets the device memory in GB * @returns Device memory or null if not available */ getMemory() { if (!("deviceMemory" in navigator)) return null; return navigator.deviceMemory || null; }, /** * Gets the device orientation * @returns Device orientation */ getOrientation() { return window.innerHeight > window.innerWidth ? "portrait" : "landscape"; }, /** * Gets the device pixel ratio * @returns Device pixel ratio */ getPixelRatio() { return window.devicePixelRatio || 1; }, /** * Gets the device platform * @returns Device platform */ getPlatform() { if ("userAgentData" in navigator) { return navigator.userAgentData.platform || ""; } return navigator.userAgent; }, /** * Gets the screen dimensions * @returns Object containing screen width and height */ getScreenDimensions() { return { width: window.screen.width, height: window.screen.height }; }, /** * Gets the device vendor * @returns Device vendor */ getVendor() { if ("userAgentData" in navigator) { return navigator.userAgentData.brands[0]?.brand || ""; } return navigator.userAgent; }, /** * Checks if a string is a valid MAC address * @param mac - MAC address to validate * @returns True if valid, false otherwise */ isMacAddress(mac) { return /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(mac); }, /** * Checks if the device is a mobile device * @returns True if mobile device */ isMobile() { return this.getDeviceType() === "mobile"; }, /** * Checks if the device is online * @returns True if online */ isOnline() { return navigator.onLine; }, /** * Checks if the device supports touch events * @returns True if touch is supported */ supportsTouch() { return "ontouchstart" in window || navigator.maxTouchPoints > 0; } }; exports.DeviceUtils = DeviceUtils; //# sourceMappingURL=DeviceUtils.js.map