18 lines
551 B
JavaScript
Executable File
18 lines
551 B
JavaScript
Executable File
import { inspect } from 'util';
|
|
/**
|
|
* A union of literals. When you want to take an exact enum value.
|
|
*/
|
|
export function oneOf(literals) {
|
|
const examples = literals.map(x => inspect(x)).join(', ');
|
|
return {
|
|
async from(str) {
|
|
const value = literals.find(x => x === str);
|
|
if (!value) {
|
|
throw new Error(`Invalid value '${str}'. Expected one of: ${examples}`);
|
|
}
|
|
return value;
|
|
},
|
|
description: `One of ${examples}`,
|
|
};
|
|
}
|
|
//# sourceMappingURL=oneOf.js.map
|