@nevware21/ts-utils
    Preparing search index...

    Function polyStrSymSplit

    • The polyStrSymSplit() splits a string into substrings using the Symbol.split method from the splitter object to provide custom behavior, like the native split method. On a runtime without a native Symbol.split (eg. IE) it calls the splitter's method for the polyfill symbol, which getKnownSymbol returns for WellKnownSymbols.split, with a native Symbol.split the runtime split method is called (which calls the splitter's method).

      Parameters

      • value: string

        The string value to be split into substrings.

      • splitter: { "[split]"(string: string, limit?: number): string[] }

        The object which contains a Symbol.split method. Like the native split, omitting splitter or passing undefined returns an array with the calling string as a single element, and any other value (including null) without a Symbol.split method is converted to a string (eg. null is "null", and a RegExp is used as the pattern) which is used to split the string.

      • Optionallimit: number

        A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

        • The array may contain fewer entries than limit if the end of the string is reached before the limit is reached.
        • If limit is 0, [] is returned.

      Returns string[]

      An Array of strings, split at each point where the separator occurs in the given string.

      0.9.1

      const splitByNumber = {
      [getKnownSymbol<typeof Symbol.split>(WellKnownSymbols.split)]: (str: string) => {
      let num = 1;
      let pos = 0;
      const result = [];
      while (pos < str.length) {
      const matchPos = strIndexOf(str, asString(num), pos);
      if (matchPos === -1) {
      result.push(strSubstring(str, pos));
      break;
      }
      result.push(strSubstring(str, pos, matchPos));
      pos = matchPos + asString(num).length;
      num++;
      }
      return result;
      }
      };

      const myString = "a1bc2c5d3e4f";
      console.log(polyStrSymSplit(myString, splitByNumber)); // [ "a", "bc", "c5d", "e", "f" ]