«-(¯`v´¯)-« 【🇸🇹🇷🇮🇳🇬-🇧🇷🇪🇦🇰🇪🇷】 »-(¯`v´¯)-»
string-breaker
Break string into array
- string array with elements that are a specific width
- string array with elements for each line
- string array with element fo reach word
Documentation
For more information view the Documentation
Repository
View the source code on github
Install
npm install --save string-breaker
Usage
; ;// 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...', ;// x => ['😇Hello', ' World', '\\nNice', ' 😇\\nhm', 'm...'] console.logx.join'\n';// 😇Hello// World// \nNice// 😇\nhm// m... x = stringBreaker'\uD83D\uDE07Hello World\nNice 😇\nhmm...', ;// x => [ '😇Hello', ' World', '\nNice ', '😇\nhmm.', '..' ]console.logx.join'\n';// 😇Hello// World//// Nice // 😇// hmm.// .. // replace all extra space with a single spacex = stringBreaker'\uD83D\uDE07 Hello World\nNice 😇 \r\nhmm...', ;// x => ['😇 Hel', 'lo Wo', 'rldNi', 'ce 😇h', 'mm...']
Split by End of Line
stringBreaker can split by eol by setting option splitOpt: splitByOpt.line
; // mixing \n and \r will result in the same output;strSrc += '\nThe quick brown fox jumped over the lazy dog.';strSrc += '\r\nThe moon is full tonight.\rI like full moons!'; ;// x => [// 'Happy cat.',// 'The quick brown fox jumped over the lazy dog.',// 'The moon is full tonight.',// 'I like full moons!' ]
Split by Words stringBreaker can split into words by setting option splitOpt: splitByOpt.word
; // mixing \n and \r will result in the same output;strSrc += '\nThe quick brown\t\t fox jumped over the lazy dog.';strSrc += '\r\nThe moon is full tonight.\rI like full moons!'; ;// x => [ 'Happy','cat.','The','quick','brown','fox','jumped',// 'over','the','lazy','dog.','The','moon','is','full',// 'tonight.','I','like','full','moons!' ]
stringBreaker by default will use code points to build output; however, this can be switched to character length by setting lenOpt of stringBreaker options.
With options is set to fullwitdh the output is built based upon if the code point for each char that is fullwidth will take two positions.
; ;;// x => [ '!可以看到他', '的棕色腹部' ] x = stringBreakerstr , ;// all but the first char is a fullwidth// x => [// '!可以看', // \u21 \u53EF \u4EE5 \u770B// '到他的', // \u5230 \u4ED6 \u7684// '棕色腹', // \u68D5 \u8272 \u8179// '部' // \u90E8// ]
With options is set to surrogatePair the output is built based upon if the code point for each char that is surrogate pair will take two positions.
; ;console.logstr.length; // 12 all surrogate pairs ;// x => [ '🧀😊😀', '😃😄😁' ] x = stringBreakerstr , ;// all the characters are surrogate pair// because widthFlags.surrogatePair is set the output// string array returns 6 not 5 due to surrogate pairs not breakable.// x => [// { surrogate } | { surrogate } | { surrogate }// '🧀😊😀', // \uD83E \uDDC0 | \uD83D \uDE0A | \uD83D \uDE00// '😃😄😁' // \uD83D \uDE03 | \uD83D \uDE04 | \uD83D \uDE01// ]
widthFlags can be set to combine options
; ;console.logstr.length; // 11 mix of surrogate pair, halfwidth and fullwidth ;// x => [ '🧀😊😀棕', '腹部!!' ] x = stringBreakerstr , ;// mix of surrogate pair, fullwidth and halfwidth// x => [// '🧀😊', // \uD83E \uDDC0 \uD83D \uDE0A two surrogate// '😀棕', // \uD83D \uDE00 \u68D5 one surrogate and one fullwidth// '腹部', // \u8179 \u90E8 two fullwidth// '!!' // \u21 \u21 two halfwidth// ]
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.
; ;;;console.logstrResult;
On this day.For this morning,when GregorSamsa wokefrom troubleddreams; hefound himselftransformed.