Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrap function #410

Merged
merged 1 commit into from
Jul 1, 2015
Merged

Add wrap function #410

merged 1 commit into from
Jul 1, 2015

Conversation

bsteephenson
Copy link
Contributor

Adds a line wrapping function that limits line length to a certain width
In response to issue #278

Let me know if there's anything more I should do (more tests, documentation, etc). I'm glad to help out.

@stoeffel
Copy link
Collaborator

stoeffel commented Mar 4, 2015

I will have a look at it on the WE.Can you add the documentation to the README.markdown? Thanks for the PR.

equal(_.wrap("My name is", 2, '.', false), "My .name .is", 'works with width 2 and cut = false');
equal(_.wrap("My name is", 2, '.', true), "My. n.am.e .is", 'works with width 2 and cut = true');
equal(_.wrap("My name is", 3, '.', false), "My .name .is", 'works with width 3 and cut = true');
equal(_.wrap("My name is", 3), "My \nname \nis", 'Default parameters work');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add more tests.

  • edgecases
  • defaults

Checkout the other tests.

@stoeffel
Copy link
Collaborator

stoeffel commented May 2, 2015

Can you rebase this? Thanks

@bsteephenson
Copy link
Contributor Author

Rebased with a mocha test
Let me know if there's anything else I should do =)

@stoeffel
Copy link
Collaborator

stoeffel commented May 2, 2015

If you add this test:
equal(wrap("My name is", -1), "My name is", "Just return original line if width <= 0")
You have 100% coverage.

Otherwise it looks good to me.

@bsteephenson
Copy link
Contributor Author

Done and squashed.

return str;
}
else if(!cut){
words = str.split(" ");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use `underscore.string/words' here?

@bsteephenson
Copy link
Contributor Author

Let's talk about the specifics of what this is supposed to do.

wrap('this is a sentence', 4, \n, false)

What should this return? Right now it would return 'this \nis a \nsentence' which I'm realizing now doesn't make much sense. Would 'this\nis a\nsentence' be more obvious?

@stoeffel
Copy link
Collaborator

stoeffel commented May 4, 2015

Would 'this\nis a\nsentence' be more obvious?

Yes, I think so.

PHP's wordwrap also drops spaces at the end. http://us3.php.net/manual/en/function.wordwrap.php
We could add an option for this, but it already has 4 arguments...

@esamattis
Copy link
Owner

Options object maybe. Could be done as backwards compatible with type checking.

@stoeffel
Copy link
Collaborator

stoeffel commented May 6, 2015

👍 for an options object I could think of some other options we could add in the future f.e. dontBreakHTMLTags

@bsteephenson
Copy link
Contributor Author

Now wrap('this is a sentence', {width: 4, seperator: '\n', cut: false}) returns 'this\nis a\nsentence'

@stoeffel
Copy link
Collaborator

Options object looks good. Could you add an option for dropping trailing spaces as discussed above? And updat the readme? Thanks for your work

@bsteephenson
Copy link
Contributor Author

I added a trailingSpaces options which, if true, fills each line with trailing spaces to make them at least width long (I'm not sure if this is what you meant). I also updated the Readme.

@stoeffel
Copy link
Collaborator

Sorry, I actually meant this:

What should this return? Right now it would return 'this \nis a \nsentence' which I'm realizing now doesn't make much sense. Would 'this\nis a\nsentence' be more obvious?

Maybe call the option preserveSpace. Sry for the confusion.

@stoeffel
Copy link
Collaborator

but the trailingSpaces option is nice too 👍

@stoeffel
Copy link
Collaborator

@bsteephenson Would be great if you would add the preserveSpaces (or take a better name) option so we can merge this.

Thanks

@bsteephenson
Copy link
Contributor Author

Working on this atm. I am having preserveSpaces have a higher precedence over trailingSpaces.

@@ -269,6 +269,25 @@ lines("Hello\nWorld");
// => ["Hello", "World"]
```

#### wrap(str, options) => string

Splits a line `str` (default '') into several lines of size `options.width` (default 75) using a `options.seperator` (default '\n'). If `options.trailingSpaces` is true, make each line at least `width` long using trailing spaces. If `options.cut` is true, create new lines in the middle of words. If `options.trailingSpaces` is true, preserve the space that should be there at the end of a line.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options.trailingSpaces is true, preserve the space that should be there at the end of a line.

This should be options.preserveSpaces

@stoeffel
Copy link
Collaborator

Besides my comments in the readme, this looks good to me. Thx a lot

@bsteephenson
Copy link
Contributor Author

Corrected all the oops's in Readme
Let me know if you want me to squash all the commits into one

@stoeffel
Copy link
Collaborator

Squashing would be nice. I want have time today to check it one more time. But I will review it again in the beginning of next week and then merge it.
Thanks for your work.

@stoeffel
Copy link
Collaborator

Did check it again. Could you squash? I will merge it.

Adds a line wrapping function that limits line length to a certain width
Added documentation and tests
@bsteephenson
Copy link
Contributor Author

Done =)

stoeffel added a commit that referenced this pull request Jul 1, 2015
@stoeffel stoeffel merged commit 6b3a278 into esamattis:master Jul 1, 2015
@stoeffel
Copy link
Collaborator

stoeffel commented Jul 1, 2015

nice thanks a lot.

@cahrens
Copy link
Contributor

cahrens commented Feb 19, 2016

@stoeffel when "wrap" was added, it wasn't added to the "exports" function as a conflict with underscore. I know mixing in underscore.string is now discouraged, but unfortunately we have a lot of old code written with the mixin paradigm. I am having difficulty upgrading underscore.string from 2.x due to the wrap conflict.

@stoeffel
Copy link
Collaborator

Hey @cahrens. Good catch. Would be awesome if you could open a PR to include wrap in the exports.
I want to merge #481 and #482 first, before I publish the next version (if you have time and willing to review these two PRs would be cool).

Thanks

@cahrens
Copy link
Contributor

cahrens commented Feb 19, 2016

Sure, I will looking into it this weekend. I would like to add a test that checks for conflicting functions between underscore and underscore.string.exports. I haven't looked at your test framework yet, but hope it's possible to add a test-only dependency on underscore.

@cahrens
Copy link
Contributor

cahrens commented Feb 19, 2016

@stoeffel I see that underscore is already a development dependency. Can you point me to documentation on how to run the tests locally? I poked around but couldn't find anything.

cahrens pushed a commit to cahrens/underscore.string that referenced this pull request Feb 20, 2016
Fixes a bug introduced in esamattis#410.
cahrens pushed a commit to cahrens/underscore.string that referenced this pull request Feb 20, 2016
Fixes a bug introduced in esamattis#410.
cahrens pushed a commit to cahrens/underscore.string that referenced this pull request Feb 20, 2016
Fixes a bug introduced in esamattis#410.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants