All files / pills-input si-pills-input-pattern-base.ts

100% Statements 10/10
100% Branches 7/7
100% Functions 2/2
100% Lines 10/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 40 41 42 43 44 45 46 47 48 49 50 51                      1x               28x                 15x 15x 15x 15x 15x   13x     13x             2x            
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { signal, Signal } from '@angular/core';
 
import {
  SiPillsInputValueHandlerTrigger,
  SiPillsInputValueParseResult
} from './si-pills-input-value-handler';
 
export const SEPARATOR_REGEX = /\s*[;,]\s*/g;
 
export abstract class SiPillsInputPatternBase {
  /**
   * Regex to split the input value into segments.
   *
   * @defaultValue SEPARATOR_REGEX
   */
  readonly separatorRegex = signal(SEPARATOR_REGEX);
  /** Regex to validate an input string item. */
  readonly validationRegex?: Signal<RegExp | undefined>;
 
  /** @internal */
  handle(
    value: string,
    trigger: SiPillsInputValueHandlerTrigger
  ): SiPillsInputValueParseResult | undefined {
    const segments = value.split(this.separatorRegex());
    const itemRegex = this.validationRegex?.();
    if (segments.length) {
      const newValue = trigger === 'input' ? segments.pop()! : '';
      if (
        segments.every(segment => {
          return segment && (!itemRegex || segment.match(itemRegex));
        })
      ) {
        return {
          newPills: segments,
          newValue
        };
      }
    }
 
    return {
      newPills: [],
      newValue: value
    };
  }
}