Skip to main content

Platform

OS detection and cross-platform Mod key resolution.

import { detectPlatform, resolveMod, isMac } from '@gentleduck/vim/platform'
import type { Platform } from '@gentleduck/vim/platform'
import { detectPlatform, resolveMod, isMac } from '@gentleduck/vim/platform'
import type { Platform } from '@gentleduck/vim/platform'

Types

Platform

type Platform = 'mac' | 'windows' | 'linux'
type Platform = 'mac' | 'windows' | 'linux'

Functions

detectPlatform()

Detects the current operating system by inspecting navigator.userAgent. The result is cached after the first call.

Returns 'linux' when navigator is not available (SSR environments).

function detectPlatform(): Platform
function detectPlatform(): Platform

Example:

const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
const platform = detectPlatform() // 'mac' | 'windows' | 'linux'

resolveMod(platform?)

Returns the platform-specific modifier that the virtual Mod key resolves to.

function resolveMod(platform?: Platform): 'meta' | 'ctrl'
function resolveMod(platform?: Platform): 'meta' | 'ctrl'
PlatformReturns
'mac''meta'
'windows''ctrl'
'linux''ctrl'

Example:

resolveMod('mac')   // 'meta'
resolveMod('linux') // 'ctrl'
resolveMod()        // uses detectPlatform() automatically
resolveMod('mac')   // 'meta'
resolveMod('linux') // 'ctrl'
resolveMod()        // uses detectPlatform() automatically

isMac(platform?)

Convenience check for macOS.

function isMac(platform?: Platform): boolean
function isMac(platform?: Platform): boolean

Example:

if (isMac()) {
  console.log('Running on macOS')
}
if (isMac()) {
  console.log('Running on macOS')
}

_resetPlatformCache()

Clears the cached platform detection result. Only useful for testing.

function _resetPlatformCache(): void
function _resetPlatformCache(): void