All files / src/data-types enum-type.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                        89x 89x 174x           89x 89x      
import { AbstractDataType } from './abstract-data-type.ts';
 
/** Arguments of `DataTypes.ENUM`: values, arrays of values or `{ values }`. */
type EnumArgument = string | readonly string[] | Readonly<{ values: readonly string[] }>;
 
/**
 * Enumerated values (`DataTypes.ENUM`).
 *
 * @internal
 */
export class EnumType extends AbstractDataType {
  /** Allowed values. */
  readonly values: readonly string[];
  /** Arguments the type was created with (v6 `options`). */
  readonly options: Readonly<{ values: readonly string[] }>;
 
  /**
   * @param args - Values, arrays of values or `{ values }`, in any combination.
   */
  constructor(...args: readonly EnumArgument[]) {
    super('ENUM', 'ENUM');
    this.values = args.flatMap((arg) =>
      typeof arg === 'string'
        ? [arg]
        : Array.isArray(arg)
          ? arg
          : (arg as Readonly<{ values: readonly string[] }>).values,
    );
    this.options = Object.freeze({ values: this.values });
    Object.freeze(this);
  }
}