diff options
-rw-r--r-- | cli/tests/unit/os_test.ts | 7 | ||||
-rw-r--r-- | cli/tsc/dts/lib.deno.ns.d.ts | 13 | ||||
-rw-r--r-- | runtime/js/30_os.js | 3 |
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, }; |