UNPKG

@scale-codec/enum

Version:
31 lines (30 loc) 763 B
// tsc-build/enum/src/lib.js var variant = (...args) => new VariantImpl(...args); var VariantImpl = class { tag; __c; constructor(...args) { this.tag = args[0]; this.__c = args.length > 1 ? [args[1]] : null; } get unit() { return !this.__c; } get content() { return this.__c?.[0] ?? void 0; } as(tag) { if (this.tag !== tag) throw new Error(`Cannot cast enum variant "${this.tag}${this.unit ? "" : `(${String(this.content)})`}" as "${tag}"`); if (this.unit) throw new Error(`Cannot cast enum variant "${this.tag}" to its content because it is unit`); return this.content; } toJSON() { const { tag, content, unit } = this; return unit ? { tag } : { tag, content }; } }; export { variant };