Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit 0e39b7a

Browse files
committed
refactor(runtime): 🚨 Resolve cognitive complexity issue
1 parent 1268648 commit 0e39b7a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

‎packages/runtime/source/environment/environment.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,24 @@ export const IN_WEB_WORKER =
5151

5252
/** Receive the name _(in lowercase)_ of the currently running JavaScript environment. */
5353
export function getRuntimeEnvironmentName(): RuntimeEnvironment {
54-
if (IN_EDGE) return "edge";
55-
else if (IN_BUN) return "bun";
56-
else if (IN_HAPPY_DOM) return "happy-dom";
57-
else if (IN_JSDOM) return "jsdom";
58-
else if (IN_DENO) return "deno";
59-
else if (IN_BROWSER) return "browser";
60-
else if (IN_NODE) return "node";
61-
else if (IN_WEB_WORKER) return "web-worker";
62-
else if (IN_DOM) return "dom";
63-
else throw new RuntimeError(`Unrecognized JavaScript runtime environment!`);
54+
/** NOTE: Order matters! */
55+
const conditions = {
56+
"edge": IN_EDGE,
57+
"bun": IN_BUN,
58+
"happy-dom": IN_HAPPY_DOM,
59+
"jsdom": IN_JSDOM,
60+
"deno": IN_DENO,
61+
"browser": IN_BROWSER,
62+
"node": IN_NODE,
63+
"web-worker": IN_WEB_WORKER,
64+
"dom": IN_DOM,
65+
} as const;
66+
67+
for (const [name, isTrue] of Object.entries(conditions)) {
68+
if (isTrue) return name as RuntimeEnvironment;
69+
}
70+
71+
throw new RuntimeError(`Unrecognized JavaScript runtime environment!`);
6472
}
6573

6674
export function validateRuntimeEnvironmentName(name: string): asserts name is RuntimeEnvironment {

0 commit comments

Comments
 (0)