Divide the lines of a string in a lines object array
This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies
:
npm install --save-dev divide-lines
const divideLines = require('divide-lines');
console.log(divideLines('\n\rHello \r\n\n The\n\nNice World '));
/* =>
{
originalString: '\n\rHello \r\n\n The\n\nNice World ',
lines: [
{
lineBreak: '\n',
originalLine: '\n'
},
{
whitespaceStart: '\r',
whitespaceEnd: ' ',
content: 'Hello',
lineBreak: '\r\n',
originalLine: '\rHello \r\n'
},
{
lineBreak: '\n',
originalLine: '\n'
},
{
whitespaceStart: ' ',
content: 'The',
lineBreak: '\n',
originalLine: ' The\n'
},
{
lineBreak: '\n',
originalLine: '\n'
},
{
whitespaceEnd: ' ',
content: 'Nice World',
originalLine: 'Nice World '
}
]
}
*/
Divide the lines of a string in a lines object Returns a object
.
Type: string
The string to divide.
Type: object
The lines object.
index.js
:
const divideLines = require('divide-lines'); console.log(divideLines('')); //=> { originalString: '', lines: [ { originalLine: '' } ] } console.log(divideLines('a')); /* => { originalString: 'a', lines: [ { content: 'a', originalLine: 'a' } ] } */ console.log(divideLines('\n\rHello \r\n\n The\n\nNice World ')); /* => { originalString: '\n\rHello \r\n\n The\n\nNice World ', lines: [ { lineBreak: '\n', originalLine: '\n' }, { whitespaceStart: '\r', whitespaceEnd: ' ', content: 'Hello', lineBreak: '\r\n', originalLine: '\rHello \r\n' }, { lineBreak: '\n', originalLine: '\n' }, { whitespaceStart: ' ', content: 'The', lineBreak: '\n', originalLine: ' The\n' }, { lineBreak: '\n', originalLine: '\n' }, { whitespaceEnd: ' ', content: 'Nice World', originalLine: 'Nice World ' } ] } */
MIT