All files / json-crdt-extensions/peritext/slice NestedType.ts

93.75% Statements 15/16
72.72% Branches 8/11
100% Functions 3/3
100% Lines 13/13

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  65x   65x     65x 120x       157x 157x 157x   34x   34x 34x   34x 34x                     90x      
import type {ArrApi} from '../../../json-crdt/model';
import {NestedTag} from './NestedTag';
import type {PersistedSlice} from './PersistedSlice';
import * as schema from './schema';
import type {SliceTypeSteps} from './types';
 
export class NestedType<T = string> {
  constructor(protected slice: PersistedSlice<T>) {}
 
  /** Enforces slice type to be an "arr" of tags. */
  public asArr(): ArrApi {
    const {slice} = this;
    const api = slice.typeApi();
    if (api && api.node.name() === 'arr') return api as unknown as ArrApi;
    let type: unknown;
    Iif (!api) type = [0];
    else {
      type = api.view();
      if (!Array.isArray(type)) type = typeof type === 'number' || typeof type === 'string' ? [type] : [0];
    }
    slice.update({type: schema.type(type as SliceTypeSteps)});
    return slice.typeApi() as unknown as ArrApi;
  }
 
  /**
   * Nested tag API for nested block types.
   *
   * @param index The position of the tag in the type array. If not specified,
   *     returns the last tag (255). Maximum index is 255.
   * @returns Slice tag API for the given position.
   */
  public tag(index: number = 255): NestedTag<T> {
    return new NestedTag<T>(this, index);
  }
}