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

100% Statements 12/12
83.33% Branches 5/6
100% Functions 4/4
100% Lines 11/11

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 34 35 36 37      139x                         679x 679x               52x 52x 52x 52x 52x 1x 1x   52x      
import { AbstractDataType, type DataType, type DataTypeInput } from './abstract-data-type.ts';
import { ArrayLiteralParser } from './array-literal-parser.ts';
 
const arrayLiteral = new ArrayLiteralParser();
 
/**
 * `type[]` (`DataTypes.ARRAY(DataTypes.TEXT)`).
 *
 * @internal
 */
export class ArrayType extends AbstractDataType {
  /** Element type. */
  readonly subtype: DataType;
  /** Arguments the type was created with (v6 `options`): the element type. */
  readonly options: Readonly<{ type: DataType }>;
  /** Converts a driver array literal into an array of strings. */
  readonly parse = (value: unknown): unknown =>
    typeof value === 'string' ? arrayLiteral.parse(value) : value;
  /** Serializes every element; present only when the element type serializes values. */
  declare readonly serialize?: (value: unknown) => unknown;
 
  /**
   * @param subtype - Element type.
   */
  constructor(subtype: DataTypeInput) {
    const element = AbstractDataType.from(subtype);
    super('ARRAY', `${element.sqlType}[]`);
    this.subtype = element;
    this.options = Object.freeze({ type: element });
    if (element.serialize !== undefined) {
      this.serialize = (value: unknown) =>
        Array.isArray(value) ? value.map((item) => element.serialize?.(item)) : value;
    }
    Object.freeze(this);
  }
}