All files / src ddic_references.ts

100% Statements 33/33
100% Branches 18/18
100% Functions 6/6
100% Lines 33/33

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      1x 4943x     1341x 1341x   1341x 1215x   1341x       159x 139x     20x 20x   20x 3x   20x 3x   20x       1165x       2x 2x   2x 2x 1x   1x             27x 27x 27x   27x 45x 45x 23x 18x 18x           27x      
import {IObject} from "./objects/_iobject";
import {IDDICReferences} from "./_iddic_references";
 
export class DDICReferences implements IDDICReferences {
  private readonly index: { [name: string]: { [type: string]: IObject[] } } = {};
 
  public setUsing(obj: IObject, using: IObject[]): void {
    const newName = obj.getName().toUpperCase();
    const newType = obj.getType();
 
    if (this.index[newName] === undefined) {
      this.index[newName] = {};
    }
    this.index[newName][newType] = using;
  }
 
  public addUsing(obj: IObject, using: IObject | undefined) {
    if (using === undefined) {
      return;
    }
 
    const newName = obj.getName().toUpperCase();
    const newType = obj.getType();
 
    if (this.index[newName] === undefined) {
      this.index[newName] = {};
    }
    if (this.index[newName][newType] === undefined) {
      this.index[newName][newType] = [];
    }
    this.index[newName][newType].push(using);
  }
 
  public clear(obj: IObject) {
    this.setUsing(obj, []);
  }
 
  public listUsing(obj: IObject): readonly IObject[] {
    const newName = obj.getName().toUpperCase();
    const newType = obj.getType();
 
    const found = this.index[newName]?.[newType];
    if (found !== undefined) {
      return found;
    } else {
      return [];
    }
  }
 
  public listWhereUsed(obj: IObject): {type: string, name: string}[] {
    // todo, add reverse index, this is slow
 
    const ret: {type: string, name: string}[] = [];
    const searchName = obj.getName().toUpperCase();
    const searchType = obj.getType();
 
    for (const name in this.index) {
      for (const type in this.index[name]) {
        for (const f of this.index[name][type]) {
          if (f.getType() === searchType && f.getName() === searchName) {
            ret.push({type, name});
            break; // current outermost loop
          }
        }
      }
    }
 
    return ret;
  }
 
}