All files / ip-input si-ip4-input.directive.ts

100% Statements 7/7
75% Branches 3/4
100% Functions 3/3
100% Lines 6/6

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                                                                    1x         132x       122x 122x   122x         502x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Directive } from '@angular/core';
import {
  AbstractControl,
  ControlValueAccessor,
  NG_VALIDATORS,
  NG_VALUE_ACCESSOR,
  ValidationErrors,
  Validator
} from '@angular/forms';
 
import { splitIpV4Sections } from './address-utils';
import { ipV4CIDRValidator, ipV4Validator } from './address-validators';
import { AddrInputEvent, SiIpInputDirective } from './si-ip-input.directive';
 
@Directive({
  selector: 'input[siIpV4]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiIp4InputDirective,
      multi: true
    },
    {
      provide: NG_VALIDATORS,
      useExisting: SiIp4InputDirective,
      multi: true
    }
  ],
  exportAs: 'siIpV4'
})
export class SiIp4InputDirective
  extends SiIpInputDirective
  implements ControlValueAccessor, Validator
{
  validate(control: AbstractControl): ValidationErrors | null {
    return this.cidr() ? ipV4CIDRValidator(control) : ipV4Validator(control);
  }
 
  maskInput(e: AddrInputEvent): void {
    const { value, pos, type } = e;
    const sections = splitIpV4Sections({ type, input: value, pos, cidr: this.cidr() });
 
    this.renderer.setProperty(
      this.inputEl,
      'value',
      sections
        .splice(0, this.cidr() ? 9 : 7)
        .map(s => s.value)
        .join('')
    );
  }
}