-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow 'info' to not error in quarto check
- Loading branch information
Showing
4 changed files
with
65 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* dynamic.ts | ||
* | ||
* Tools for managing the interface between dynamic and static typing | ||
* in Quarto. | ||
* | ||
* Ideally, every usage of `any` or `as` would appear in this file. | ||
* | ||
* Copyright (C) 2024 Posit Software, PBC | ||
*/ | ||
|
||
import { DynamicTypeCheckError } from "../core/lib/error.ts"; | ||
|
||
export const checkStringEnum = <T extends string>( | ||
...values: T[] | ||
): (value: string) => T => { | ||
const valueSet: Set<string> = new Set(values); | ||
return (value: string): T => { | ||
if (!valueSet.has(value)) { | ||
throw new DynamicTypeCheckError( | ||
"Invalid value '" + value + "' (valid values are " + | ||
values.join(", ") + ").", | ||
); | ||
} | ||
return value as unknown as T; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* dynamic.test.ts | ||
* | ||
* Copyright (C) 2024 Posit Software, PBC | ||
* | ||
*/ | ||
|
||
import { checkStringEnum } from "../../../src/typing/dynamic.ts"; | ||
import { unitTest } from "../../test.ts"; | ||
import { assert, assertThrows } from "testing/asserts"; | ||
|
||
// deno-lint-ignore require-await | ||
unitTest("checkStringEnum", async () => { | ||
const check = checkStringEnum("a", "b", "c"); | ||
assert(check("a") === "a"); | ||
assert(check("b") === "b"); | ||
assert(check("c") === "c"); | ||
assertThrows(() => check("d"), Error, "Invalid value 'd' (valid values are a, b, c)."); | ||
}); |