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.
1 lines • 6.79 kB
Source Map (JSON)
{"version":3,"file":"DeviceUtils.mjs","sources":["../src/DeviceUtils.ts"],"sourcesContent":["/**\n * @module DeviceUtils\n * @description A collection of utility functions for device detection and capability checking.\n * @example\n * ```typescript\n * import { DeviceUtils } from 'houser-js-utils';\n *\n * // Check if running on mobile device\n * const isMobile = DeviceUtils.isMobile();\n *\n * // Get device type\n * const deviceType = DeviceUtils.getDeviceType();\n *\n * // Check for touch support\n * const hasTouch = DeviceUtils.isTouchDevice();\n * ```\n */\n\nexport const DeviceUtils = {\n /**\n * Adds a listener for online/offline status changes\n * @param callback - Function to call when status changes\n * @returns Function to remove the listener\n */\n addOnlineStatusListener(callback: (isOnline: boolean) => void): () => void {\n const handleChange = () => callback(navigator.onLine);\n window.addEventListener(\"online\", handleChange);\n window.addEventListener(\"offline\", handleChange);\n return () => {\n window.removeEventListener(\"online\", handleChange);\n window.removeEventListener(\"offline\", handleChange);\n };\n },\n\n /**\n * Gets the battery level of the device\n * @returns Promise resolving to battery level (0-1) or null if not available\n */\n async getBatteryLevel(): Promise<number | null> {\n if (!(\"getBattery\" in navigator)) return null;\n try {\n const battery = await (navigator as any).getBattery();\n return battery.level;\n } catch {\n return null;\n }\n },\n\n /**\n * Gets the connection type of the device\n * @returns Connection type or null if not available\n */\n getConnectionType(): string | null {\n if (!(\"connection\" in navigator)) return null;\n return (navigator as any).connection?.effectiveType || null;\n },\n\n /**\n * Gets the device type (mobile, tablet, desktop)\n * @returns Device type\n */\n getDeviceType(): \"mobile\" | \"tablet\" | \"desktop\" {\n const ua = navigator.userAgent;\n if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {\n return \"tablet\";\n }\n if (\n /Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(\n ua\n )\n ) {\n return \"mobile\";\n }\n return \"desktop\";\n },\n\n /**\n * Gets the number of CPU cores available\n * @returns Number of CPU cores\n */\n getHardwareConcurrency(): number {\n return navigator.hardwareConcurrency || 1;\n },\n\n /**\n * Gets the device language\n * @returns Device language\n */\n getLanguage(): string {\n return navigator.language;\n },\n\n /**\n * Gets the device memory in GB\n * @returns Device memory or null if not available\n */\n getMemory(): number | null {\n if (!(\"deviceMemory\" in navigator)) return null;\n return (navigator as any).deviceMemory || null;\n },\n\n /**\n * Gets the device orientation\n * @returns Device orientation\n */\n getOrientation(): \"portrait\" | \"landscape\" {\n return window.innerHeight > window.innerWidth ? \"portrait\" : \"landscape\";\n },\n\n /**\n * Gets the device pixel ratio\n * @returns Device pixel ratio\n */\n getPixelRatio(): number {\n return window.devicePixelRatio || 1;\n },\n\n /**\n * Gets the device platform\n * @returns Device platform\n */\n getPlatform(): string {\n if (\"userAgentData\" in navigator) {\n return (navigator as any).userAgentData.platform || \"\";\n }\n return navigator.userAgent;\n },\n\n /**\n * Gets the screen dimensions\n * @returns Object containing screen width and height\n */\n getScreenDimensions(): { width: number; height: number } {\n return {\n width: window.screen.width,\n height: window.screen.height,\n };\n },\n\n /**\n * Gets the device vendor\n * @returns Device vendor\n */\n getVendor(): string {\n if (\"userAgentData\" in navigator) {\n return (navigator as any).userAgentData.brands[0]?.brand || \"\";\n }\n return navigator.userAgent;\n },\n\n /**\n * Checks if a string is a valid MAC address\n * @param mac - MAC address to validate\n * @returns True if valid, false otherwise\n */\n isMacAddress(mac: string): boolean {\n return /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(mac);\n },\n\n /**\n * Checks if the device is a mobile device\n * @returns True if mobile device\n */\n isMobile(): boolean {\n return this.getDeviceType() === \"mobile\";\n },\n\n /**\n * Checks if the device is online\n * @returns True if online\n */\n isOnline(): boolean {\n return navigator.onLine;\n },\n\n /**\n * Checks if the device supports touch events\n * @returns True if touch is supported\n */\n supportsTouch(): boolean {\n return \"ontouch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