All files / lib/dynamic-form-action/dynamic-form-icon dynamic-form-icon.service.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 8/8
100% Lines 19/19

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            1x       11x   11x   11x                   7x 7x 5x   2x       11x 11x 11x 11x 3x     8x 8x 9x                 8x 10x 9x      
import { Inject, Injectable, Optional } from '@angular/core';
import { DynamicFormLibraryService } from '../../dynamic-form-library/dynamic-form-library.service';
import { DynamicFormIconConfig, DynamicFormIconConfigs, DYNAMIC_FORM_ICON_CONFIGS } from './dynamic-form-icon-config';
import { DynamicFormIconTemplate } from './dynamic-form-icon-template';
 
@Injectable()
export class DynamicFormIconService {
  readonly iconConfig: DynamicFormIconConfig;
 
  constructor(
    private libraryService: DynamicFormLibraryService,
    @Optional() @Inject(DYNAMIC_FORM_ICON_CONFIGS)
    private iconConfigs: DynamicFormIconConfigs,
  ) {
    this.iconConfig = this.mergeIconConfigs(this.iconConfigs);
  }
 
  getIcon(icon: string): string;
  /**
   * @deprecated The method should not be used
   */
  // eslint-disable-next-line @typescript-eslint/unified-signatures
  getIcon(template: DynamicFormIconTemplate): string;
  getIcon(iconOrTemplate: string | DynamicFormIconTemplate): string {
    const icon = typeof iconOrTemplate === 'string' ? iconOrTemplate : iconOrTemplate?.icon;
    if (icon) {
      return this.iconConfig.icons[icon] || icon;
    }
    return undefined;
  }
 
  private mergeIconConfigs(configs: DynamicFormIconConfigs): DynamicFormIconConfig {
    const library = this.libraryService.library;
    const libraryName = library.name;
    const defaultConfig = { icons: {}, libraryName };
    if (!configs || !configs.length) {
      return defaultConfig;
    }
 
    const libraryConfigs = this.getLibraryConfigs(configs);
    return libraryConfigs.reduce((result, config) => {
      return {
        ...result, ...config,
        icons: { ...result.icons, ...config.icons },
        libraryName,
      };
    }, defaultConfig);
  }
 
  private getLibraryConfigs(configs: DynamicFormIconConfigs): DynamicFormIconConfigs {
    return this.libraryService.libraryNamesReverse
      .map(name => configs.find(config => config.libraryName === name))
      .filter(config => !!config);
  }
}