Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

options.ts

28 lines1.1 KB

1import * as Duration from "effect/Duration";
2import * as Option from "effect/Option";
3​
4/** The duration `input` is, or an error that names `what` and shows one that reads. */
5export const durationOf = (what: string, input: Duration.Input): Duration.Duration =>
6 Option.getOrThrowWith(
7 Duration.fromInput(input),
8 () =>
9 new Error(
10 `${what} cannot read ${JSON.stringify(input)} as a duration. Write one such as "5 minutes".`,
11 ),
12 );
13​
14/**
15 * Refuses an option the call does not take. A workflow file is not type-checked, so a misspelt or
16 * renamed option would otherwise be dropped without a word.
17 */
18export const checkOptions = (
19 what: string,
20 options: object | undefined,
21 allowed: ReadonlyArray<string>,
22) => {
23 if (options === undefined || options === null) return;
24 const unknown = Object.keys(options).filter((key) => !allowed.includes(key));
25 if (unknown.length === 0) return;
26 const listed = (keys: ReadonlyArray<string>) => keys.map((key) => `\`${key}\``).join(", ");
27 throw new Error(`${what} does not take ${listed(unknown)}. It takes ${listed(allowed)}.`);
28};