jonschlinkert / word-wrap

Wrap words to a specified length.
https://github.com/jonschlinkert
MIT License
194 stars 57 forks source link

Wrapping multiline text #27

Open fcastilloec opened 6 years ago

fcastilloec commented 6 years ago

I'm having trouble understanding how this module works with multiline text and I feel the current behavior has a bug. Here's an example:

const wrap = require('word-wrap')
const test = `Highlights:\n First\n Second\n Third`

result = wrap(test, {width: 80}, indent: '*');

The output I get is *Highlights:\n \n*First\n \n*Second\n \n*Third but what I expected was *Highlights:\n\n* First\n\n* Second\n\n* Third. Why are the spaces after a newline being included in the previous line? Is this a bug?

Deide commented 6 years ago

Shouldn't it return *Highlights:\n* First\n* Second\n* Third? Why would it add more line breaks to lines that are already below a width of 80? In fact, that is what it does if there's not a space after the newline - seems like a bug to behave this way when there's a space. Maybe there should be an option to trim each line; I think the current trim just does the whole thing.

Other than that, there are also cases where it's desirable to have the indent added on blank lines (for example, when you have "\n\n" in the source text).