All files / lib/dynamic-form-evaluation dynamic-form-evaluation.builder.ts

100% Statements 16/16
100% Branches 3/3
100% Functions 5/5
100% Lines 15/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                  1x       78x   78x   78x       3x       16x 3x 3x 3x 2x 2x 2x 2x 2x   1x        
import { Inject, Injectable, Optional } from '@angular/core';
import { DynamicFormControl } from '../dynamic-form-control/dynamic-form-control';
import { DynamicFormControlEvaluator } from '../dynamic-form-control/dynamic-form-control-evaluator';
import { DynamicFormControlEvaluatorType } from '../dynamic-form-control/dynamic-form-control-evaluator-type';
import { DynamicFormControlEvaluatorTypeConfig,
  DYNAMIC_FORM_CONTROL_EVALUATOR_TYPE_CONFIG } from '../dynamic-form-control/dynamic-form-control-evaluator-type-config';
import { DynamicFormLibraryService } from '../dynamic-form-library/dynamic-form-library.service';
 
@Injectable()
export class DynamicFormEvaluationBuilder {
  readonly controlEvaluatorTypes: DynamicFormControlEvaluatorType[];
 
  constructor(
    private libraryService: DynamicFormLibraryService,
    @Optional() @Inject(DYNAMIC_FORM_CONTROL_EVALUATOR_TYPE_CONFIG)
    private controlEvaluatorConfig: DynamicFormControlEvaluatorTypeConfig,
  ) {
    this.controlEvaluatorTypes = this.libraryService.filterTypes(this.controlEvaluatorConfig);
  }
 
  getControlEvaluatorType(type: string): DynamicFormControlEvaluatorType {
    return this.controlEvaluatorTypes.find(f => f.type === type);
  }
 
  createControlEvaluators(control: DynamicFormControl): DynamicFormControlEvaluator[] {
    return Object.keys(control.definition.evaluators || {}).reduce((result, key) => {
      const evaluatorDefinition = control.definition.evaluators[key];
      const evaluatorType = this.getControlEvaluatorType(evaluatorDefinition.type);
      if (evaluatorType) {
        const type = evaluatorType.type;
        const inputType = evaluatorType.inputType;
        const func = evaluatorType.func;
        const evaluator = new DynamicFormControlEvaluator(key, type, inputType, control, func);
        return result.concat(evaluator);
      }
      return result;
    }, []);
  }
}