jonschlinkert / word-wrap

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

feature request: able specify different indent for the first line #17

Open jonathon-love opened 7 years ago

jonathon-love commented 7 years ago

hey, it would be super handy if a different indent could be specified for the first line, i.e.

  Lorem ipsum dolor sit amet, consectetur adipiscing
      elit, sed do eiusmod tempor incididunt ut labore
      et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut
      aliquip ex ea commodo consequat.

thanks, and keep up the good work!

Whobeu commented 3 years ago

This is something I was looking for too and it was rather trivial to add to the module and option called "trimstart" after the "options.trim" check:

if (options.trimstart === true) {
    result = result.replace(/^\s*/, '');
}

With the new option added to my code:

const wrapped = wrap(text, { indent: '      ', width: 60, trimstart: true });
console.log(`Text: ${wrapped}`);

The output is:

Text: Sit cillum sunt id ullamco esse aliquip ullamco fugiat
      cillum irure aute. Eiusmod voluptate ea adipisicing aliquip
      minim irure commodo adipisicing voluptate. Magna Lorem est
      ea incididunt nisi et aute eiusmod excepteur et irure
      pariatur non. Pariatur ut labore non mollit est adipisicing
      fugiat dolore minim. Dolor est veniam ullamco est.

Of course I could skip modifying the word-wrap module and apply the regex (or newer trimStart() method) to the resultant "wrapped" string instead:

console.log(`Text: ${wrapped.replace(/^\s*/, '')}`);
console.log(`Text: ${wrapped.trimStart()}`);