Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "main"

Index

Enumerations

Interfaces

Functions

Functions

Const stringBreaker

  • Breaks a string into a string array

    Parameters

    • str: string

      The string to break into string array

    • Optional opt: IStringBreakOpt | number

      parameters to affect the output. Can be number or IStringBreakOpt If a number is passed in it becomes the width for the output. If no parameter is passed then output will be broken into string array with 80 characters per element.

      let x: string[];
      x = stringBreaker('some long text');
      // is the same as
      x = stringBreaker('some long text', 80),
      // is the same as
      x = stringBreaker('some long text' {width: 80});

      Example:

      
      import { stringBreaker } from 'string-breaker';
      
      let x = stringBreaker('The quick brown fox jumped over the lazy dog', 5);
      // x => ['The q','uick ','brown',' fox ','jumpe','d ove','r the',' lazy',' dog']
      
      x = stringBreaker('Hello World\nNice ๐Ÿ˜‡\nhmm... ', 5);
      // x => ['Hello', ' Worl', 'dNice', ' ๐Ÿ˜‡hmm', '... ']
      
      x = stringBreaker('\uD83D\uDE07Hello World\nNice ๐Ÿ˜‡\nhmm...', 6);
      // x => ['๐Ÿ˜‡Hello', ' World', 'Nice ๐Ÿ˜‡', 'hmm...']
      
      x = stringBreaker('\uD83D\uDE07Hello World\nNice ๐Ÿ˜‡\r\nhmm...', {
          width: 6,
          lnEnd: lnEndOpt.encode
          });
      // x => ['๐Ÿ˜‡Hello', ' World', '\\nNice', ' ๐Ÿ˜‡\\nhm', 'm...']

      Split by End of Line stringBreaker can split by eol by setting option lnEnd: lnEndOpt.splitByEol

      Example Splitting by End of Line:

      import { stringBreaker } from 'string-breaker';
      
      // mixing \n and \r will result in the same output
      let strSrc = 'Happy cat.';
      strSrc += '\nThe quick brown fox jumped over the lazy dog.';
      strSrc += '\r\nThe moon is full tonight.\rI like full moons!';
      
      const x = stringBreaker(strSrc, { splitOpt: splitByOpt.line });
      // x => [
      //  'Happy cat.',
      //  'The quick brown fox jumped over the lazy dog.',
      //  'The moon is full tonight.',
      //  'I like full moons!' ]

      Example splitting by Word:

      import { stringBreaker } from 'string-breaker';
      
      // mixing \n and \r will result in the same output
      let strSrc = 'Happy cat.';
      strSrc += '\nThe quick   brown\t\t fox jumped over the lazy dog.';
      strSrc += '\r\nThe moon is full tonight.\rI like full moons!';
      
      const x = stringBreaker(strSrc, { splitOpt: splitByOpt.word });
      // x => [ 'Happy','cat.','The','quick','brown','fox','jumped',
      //        'over','the','lazy','dog.','The','moon','is','full',
      //        'tonight.','I','like','full','moons!' ]
      

      Example split by width and preserve words

       
      When split using width and flag widthFlags.nearestWord the elements in the array will split where there is a whitespace and not before. If the whitespace is a printing char as in the case of \u1680 then it will be include at the end of the element: Otherwise the whitespace is removed from the end of the element;
       
      Elemnets will not start with a whitespace unless that whitespace happens to be a printalbe whitespace such as \u1680. For practical purposes all lines will not start with a whitespace.
      import { stringBreaker } from 'string-breaker';
      
      var str = 'On this\u1680day.\u1680For this morning, when Gregor\u3000Samsa woke from troubled dreams; he found himself transformed.';
      const result: string[] = stringBreaker(str, { width: 10, lenOpt: widthFlags.nearestWord });
      const strResult = result.join('\n');
      console.log(strResult);
      On thisแš€day. 
      For this morning,
      when Gregor
      Samsa woke
      from troubled
      dreams; he
      found himself
      transformed.

    Returns string[]

Generated using TypeDoc