diff --git a/README.md b/README.md index 8279770..cf5c90c 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ if (isJsDom) { if (isDeno) { // do deno only stuff } + +if (isHappyDom) { + // do happy-dom only stuff +} ``` ES5 style import @@ -64,8 +68,12 @@ if (jsEnv.isJsDom) { if (jsEnv.isDeno) { // do deno only stuff } + +if (jsEnv.isHappyDom) { + // do happy DOM only stuff +} ``` ## License -MIT © Dineshkumar Pandiyan \ No newline at end of file +MIT © Dineshkumar Pandiyan diff --git a/src/index.d.ts b/src/index.d.ts index a461086..623a0e3 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -3,3 +3,4 @@ export declare const isWebWorker: boolean; export declare const isNode: boolean; export declare const isJsDom: boolean; export declare const isDeno: boolean; +export declare const isHappyDom: boolean; diff --git a/src/index.js b/src/index.js index 787dcda..cb2beff 100644 --- a/src/index.js +++ b/src/index.js @@ -21,9 +21,15 @@ const isJsDom = (navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom"))); +/** + * @see https://github.com/capricorn86/happy-dom/discussions/481 + */ +const isHappyDom = + typeof window !== "undefined" && typeof navigator !== "undefined" && typeof window.happyDOM !== "undefined"; + const isDeno = typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; -export { isBrowser, isWebWorker, isNode, isJsDom, isDeno }; +export { isBrowser, isWebWorker, isNode, isJsDom, isDeno, isHappyDom }; diff --git a/test/index.js b/test/index.js index 14cd4d9..77689ef 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ import { assert } from "chai"; -import { isBrowser, isNode, isWebWorker, isDeno, isJsDom } from "../src"; +import { isBrowser, isNode, isWebWorker, isDeno, isJsDom, isHappyDom } from "../src"; console.log({ isBrowser, @@ -7,6 +7,7 @@ console.log({ isWebWorker, isJsDom, isDeno, + isHappyDom, }); describe("Browser or Node.js", () => {