summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-01-09 20:19:55 +0100
committerGitHub <noreply@github.com>2023-01-09 20:19:55 +0100
commite6c49d14b1ea6331d2e13f7a0b74a0e41c142596 (patch)
treed045ba357a4fe37c1bb9d9b7596d3c69085d8233
parentfa175d8cdaeff37daa68cadea5b0e59fa794a0c2 (diff)
feat(runtime/os): add `Deno.env.has()` (#17315)
-rw-r--r--cli/tests/unit/os_test.ts7
-rw-r--r--cli/tsc/dts/lib.deno.ns.d.ts13
-rw-r--r--runtime/js/30_os.js3
3 files changed, 23 insertions, 0 deletions
diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts
index 81f912bcc..667c3cc81 100644
--- a/cli/tests/unit/os_test.ts
+++ b/cli/tests/unit/os_test.ts
@@ -26,6 +26,13 @@ Deno.test({ permissions: { env: true } }, function deleteEnv() {
assertEquals(Deno.env.get("TEST_VAR"), undefined);
});
+Deno.test({ permissions: { env: true } }, function hasEnv() {
+ Deno.env.set("TEST_VAR", "A");
+ assert(Deno.env.has("TEST_VAR"));
+ Deno.env.delete("TEST_VAR");
+ assert(!Deno.env.has("TEST_VAR"));
+});
+
Deno.test({ permissions: { env: true } }, function avoidEmptyNamedEnv() {
assertThrows(() => Deno.env.set("", "v"), TypeError);
assertThrows(() => Deno.env.set("a=a", "v"), TypeError);
diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts
index ab53a064e..5941a5d57 100644
--- a/cli/tsc/dts/lib.deno.ns.d.ts
+++ b/cli/tsc/dts/lib.deno.ns.d.ts
@@ -1231,6 +1231,19 @@ declare namespace Deno {
*/
delete(key: string): void;
+ /** Check whether an environment variable is present or not.
+ *
+ * ```ts
+ * Deno.env.set("SOME_VAR", "Value");
+ * Deno.env.has("SOME_VAR"); // outputs true
+ * ```
+ *
+ * Requires `allow-env` permission.
+ *
+ * @tags allow-env
+ */
+ has(key: string): boolean;
+
/** Returns a snapshot of the environment variables at invocation as a
* simple object of keys and values.
*
diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js
index f81a5c08d..8069c3a34 100644
--- a/runtime/js/30_os.js
+++ b/runtime/js/30_os.js
@@ -96,6 +96,9 @@
return ops.op_env();
},
set: setEnv,
+ has(key) {
+ return getEnv(key) !== undefined;
+ },
delete: deleteEnv,
};