This repository was archived by the owner on Dec 30, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +18
-10
lines changed
packages/runtime/source/environment Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Original file line number Diff line number Diff line change @@ -51,16 +51,24 @@ export const IN_WEB_WORKER =
51
51
52
52
/** Receive the name _(in lowercase)_ of the currently running JavaScript environment. */
53
53
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!` ) ;
64
72
}
65
73
66
74
export function validateRuntimeEnvironmentName ( name : string ) : asserts name is RuntimeEnvironment {
You can’t perform that action at this time.
0 commit comments