Coder Social home page Coder Social logo

word-wrap's Introduction

word-wrap NPM version NPM monthly downloads NPM total downloads Linux Build Status

Wrap words to a specified length.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save word-wrap

Usage

var wrap = require('word-wrap');

wrap('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.');

Results in:

  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.

Options

image

options.width

Type: Number

Default: 50

The width of the text before wrapping to a new line.

Example:

wrap(str, {width: 60});

options.indent

Type: String

Default: `` (two spaces)

The string to use at the beginning of each line.

Example:

wrap(str, {indent: '      '});

options.newline

Type: String

Default: \n

The string to use at the end of each line.

Example:

wrap(str, {newline: '\n\n'});

options.escape

Type: function

Default: function(str){return str;}

An escape function to run on each line after splitting them.

Example:

var xmlescape = require('xml-escape');
wrap(str, {
  escape: function(string){
    return xmlescape(string);
  }
});

options.trim

Type: Boolean

Default: false

Trim trailing whitespace from the returned string. This option is included since .trim() would also strip the leading indentation from the first line.

Example:

wrap(str, {trim: true});

options.cut

Type: Boolean

Default: false

Break a word between any two letters when the word is longer than the specified width.

Example:

wrap(str, {cut: true});

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
47 jonschlinkert
7 OlafConijn
3 doowb
2 aashutoshrathi
2 lordvlad
2 hildjj
1 danilosampaio
1 2fd
1 leonard-thieu
1 mohd-akram
1 toddself
1 wolfgang42
1 zachhale

Author

Jon Schlinkert

License

Copyright © 2023, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on July 22, 2023.

word-wrap's People

Contributors

2fd avatar aashutoshrathi avatar danilosampaio avatar doowb avatar hildjj avatar jonschlinkert avatar leonard-thieu avatar lordvlad avatar mohd-akram avatar olafconijn avatar toddself avatar wolfgang42 avatar zachhale avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

word-wrap's Issues

Trim trailing whitespace

Right now it leaves trailing whitespace at the end of the broken lines. An option to fix this would be very nice!

Question: how should this library handle undefined/null ?

This is more a question than an issue.

I was looking at the tests and wasn't able to answer the following question: how should word-wrap handle the nullor undefined case ?

I saw this in the index.js

if (str == null) {
    return str;
}

Don't you think this should return an empty string ?

Feature Request: param "Ignore" based on pattern

A handy feature would be to ignore wrapping based on whether or not the line in the string that was being processed matched a certain pattern (or a pattern in an array of patterns).

My use case is that I have a large block of markdown text that is stored as a string in Javascript, and then I'd like to output that to a human readable markdown file. For most all cases, this library works wonderfully, however, in the case of markdown tables, I'd like for the word wrapping not to happen, even though they are longer than the specified width. An option to ignore based on a pattern that would match lines in a markdown table (i.e. start and end with |) would be very useful.

Another wrinkle here is that I'm also supplying an indent, and would like the indent to still apply for that line.

Any interest in this? Either way, thanks for the tool!

word wrap throws an exception if the regex doesn't match anything on line 24

input: \r\n returns:
↳ npm test

[email protected] test /Users/todd/src/scripto2/node_modules/word-wrap
mocha -R spec

wrap
✓ should use defaults to wrap words in the given string:
✓ should wrap to the specified width:
✓ should indent the specified amount:
✓ should use the given string for newlines:
✓ should trim trailing whitespace:
1) should handle strings with just newlines

5 passing (9ms)
1 failing

  1. wrap should handle strings with just newlines:
    TypeError: Cannot read property 'join' of null
    at module.exports (/Users/todd/src/scripto2/node_modules/word-wrap/index.js:25:35)
    at Context. (/Users/todd/src/scripto2/node_modules/word-wrap/test.js:38:5)
    at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
    at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
    at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)
    at /usr/local/lib/node_modules/mocha/lib/runner.js:452:12
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)
    at /usr/local/lib/node_modules/mocha/lib/runner.js:309:7
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)
    at Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
    at processImmediate as _immediateCallback

Feature request: Option to disallow orphaned word

In the case where width is passed, it'd be nice to also have the option to disallow orphaned words

I would like to create a pull request for this feature, but first wanted to make sure you'd be on-board with the option.

  • Definition of 'word' could be handled by importing the lodash.words package

feature request: able specify different indent for the first line

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!

Add options to break words if encounters long words.

When we meet a paragraph contains some very long words:

this is a veryveryveryveryveryveryverylonglongloooooongword, what should we do to handle this veryveryveryveryverylooooongword?

Should it look like this under specific width:

this is a veryveryveryveryveryveryvery-
longlongloooooongword, what should 
we do to handle this veryveryveryvery-
verylooooongword?

cut option eats newlines

Expected:

wrap('a\n\nb', {indent: '', cut: true}) == 'a\n\nb'

Actual:

wrap('a\n\nb', {indent: '', cut: true}) == 'a\nb'

Should not indent by default

I think having a default indent of 2 spaces is very confusing given that isn't the library's primary function.

Wrapping multiline text

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?

Give admin privileges to somebody else

It seems like @jonschlinkert is a busy guy without much desire to keep this thing up to date or respond to inquiries on this. The most recent PR #33 has several individuals who would put the care into ensuring that code in this repo is solid, secure and up to date. @jonschlinkert, please give somebody else privileges to merge pull requests for the sake of the 1400+ projects that depend on word-wrap and 29M+ weekly downloads.

@jonschlinkert, if you're going to reasonably say in your bio... "I've created more than 1,000 open source projects in an effort to reach my goal. Open source software takes a lot of time to create and maintain, and millions of projects now depend on my code." then be a help to others in the community who depend on your code by allowing others to aid in maintaining it if you can't maintain it yourself.

(I realize that this is a shot in the dark, just trying as many avenues as possible to get @jonschlinkert's attention)

Spaces not outputting as expected.

I ran this code in NodeJS console using npm version "^1.2.3"

var wrap = require('word-wrap');
var test = '1234 12345678';
var arguments = {width: 10, indent: ' ', newline: ' \n'};
var result = wrap(test, arguments);
result

In the following I will use '-' to represent a space.

I expected result to output -1234-\n-12345678 however the output of result was -1234--\n12345678.

Can someone explain why result has more and less spaces than I expected or confirm that this is a bug.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.