All files / json-type/src/value Value.ts

87.5% Statements 21/24
62.5% Branches 5/8
85.71% Functions 6/7
94.73% Lines 18/19

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 38 39 40 41 4237x 37x       37x 2x 2x 2x 1x 1x 1x   1x     37x   30x 30x               1x 1x       1x 1x             37x  
import {printTree} from 'tree-dump/lib/printTree';
import {printJson} from 'tree-dump/lib/printJson';
import type {Printable} from 'tree-dump';
import type {ResolveType, Type} from '../type/types';
 
const copyForPrint = (data: unknown): unknown => {
  Iif (typeof data === 'function') return '__fN---';
  Iif (Array.isArray(data)) return data.map(copyForPrint);
  if (data && typeof data === 'object') {
    const res: Record<string, unknown> = {};
    for (const k in data) res[k] = copyForPrint((data as any)[k]);
    return res;
  }
  return data;
};
 
export class Value<T extends Type = Type> implements Printable {
  constructor(
    public data: ResolveType<T>,
    public type?: T,
  ) {}
 
  public name(): string {
    return 'Value';
  }
 
  public toString(tab: string = ''): string {
    const type = this.type;
    return (
      this.name() +
      (type
        ? printTree(tab, [
            (tab) => type.toString(tab),
            (tab) => printJson(tab, copyForPrint(this.data)).replace(/"__fN---"/g, 'fn()'),
          ])
        : '')
    );
  }
}
 
export const unknown = (data: unknown): Value<Type> => new (Value as any)(data);