All files / src/util ImmutableCollectionOperations.ts

100% Statements 34/34
100% Branches 11/11
100% Functions 16/16
100% Lines 29/29

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 99                                        2x 2x     1x         18x       6x       7x       2x               66x     171x 66x       44x 44x 44x 273x 20x   253x 2x     252x     44x       10x 10x 10x         11x 11x 11x         22x 22x 22x         11x      
/*
 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import DomainObject from 'domain/DomainObject';
import DomainObjectView from 'domain/DomainObjectView';
 
export function toggleElement<T>(collection: T[], element: T): T[] {
  if (collection.indexOf(element) !== -1) {
    return collection.filter(e => e !== element);
  }
  else {
    return [...collection, element];
  }
}
 
export function withoutElementWithId<T extends { id?: number }>(collection: T[], removedElementId: number): T[] {
  return collection.filter(element => element.id !== removedElementId);
}
 
export function withoutElement<T extends { id?: number }>(collection: T[], removedElement: T): T[] {
  return withoutElementWithId(collection, removedElement.id as number);
}
 
export function withElement<T extends { id?: number }>(collection: T[], addedElement: T): T[] {
  return [...collection, addedElement];
}
 
export function withUpdatedElement<T extends { id?: number }>(collection: T[], updatedElement: T): T[] {
  return withElement(withoutElement(collection, updatedElement), updatedElement);
}
 
interface ObjectWithKeys {
  [key: string]: any;
}
 
export function objectWithout<F>(obj: F, ...without: (keyof F)[]): F {
  const copy: ObjectWithKeys = {
    ...obj
  };
  without.forEach(key => delete copy[key as string]);
  return copy as F;
}
 
export function mapDomainObjectToView<T extends DomainObject>(obj: T|DomainObjectView<T>): DomainObjectView<T> {
  let result = {} as ObjectWithKeys;
  const objWithKeys = obj as ObjectWithKeys;
  for(const key in obj) {
    if (objWithKeys[key] !== null && objWithKeys[key].id !== undefined) {
      result[key] = objWithKeys[key].id;
    }
    else if (Array.isArray(objWithKeys[key]) && objWithKeys[key].length > 0 && objWithKeys[key][0].id !== undefined) {
      result[key] = objWithKeys[key].map((ele: DomainObject)  => ele.id);
    }
    else {
      result[key] = objWithKeys[key];
    }
  }
  return result as DomainObjectView<T>;
}
 
export function createIdMapFromList<T extends DomainObject>(collection: T[]): Map<number, DomainObjectView<T>> {
  const map = new Map<number, DomainObjectView<T>>();
  collection.forEach(ele => map.set(ele.id as number, mapDomainObjectToView(ele)));
  return map;
}
 
export function mapWithoutElement<T extends DomainObject>(map: Map<number, DomainObjectView<T>>,
  removedElement: T|DomainObjectView<T>): Map<number, DomainObjectView<T>> {
  const copy = new Map<number, DomainObjectView<T>>(map);
  copy.delete(removedElement.id as number);
  return copy;
}
 
export function mapWithElement<T extends DomainObject>(map: Map<number, DomainObjectView<T>>,
  addedElement: T|DomainObjectView<T>): Map<number, DomainObjectView<T>> {
  const copy = new Map<number, DomainObjectView<T>>(map);
  copy.set(addedElement.id as number, mapDomainObjectToView(addedElement));
  return copy;
}
 
export function mapWithUpdatedElement<T extends DomainObject>(map: Map<number, DomainObjectView<T>>,
  updatedElement: T|DomainObjectView<T>): Map<number, DomainObjectView<T>> {
  return mapWithElement(map, updatedElement);
}