-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
77 lines (65 loc) · 1.53 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// deno-lint-ignore-file no-var no-explicit-any
declare var Bun: any;
declare var process: any;
/**
* Get the environment variable value
*
* @example
* ```ts
* import * as env from "jsr:@tsirysndr/env-js";
* console.log(env.get("HOME")); // /home/user
*/
export function get(key: string): string | undefined {
if (Object.keys(globalThis).includes("Bun")) {
return Bun.env[key];
}
if (Object.keys(globalThis).includes("process")) {
return process.env[key];
}
if (Object.keys(globalThis).includes("Deno")) {
return Deno.env.get(key);
}
return undefined;
}
/**
* Verify if the environment variable exists
*
* @example
* ```ts
* import * as env from "jsr:@tsirysndr/env-js";
* console.log(env.has("HOME")); // true
* ```
*/
export function has(key: string): boolean {
if (Object.keys(globalThis).includes("Bun")) {
return !!Bun.env[key];
}
if (Object.keys(globalThis).includes("process")) {
return !!process.env[key];
}
if (Object.keys(globalThis).includes("Deno")) {
return Deno.env.has(key);
}
return false;
}
/**
* Convert the environment variables to an object
*
* @example
* ```ts
* import * as env from "jsr:@tsirysndr/env-js";
* console.log(env.toObject());
* ```
*/
export function toObject(): Record<string, string> {
if (Object.keys(globalThis).includes("Bun")) {
return Bun.env;
}
if (Object.keys(globalThis).includes("process")) {
return process.env;
}
if (Object.keys(globalThis).includes("Deno")) {
return Deno.env.toObject();
}
return {};
}