All files / src/utils object.ts

20% Statements 2/10
0% Branches 0/21
0% Functions 0/2
20% Lines 2/10

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 401x           1x                                                                  
import { Constants } from '@logchain/configs';
import {AnyObject} from '@loopback/repository';
 
/**
 * Object utils.
 */
export class ObjectUtils {
  /**
   * Assign value from object to object by specific array.
   * @param  {Record<string} fromObj
   * @param  {} AnyObject>
   * @param  {Record<string} toObj
   * @param  {} AnyObject>
   * @param  {Array<string>} properties
   * @param  {} removeId=true
   * @returns void
   */
  public static assignByProperties(
    fromObj: Record<string, AnyObject>,
    toObj: Record<string, AnyObject>,
    properties: Array<string>,
    includes = undefined,
    removeId = true,
  ): void {
    if (fromObj && toObj && properties && properties instanceof Array) {
      properties.map((property) => {
        if (typeof fromObj[property] !== 'undefined' && fromObj[property] !== null) {
          if ((removeId && property === 'id') || (includes && !includes[property]) || Constants.doNotAllowedCopyFields[property]) {
            if (Constants.defaultValuesForCopy[property] !== undefined) {
              toObj[property] = Constants.defaultValuesForCopy[property]; // Set default value if it doesn't allow null
            }
            return true;
          }
          toObj[property] = fromObj[property];
        }
      });
    }
  }
}