Skip to content

Instantly share code, notes, and snippets.

@arabold
Created April 13, 2020 01:52
Show Gist options
  • Save arabold/16905cc4cea827a5c1f544240b4503ce to your computer and use it in GitHub Desktop.
Save arabold/16905cc4cea827a5c1f544240b4503ce to your computer and use it in GitHub Desktop.
/**
* Returns `true` if code is running with JSDOM
*/
static get isJSDOM(): boolean {
return navigator?.userAgent?.includes("Node.js") || navigator?.userAgent?.includes("jsdom");
}
/**
* Returns `true` if code is running in a web browser environment
*/
static get isWebWorker(): boolean {
return global && typeof global.WorkerGlobalScope !== "undefined" && global.self instanceof global.WorkerGlobalScope;
}
/**
* Returns `true` if code is running in a web browser environment
*/
static get isBrowser(): boolean {
return (typeof window !== "undefined" || Logger.isWebWorker) && !Logger.isJSDOM;
}
/**
* Returns `true` if code is running in a web browser environment
*/
static get isIE(): boolean {
return Logger.isBrowser && document && window && document.documentMode && window.StyleMedia;
}
/**
* Returns `true` if code is running in React Native
*/
static get isReactNative(): boolean {
return Logger.isBrowser && navigator && navigator.product === "ReactNative";
}
/**
* Returns `true` if code is running in React Native and the Debugger is attached
*/
static get isReactNativeDebugger(): boolean {
return Logger.isReactNative && typeof global?.DedicatedWorkerGlobalScope !== "undefined";
}
/**
* Returns `true` if code is running in a Node.js-like environment (including Electron)
*/
static get isNode(): boolean {
// Note that Webpack et al are often adding process, module, require, etc.
return typeof process === "object" && typeof process.env === "object" && !Logger.isBrowser;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment