UNPKG

succulent

Version:

Powerful and easy runtime type checking

24 lines 891 B
import { Schema } from "../schema.js"; function isEnumMemberName(x, enumObject) { // @ts-expect-error - This is some real fun voodoo :) return x in enumObject && typeof enumObject[enumObject[x]] !== "number"; } function enumKeys(enumObject) { return Object.keys(enumObject).filter((key) => isEnumMemberName(key, enumObject)); } /** * @remarks * This `Schema` is a little different from the rest, in that it assumes you are using * TypeScript, *and* that you have already defined a TypeScript `enum`. */ export function $enum(enumObject, options = {}) { const keys = enumKeys(enumObject); const values = new Set(keys.map((key) => enumObject[key])); return new Schema((x) => values.has(x), { displayName: options.displayName ?? `enum { ${keys.join(", ")} }`, *iter() { yield* values; }, }); } //# sourceMappingURL=enum.js.map