All files / src/Utility DataStructures.ts

94.11% Statements 16/17
86.66% Branches 13/15
100% Functions 5/5
94.11% Lines 16/17

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                                7x         85x 460x 460x 460x   460x 75x 385x         385x                                                       9x 9x                 10x 1x   10x                 2x                 1x      
/**
 * ## DataStructures Utility
 * @exports
 *  - FixedSizeArray<T>
 *  - MapObject
 * @module DataStructures
 * @category Utility
 */
 
/**
 * Recursive function to map an object and its properties to a string key Map.
 *
 * @param objectToMap - Object to be mapped.
 * @param map - Map to store the object and its properties.
 * @param parentKey - Key of the parent object. Optional.
 */
export const MapObject = (
  objectToMap: Record<string, any>,
  map: Map<string, object | Array<any> | string | boolean>,
  parentKey?: string
) => {
  for (const key in objectToMap) {
    Eif (objectToMap.hasOwnProperty(key)) {
      const value = objectToMap[key];
      const newKey = parentKey ? `${parentKey}.${key}` : key;
 
      if (typeof value === "object" && !(value instanceof Array)) {
        MapObject(value, map, newKey);
      } else if (
        value instanceof Array ||
        typeof value === "string" ||
        typeof value === "boolean"
      ) {
        map.set(newKey, value);
      } else E{
        console.error("MapObject Not expecting this value!", key, value);
      }
    }
  }
};
 
/**
 * A class that represents a fixed-size array that removes the oldest item
 * when a new item is added and the size exceeds the maximum.
 */
export class FixedSizeArray<T> {
  /**
   * The data stored in the array
   */
  private readonly data: T[];
  /**
   * The maximum size of the array
   */
  private readonly maxSize: number;
 
  /**
   * Creates a new instance of the FixedSizeArray
   *
   * @param maxSize The maximum size of the array
   */
  constructor(maxSize: number) {
    this.data = [];
    this.maxSize = maxSize;
  }
 
  /**
   * Adds a new item to the array
   *
   * @param item The item to be added
   */
  add(item: T) {
    if (this.data.length === this.maxSize) {
      this.data.shift();
    }
    this.data.push(item);
  }
 
  /**
   * Returns the data stored in the array
   *
   * @returns The data stored in the array
   */
  getData(): T[] {
    return this.data;
  }
 
  /**
   * Iterates over the items in the array and performs a callback function on each item
   *
   * @param callback A callback function to be performed on each item in the array
   */
  forEach(callback: (item: T, index: number, array: T[]) => void) {
    this.data.forEach(callback);
  }
}