File

projects/wms-framework/src/lib/baseframework/datatypesUtilities.ts

Index

Methods

Methods

Static createFrom
createFrom(days: number, hours: number, minutes: number, seconds: number, milliseconds?: number)
Parameters :
Name Type Optional
days number No
hours number No
minutes number No
seconds number No
milliseconds number Yes
Returns : number
Static getDays
getDays(n: number)
Parameters :
Name Type Optional
n number No
Returns : number
Static getHours
getHours(n: number)
Parameters :
Name Type Optional
n number No
Returns : number
Static getMinutes
getMinutes(n: number)
Parameters :
Name Type Optional
n number No
Returns : number
Static getSeconds
getSeconds(n: number)
Parameters :
Name Type Optional
n number No
Returns : number
Static getTotalHours
getTotalHours(n: number)
Parameters :
Name Type Optional
n number No
Returns : number
import { Debugger } from '../diagnostics/Debugger';
import { DefaultNumberFormatter } from '../utils/FlexibleJsNumberFormatter';
import { out } from '../utils/RefOrOut';
import { smParseDate } from './dateUtilities';
import { FormatException } from './Exceptions';
import { GlobalizationHelper } from './globalization';

/**
 * Tries to parse an input string into a boolean value.
 *
 * @export
 * @param {string} booleanStr The input string.
 * @param {out<boolean>} result The output boolean value.
 * @return {*}  {boolean} `true` if the parsing is successful, `false` otherwise.
 */
export function smTryParseBoolean(
  booleanStr: string,
  result: out<boolean>
): boolean {
  const strLower = booleanStr?.toLowerCase() ?? '';
  const value = strLower === 'true';
  if (value || strLower === 'false') {
    result.value = value;
    return true;
  } else {
    return false;
  }
}

/**
 * Parse an input string into a boolean value.
 *
 * @export
 * @param {string} booleanStr The input string.
 * @return {*}  {boolean} `true` if the input string is 'true' (case insensitive),
 *                        or `false` if the input string is 'false' (case insensitive).
 * @throws If the input string is not 'true' or 'false'.
 */
export function parseBoolean(booleanStr: string): boolean {
  let result = false;
  if (
    smTryParseBoolean(
      booleanStr,
      out((v) => (result = v))
    )
  ) {
    return result;
  } else {
    throw new Error('Invalid boolean string');
  }
}

/**
 * Tries to parse an input string into an integer value.
 * Only decimal digits are allowed, hexadecimal digits are not allowed.
 *
 * @export
 * @param {string} numberStr The input string.
 * @param {out<number>} result The output integer value.
 * @return {*}  {boolean} `true` if the parsing is successful, `false` otherwise.
 */
export function smTryParseInt(numberStr: string, result: out<number>): boolean {
  /* istanbul ignore else  */
  if (/^\s*[-+]?\d+\s*$/.test(numberStr)) {
    const parsed = parseInt(numberStr, 10);
    /* istanbul ignore else  */
    if (!Number.isNaN(parsed)) {
      result.value = parsed;
      return true;
    }
  }
  return false;
}

/**
 * Tries to parse an input string into an short integer value.
 * Only decimal digits are allowed, hexadecimal digits are not allowed.
 *
 * @export
 * @param {string} numberStr The input string.
 * @param {out<number>} result The output integer value.
 * @return {*}  {boolean} `true` if the parsing is successful, `false` otherwise.
 */
export function smTryParseShort(
  numberStr: string,
  result: out<number>
): boolean {
  /* istanbul ignore else  */
  if (smTryParseInt(numberStr, result)) {
    /* istanbul ignore else  */
    if (-32768 <= result.value && result.value <= 32767) {
      return true;
    } else {
      result.value = null;
      return false;
    }
  }
  return false;
}

/**
 * Tries to parse an input string into a float value.
 *
 * @export
 * @param {string} numberStr The input string.
 * @param {out<number>} result The output float value.
 * @return {*}  {boolean} `true` if the parsing is successful, `false` otherwise.
 */
export function smTryParseFloat(
  numberStr: string,
  result: out<number>
): boolean {
  const parsed = parseFloat(numberStr);
  if (!Number.isNaN(parsed)) {
    result.value = parsed;
    return true;
  } else {
    return false;
  }
}

export function smCompareNumbers(n1: number, n2: number): number {
  const comp = n1 - n2;
  return comp < 0 ? -1 : comp > 0 ? 1 : 0;
}

export function getBytes32(value: number) {
  return [
    // eslint-disable-next-line no-bitwise
    0xff & value,
    // eslint-disable-next-line no-bitwise
    (0xff00 & value) >> 8,
    // eslint-disable-next-line no-bitwise
    (0xff0000 & value) >> 16,
    // eslint-disable-next-line no-bitwise
    (0xff000000 & value) >> 24,
  ];
}

export function smDecimalGetBits(value: number): number[] {
  Debugger.Throw('This function is not supported');
  return null;
}

export function convertToSingleCharString(
  obj: unknown,
  globalizationHelper?: GlobalizationHelper
) {
  if (obj !== null && typeof obj != 'undefined') {
    if (typeof obj === 'number') {
      return String.fromCharCode(obj);
    } else {
      return obj.toString()[0];
    }
  } else {
    return '\u0000';
  }
}

export function isDigitCharacter(str: string): boolean {
  return str.length === 1 && str[0] >= '0' && str[0] <= '9';
}

export function isSingleUpperCaseChar(str: string): boolean {
  return str.length === 1 && /[A-Z]/.test(str);
}

export function isSingleLowerCaseChar(str: string): boolean {
  return str.length === 1 && /[a-z]/.test(str);
}

export function isSingleCharLetterOrDigit(str: string) {
  return str.length === 1 && /^\w/.test(str) && str[0] !== '_';
}
/**
 * Converts the given string parameter to upper case.
 * @param str The string to be converted.
 * @returns A new string with upper case applied.
 */
export function toUpperCharacter(str: string): string {
  return str.toUpperCase();
}

export class TimeRangeUtils {
  public static getDays(n: number): number {
    Debugger.Throw('Not implementeD');
    return 0;
  }
  public static getHours(n: number): number {
    Debugger.Throw('Not implemented');
    return 0;
  }
  public static getTotalHours(n: number): number {
    Debugger.Throw('Not implemented');
    return 0;
  }
  public static getMinutes(n: number): number {
    Debugger.Throw('Not implemented');
    return 0;
  }
  public static getSeconds(n: number): number {
    Debugger.Throw('Not implemented');
    return 0;
  }
  public static createFrom(
    days: number,
    hours: number,
    minutes: number,
    seconds: number,
    milliseconds?: number
  ): number {
    Debugger.Throw('Not implemented');
    return 0;
  }
}

/**
 * Applies a numeric format to the given string
 *
 * @export
 * @param {number} value value to format
 * @param {string} format format string
 * @return {*}  {string} the formatted value
 */
export function applyNumericFormat(value: number, format: string): string {
  return format
    ? DefaultNumberFormatter.FormatNumber(value, format)
    : value.toString();
}

export function convertToFloat(value: any) {
  if (typeof value === 'number') {
    return value;
  } else if (typeof value === 'string') {
    return parseFloat(value.replace(/,/g, ''));
  } else {
    return 0;
  }
}

/**
 * Converts a string or a Obj type DateTime format into Date,
 * on null case it will return default MinDate.
 *
 * @export
 * @param {*} value
 * @return {*}  {Date}
 */
export function convertToDateTime(value): Date {
  /* istanbul ignore else */
  if (
    value instanceof Date ||
    Object.prototype.toString.call(value) === '[object Date]'
  ) {
    return value;
  } else if (typeof value === 'string') {
    return smParseDate(value);
  } else if (value == null) {
    return new Date('0001-01-01T00:00:00Z');
  }
  throw new FormatException('Value was not recognized as a valid Date.');
}

result-matching ""

    No results matching ""