index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* globals WorkerGlobalScope, DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, ServiceWorkerGlobalScope */
  2. export const isBrowser = globalThis.window?.document !== undefined;
  3. export const isNode = globalThis.process?.versions?.node !== undefined;
  4. export const isBun = globalThis.process?.versions?.bun !== undefined;
  5. export const isDeno = globalThis.Deno?.version?.deno !== undefined;
  6. export const isElectron = globalThis.process?.versions?.electron !== undefined;
  7. export const isJsDom = globalThis.navigator?.userAgent?.includes('jsdom') === true;
  8. export const isWebWorker = typeof WorkerGlobalScope !== 'undefined' && globalThis instanceof WorkerGlobalScope;
  9. export const isDedicatedWorker = typeof DedicatedWorkerGlobalScope !== 'undefined' && globalThis instanceof DedicatedWorkerGlobalScope;
  10. export const isSharedWorker = typeof SharedWorkerGlobalScope !== 'undefined' && globalThis instanceof SharedWorkerGlobalScope;
  11. export const isServiceWorker = typeof ServiceWorkerGlobalScope !== 'undefined' && globalThis instanceof ServiceWorkerGlobalScope;
  12. // Note: I'm intentionally not DRYing up the other variables to keep them "lazy".
  13. const platform = globalThis.navigator?.userAgentData?.platform;
  14. export const isMacOs = platform === 'macOS'
  15. || globalThis.navigator?.platform === 'MacIntel' // Even on Apple silicon Macs.
  16. || globalThis.navigator?.userAgent?.includes(' Mac ') === true
  17. || globalThis.process?.platform === 'darwin';
  18. export const isWindows = platform === 'Windows'
  19. || globalThis.navigator?.platform === 'Win32'
  20. || globalThis.process?.platform === 'win32';
  21. export const isLinux = platform === 'Linux'
  22. || globalThis.navigator?.platform?.startsWith('Linux') === true
  23. || globalThis.navigator?.userAgent?.includes(' Linux ') === true
  24. || globalThis.process?.platform === 'linux';
  25. export const isIos = platform === 'iOS'
  26. || (globalThis.navigator?.platform === 'MacIntel' && globalThis.navigator?.maxTouchPoints > 1)
  27. || /iPad|iPhone|iPod/.test(globalThis.navigator?.platform);
  28. export const isAndroid = platform === 'Android'
  29. || globalThis.navigator?.platform === 'Android'
  30. || globalThis.navigator?.userAgent?.includes(' Android ') === true
  31. || globalThis.process?.platform === 'android';