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

65.38% Statements 17/26
35.71% Branches 5/14
50% Functions 1/2
65% Lines 13/20

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 3758x     58x 15053x     14471x   582x 582x 582x 582x 1287x 1287x     1287x         582x             58x              
import {SliceTypeName} from './constants';
import type {SliceType} from '../slice/types';
 
export const validateType = (type: SliceType) => {
  switch (typeof type) {
    case 'string':
    case 'number':
      return;
    case 'object': {
      Iif (!(type instanceof Array)) throw new Error('INVALID_TYPE');
      Iif (type.length > 32) throw new Error('INVALID_TYPE');
      const length = type.length;
      LOOP: for (let i = 0; i < length; i++) {
        const step = type[i];
        switch (typeof step) {
          case 'string':
          case 'number':
            continue LOOP;
          default:
            throw new Error('INVALID_TYPE');
        }
      }
      return;
    }
    default:
      throw new Error('INVALID_TYPE');
  }
};
 
export const formatType = (type: SliceType): string => {
  let formatted: string = JSON.stringify(type);
  const num = Number(type);
  Iif ((typeof type === 'number' || num + '' === type) && Math.abs(num) <= 64 && SliceTypeName[num])
    formatted = '<' + SliceTypeName[num] + '>';
  return formatted;
};