Skip to content

forresst/divide-lines

Repository files navigation

divide-lines

Divide the lines of a string in a lines object array

Coverage Status version node-version downloads

MIT License PRs Welcome Code of Conduct XO code style

Watch on GitHub Star on GitHub

Table of Contents

Installation

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

Usage

Node.js

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  '
			}
		]
	}
*/

API

divideLines(input)

Divide the lines of a string in a lines object Returns a object.

input

Type: string

The string to divide.

return

Type: object

The lines object.

Example

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  '
  		}
  	]
  }
*/

LICENSE

MIT