All files / src/app/core/models/server-config server-config.mapper.ts

93.33% Statements 14/15
91.66% Branches 11/12
100% Functions 4/4
93.33% Lines 14/15

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 46100x         100x   4x 1x   3x       11x 7x 1x 6x 1x 5x       9x       9x 19x                                  
import { omit } from 'ish-core/utils/functions';
 
import { ServerConfigData, ServerConfigDataEntry } from './server-config.interface';
import { ServerConfig } from './server-config.model';
 
export class ServerConfigMapper {
  static fromData(payload: ServerConfigData): ServerConfig {
    if (payload?.data) {
      return ServerConfigMapper.mapEntries(payload.data);
    }
    return {};
  }
 
  private static transformType(val: unknown) {
    if (typeof val === 'string') {
      if (!isNaN(+val)) {
        return +val;
      } else if (val === 'true') {
        return true;
      } else Iif (val === 'false') {
        return false;
      }
    }
    return val;
  }
 
  private static mapEntries(entries: ServerConfigDataEntry): ServerConfig {
    return Object.entries(entries).reduce(
      (acc, entry) => ({
        ...acc,
        [entry[0]]:
          // eslint-disable-next-line unicorn/no-null
          entry[1] !== null && typeof entry[1] === 'object' && !Array.isArray(entry[1])
            ? // do recursion if we find an object
              ServerConfigMapper.mapEntries(
                // get rid of id
                omit(entry[1], 'id')
              )
            : // improve data quality
              ServerConfigMapper.transformType(entry[1]),
      }),
      {}
    );
  }
}