1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

feat(runtime/os): add Deno.env.has() (#17315)

This commit is contained in:
Leo Kettmeir 2023-01-09 20:19:55 +01:00 committed by GitHub
parent fa175d8cda
commit e6c49d14b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View file

@ -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);

View file

@ -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.
*

View file

@ -96,6 +96,9 @@
return ops.op_env();
},
set: setEnv,
has(key) {
return getEnv(key) !== undefined;
},
delete: deleteEnv,
};