Skip to main content

System

Platform detection for client-side code — operating system, mobile platform, touch support. It reads navigator.userAgent, which is a heuristic and not a fact: browsers misreport, and iPadOS presents itself as macOS. Prefer feature detection where one exists, keep these for the cases where behaviour genuinely differs by platform (shortcut labels, store links), and use os or process on the server.

Example

import { isMacOs, isTouchDevice } from '@rtorcato/js-common/system'

// Label a shortcut the way this platform writes it.
const shortcut = isMacOs() ? '⌘K' : 'Ctrl+K'

if (isTouchDevice()) enableSwipeGestures()

Both read navigator, so they are heuristics: iPadOS reports itself as macOS, and a laptop with a touchscreen answers yes to isTouchDevice. Feature-detect where you can.

Import

import { getPlatform, isAndroid, isIOS } from '@rtorcato/js-common/system'

Exports

NameSummary
getPlatformReturns a string representing the detected platform.
isAndroidChecks if the device is running Android.
isIOSChecks if the device is running iOS.
isLinuxChecks if the current OS is Linux.
isMacOsChecks if the current OS is macOS.
isTouchDeviceChecks if the device supports touch events.
isWindowsChecks if the current OS is Windows.

See also

  • os — platform, arch, home and temp directories
  • node — Node version checks and optional requires
  • env — read env vars and branch on NODE_ENV
  • process — cwd, pid, uptime, exit, CI detection