File

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

Description

String class

Index

Methods

Methods

Static compareOrdinal
compareOrdinal(strA: string, strB: string)

Compares two strings by evaluating the numeric values of the corresponding 0 if are equal. -1 if strA is less than strB. 1 if strA is greater than strB.

Parameters :
Name Type Optional
strA string No
strB string No
Returns : number

An integer that indicates the lexical relationship between the two comparands. 0 if are equal. -1 if strA is less than strB. 1 if strA is greater than strB.

Private Static getMinIndexOfSeparatorWithSeparator
getMinIndexOfSeparatorWithSeparator(phrase: string, separators: string[])

Gets the min index that equals any of the separations patterns if this exists, otherwise returns null

Parameters :
Name Type Optional
phrase string No
separators string[] No
Returns : any | null

An anonymous object with the index and its specific separator

Private Static getSubstring
getSubstring(phrase: string, indexWithSeparator: any)

Gets the resulting substring in a given index according to the separation pattern

Parameters :
Name Type Optional
phrase string No
indexWithSeparator any No
Returns : string[]

Returns a string array with the resulting substring and the remaining word

Static split
split(phrase: string, separator: string[], options?: StringSplitOptions)

Splits a string into substrings according to the given separators

Parameters :
Name Type Optional
phrase string No
separator string[] No
options StringSplitOptions Yes
Returns : string[]

An array of strings

import { StringSplitOptions } from './StringSplitOptions';

/**
 *  String class
 *
 * @export
 * @class Convert
 */
export class StringHelper {
  /**
   * Compares two strings by evaluating the numeric values of the corresponding
   * @param strA
   * @param strB
   * @returns An integer that indicates the lexical relationship between the two comparands.
   *          0 if are equal.
   *          -1 if strA is less than strB.
   *          1 if strA is greater than strB.
   */
  public static compareOrdinal(strA: string, strB: string): number {
    if (strA == strB) {
      return 0;
    }
    if (strA == null) {
      return -1;
    }
    if (strB == null) {
      return 1;
    }
    return strA.localeCompare(strB);
  }
  /**
   * Splits a string into substrings according to the given separators
   * @param phrase
   * @param separator
   * @param options
   * @returns An array of strings
   */
  public static split(
    phrase: string,
    separator: string[],
    options?: StringSplitOptions
  ): string[] {
    const wordToPushIndex: number = 0;
    const newPhraseIndex: number = 1;
    const emptyStringIndex: number = 0;

    let iPhrase = phrase;
    let splitResult: string[] = new Array();

    if (separator.length == 1 && separator[emptyStringIndex] == '') {
      splitResult.push(iPhrase);
      return splitResult;
    }
    if (separator.length > 0 && separator.includes('')) {
      separator = separator.filter((x) => x != '');
    }

    let index = this.getMinIndexOfSeparatorWithSeparator(iPhrase, separator);

    while (index != null) {
      const getSubstringOutput: string[] = this.getSubstring(iPhrase, index);
      splitResult.push(getSubstringOutput[wordToPushIndex]);
      iPhrase = getSubstringOutput[newPhraseIndex];
      index = this.getMinIndexOfSeparatorWithSeparator(iPhrase, separator);
    }

    splitResult.push(iPhrase);

    if (
      options != undefined &&
      options == StringSplitOptions.RemoveEmptyEntries
    ) {
      splitResult = splitResult.filter((e) => e);
    }

    return splitResult;
  }

  /**
   * Gets the resulting substring in a given index according to the separation pattern
   * @param phrase
   * @param indexWithSeparator
   * @returns Returns a string array with the resulting substring and the remaining word
   */
  private static getSubstring(
    phrase: string,
    indexWithSeparator: any
  ): string[] {
    let subStr: string[] = new Array();
    subStr.push(phrase.substring(0, indexWithSeparator.ind));
    subStr.push(
      phrase.slice(
        indexWithSeparator.ind + indexWithSeparator.sep.length,
        phrase.length
      )
    );
    return subStr;
  }

  /**
   * Gets the min index that equals any of the separations patterns if this exists, otherwise returns null
   * @param phrase
   * @param separators
   * @returns An anonymous object with the index and its specific separator
   */
  private static getMinIndexOfSeparatorWithSeparator(
    phrase: string,
    separators: string[]
  ): any | null {
    let spltIndex: number = -1;
    let coincidencesArr: any[] = new Array();
    let minIndexCoincidence: any | null;

    for (let separator of separators) {
      spltIndex = phrase.indexOf(separator);

      if (spltIndex > -1) {
        let tuple = { ind: spltIndex, sep: separator };

        coincidencesArr.push(tuple);
      }
    }

    minIndexCoincidence =
      coincidencesArr.length > 0
        ? coincidencesArr.reduce((prev, curr) =>
            prev.ind < curr.ind ? prev : curr
          )
        : null;

    return minIndexCoincidence;
  }
}

result-matching ""

    No results matching ""