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

66.66% Statements 26/39
37.5% Branches 3/8
66.66% Functions 8/12
73.52% Lines 25/34

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9936x 36x 36x                               36x 36x     3x                                 7x 7x 7x 7x                     1x 1x             5x 5x 5x 5x 5x 5x 5x               5x 5x 5x 5x       5x                         1x      
import {ModuleType} from '../type/classes/ModuleType';
import {Value} from './Value';
import {FnValue} from './FnValue';
import type {Printable} from 'tree-dump/lib/types';
import type * as classes from '../type';
import type {TypeBuilder} from '../type/TypeBuilder';
 
export type UnObjType<T> = T extends classes.ObjType<infer U> ? U : never;
export type UnObjValue<T> = T extends ObjValue<infer U> ? U : never;
export type UnObjFieldTypeVal<T> = T extends classes.KeyType<any, infer U> ? U : never;
export type ObjFieldToTuple<F> = F extends classes.KeyType<infer K, infer V> ? [K, V] : never;
export type ToObject<T> = T extends [string, unknown][] ? {[K in T[number] as K[0]]: K[1]} : never;
export type ObjValueToTypeMap<F> = ToObject<{
  [K in keyof F]: ObjFieldToTuple<F[K]>;
}>;
 
export type Ensure<T, X> = T extends X ? T : X;
 
export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implements Printable {
  public static new = (system: ModuleType = new ModuleType()) => new ObjValue({}, system.t.obj);
 
  public get system(): classes.ModuleType {
    return (this.type as T).getSystem();
  }
 
  public get t(): TypeBuilder {
    return this.system.t;
  }
 
  public keys(): string[] {
    const type = this.type as T;
    return type.keys.map((field: classes.KeyType<string, any>) => field.key);
  }
 
  public get<K extends keyof ObjValueToTypeMap<UnObjType<T>>>(
    key: K,
  ): Value<
    ObjValueToTypeMap<UnObjType<T>>[K] extends classes.Type ? ObjValueToTypeMap<UnObjType<T>>[K] : classes.Type
  > {
    const field = this.type!.getField(<string>key);
    Iif (!field) throw new Error('NO_FIELD');
    const data = (this.data as Record<string, unknown>)[<string>key];
    return new Value(data, field.val) as any;
  }
 
  public fn<K extends keyof ObjValueToTypeMap<UnObjType<T>>>(
    key: K,
  ): FnValue<
    Ensure<
      ObjValueToTypeMap<UnObjType<T>>[K] extends classes.Type ? ObjValueToTypeMap<UnObjType<T>>[K] : classes.Type,
      classes.FnType<any, any, any>
    >
  > {
    const val = this.get(key);
    return new FnValue(val.data, val.type as any);
  }
 
  public field<F extends classes.KeyType<any, any>>(
    field: F | ((t: TypeBuilder) => F),
    data: classes.ResolveType<UnObjFieldTypeVal<F>>,
  ): ObjValue<classes.ObjType<[...UnObjType<T>, F]>> {
    field = typeof field === 'function' ? field((this.type as classes.ObjType<any>).getSystem().t) : field;
    (this.data as any)[field.key] = data;
    const type = this.type!;
    const system = type.system;
    Iif (!system) throw new Error('NO_SYSTEM');
    type.keys.push(field);
    return this as any;
  }
 
  public add<K extends string, V extends classes.Type>(
    key: K,
    type: V | ((t: TypeBuilder) => V),
    data: classes.ResolveType<V>,
  ) {
    const system = (this.type as classes.ObjType<any>).getSystem();
    const t = system.t;
    type = typeof type === 'function' ? type(t) : type;
    return this.field(t.Key(key, type), data);
  }
 
  public set<K extends string, V extends classes.Type>(key: K, value: Value<V>) {
    return this.add(key, value.type!, value.data);
  }
 
  public merge<O extends ObjValue<any>>(obj: O): ObjValue<classes.ObjType<[...UnObjType<T>, ...UnObjType<O['type']>]>> {
    Object.assign(this.data as object, obj.data);
    const type = this.type!;
    const system = type.system;
    Iif (!system) throw new Error('NO_SYSTEM');
    type.keys.push(...type.keys);
    return this as any;
  }
 
  public name(): string {
    return 'ObjValue';
  }
}