All files / filtered-search si-filtered-search-helper.ts

100% Statements 18/18
100% Branches 11/11
100% Functions 9/9
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                        1x 112x 142x                   1x 105x           1x       54x     1x 254x             1x         6x 1x     5x   4x   1x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { formatDate } from '@angular/common';
import { isValid } from '@siemens/element-ng/datepicker';
 
import { CriterionDefinition, OptionCriterion, OptionType } from './si-filtered-search.model';
 
export interface TypeaheadOptionCriterion extends OptionCriterion {
  selected?: boolean;
  translatedLabel: string;
}
 
export interface InternalCriterionDefinition extends CriterionDefinition {
  label: string;
  translatedLabel: string;
}
 
/** Convert options to option criterions */
export const toOptionCriteria = (values?: OptionType[]): OptionCriterion[] =>
  values?.map(v =>
    typeof v === 'string'
      ? { value: v }
      : { label: v.label, value: v.value, iconClass: v.iconClass }
  ) ?? [];
 
/*
 * Update selected state the matching is based on value since plain
 * string options will automatically fill the value attribute with the
 * actual string value.
 */
export const selectOptions = (options: TypeaheadOptionCriterion[], toSelect: string[]): void =>
  options.forEach(val => (val.selected = toSelect.includes(val.value)));
 
/**
 * Difference by name, create an array that contains those elements of criteria's a that are not in criteria's b.
 * This operation is also sometimes called minus (-).
 */
export const differenceByName = (
  a: InternalCriterionDefinition[],
  b: { config: InternalCriterionDefinition }[]
): InternalCriterionDefinition[] =>
  a.filter(x => b.filter(y => y.config.name === x.name).length === 0);
 
/** Convert criteria to internal model criteria */
export const toInternalCriteria = (crit: CriterionDefinition): InternalCriterionDefinition => {
  return {
    ...crit,
    label: crit.label ?? crit.name,
    translatedLabel: crit.label ?? crit.name
  };
};
 
export const getISODateString = (
  date: Date | undefined,
  format: 'date' | 'date-time',
  locale: string
): string => {
  if (!isValid(date)) {
    return '';
  }
 
  switch (format) {
    case 'date':
      return formatDate(date, 'yyyy-MM-dd', locale);
    case 'date-time':
      return date.toISOString();
  }
};