mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 16:49:18 -05:00
feat(runtime/os): add Deno.env.has()
(#17315)
This commit is contained in:
parent
fa175d8cda
commit
e6c49d14b1
3 changed files with 23 additions and 0 deletions
|
@ -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);
|
||||
|
|
13
cli/tsc/dts/lib.deno.ns.d.ts
vendored
13
cli/tsc/dts/lib.deno.ns.d.ts
vendored
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -96,6 +96,9 @@
|
|||
return ops.op_env();
|
||||
},
|
||||
set: setEnv,
|
||||
has(key) {
|
||||
return getEnv(key) !== undefined;
|
||||
},
|
||||
delete: deleteEnv,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue