All files / src helper.ts

37.5% Statements 18/48
5.26% Branches 1/19
12.5% Functions 2/16
36.95% Lines 17/46

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                1x 1x               1x       1x         1x       1x         1x                                                     1x                                                     1x       1x                   1x                                       1x 60x 64x 64x 45x               1x      
/*
 * The coLAB project
 * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
 *
 * Licensed under the MIT License
 */
 
import { HttpSession, WithId } from 'colab-rest-client';
import { escapeRegExp } from 'lodash';
import logger from './logger';
 
// *************************************************************************************************
// String format check through regular expressions
 
/**
 * Check that the email format is valid
 */
export function assertEmailFormat(data: string) {
  return data.match(emailFormat) != null;
}
 
const emailFormat = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
 
/**
 * Check that the username format is valid
 */
export function assertUserNameFormat(data: string) {
  return data.match(userNameFormat) != null;
}
 
const userNameFormat = /^[a-zA-Z0-9._-]+$/;
 
/**
 * Filter data of the list to only those matching the regular expressions
 */
export function regexFilter<T>(
  list: T[],
  search: string,
  matchFn: (regex: RegExp, item: T) => boolean,
): T[] {
  Iif (search.length <= 0) {
    return list;
  }
 
  const regexes = search.split(/\s+/).map(regex => new RegExp(escapeRegExp(regex), 'i'));
 
  return list.filter(item => {
    return regexes.reduce<boolean>((acc, regex) => {
      Iif (acc == false) {
        return false;
      }
      return matchFn(regex, item);
    }, true);
  });
}
 
// *************************************************************************************************
// sorting
 
/**
 * Sort strings : null first, then according to language
 */
export function sortSmartly(
  a: string | null | undefined,
  b: string | null | undefined,
  lang: string,
) {
  Iif (a == null || b == null) {
    return 0;
  }
 
  Iif (a == null && b != null) {
    return 1;
  }
 
  Iif (a != null && b == null) {
    return -1;
  }
 
  return a.localeCompare(b, lang, { numeric: true });
}
 
// *************************************************************************************************
// developpment tools
 
/**
 * Logs an error and, as it is typed as never, it throws a compilation error if a case is missing.
 */
// Advice : add this comment before when using it // If next line is erroneous, it means a type of xxx is not handled
export function assertUnreachable(x: never): void {
  logger.error(x);
}
 
export const mapById = <T extends WithId>(entities: T[]): { [id: number]: T } => {
  const map: { [id: number]: T } = {};
  entities.forEach(entity => {
    Iif (entity.id != null) {
      map[entity.id] = entity;
    }
  });
  return map;
};
 
export function buildLinkWithQueryParam(
  baseUrl: string,
  queryParameters?: { [key: string]: string | null | undefined },
): string {
  if (queryParameters == null) {
    return baseUrl;
  } else {
    return (
      baseUrl +
      '?' +
      Object.entries(queryParameters)
        .map(kv =>
          kv[0] && kv[1] ? encodeURIComponent(kv[0]) + '=' + encodeURIComponent(kv[1]) : null,
        )
        .filter(param => !!param)
        .join('&')
    );
  }
}
 
export function removeAllItems(array: unknown[], items: unknown[]): void {
  items.forEach(item => {
    const index = array.indexOf(item);
    if (index >= 0) {
      array.splice(index, 1);
    }
  });
}
 
/**
 * Determine if the session is the current one
 */
export function isMySession(session: HttpSession) {
  return session.userAgent === navigator.userAgent;
}