-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a line wrapping function that limits line length to a certain width Added documentation and tests
- Loading branch information
1 parent
09ba69b
commit 4c8a7b0
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var equal = require('assert').equal; | ||
var wrap = require('../wrap'); | ||
|
||
test('#wrap', function(){ | ||
|
||
// without trailing spaces | ||
equal(wrap("My name is", { width: 2, seperator:'.', cut:false, trailingSpaces:false } ), "My.name.is", 'works with width 2 and cut = false'); | ||
equal(wrap("My name is", { width: 2, seperator:'.', cut:true, trailingSpaces:false } ), "My. n.am.e .is", 'works with width 2 and cut = true'); | ||
equal(wrap("My name is", { width: 3, seperator:'.', cut:false, trailingSpaces:false } ), "My.name.is", 'works with width 3 and cut = true'); | ||
equal(wrap("My name is", { width: 3, seperator:'.', cut:true, trailingSpaces:false } ), "My .nam.e i.s", 'works with width 3 and cut = true'); | ||
|
||
// with trailing spaces | ||
equal(wrap("My name is", { width: 2, seperator:'.', cut:false, trailingSpaces:true } ), "My.name.is", 'works with width 2 and cut = false and trailingSpaces = true'); | ||
equal(wrap("My name is", { width: 2, seperator:'.', cut:true, trailingSpaces:true } ), "My. n.am.e .is", 'works with width 2 and cut = true and trailingSpaces = true'); | ||
equal(wrap("My name is", { width: 3, seperator:'.', cut:false, trailingSpaces:true } ), "My .name.is ", 'works with width 3 and cut = true and trailingSpaces = true'); | ||
equal(wrap("My name is", { width: 3, seperator:'.', cut:true, trailingSpaces:true } ), "My .nam.e i.s ", 'works with width 3 and cut = true and trailingSpaces = true'); | ||
|
||
// with preserveSpaces | ||
equal(wrap('My name is', {width: 2, seperator:'.', cut:false, preserveSpaces:true }), 'My .name .is', 'preserve spaces keeps the space at the end of a line'); | ||
equal(wrap('My name is', {width: 3, seperator:'.', cut:false, preserveSpaces:true }), 'My .name .is', 'preserve spaces keeps the space at the end of a line'); | ||
|
||
// with preserveSpaces and trailingSpaces | ||
equal(wrap('My name is', {width: 2, seperator:'.', cut:false, preserveSpaces:true, trailingSpaces:true }), 'My .name .is', 'preserve spaces takes precedence over trailing spaces'); | ||
|
||
|
||
// defaults | ||
equal(wrap("My name is", { width: 3 } ), "My\nname\nis", 'Default parameters work'); | ||
equal(wrap("My name is"), "My name is", 'Default parameters work'); | ||
equal(wrap("", { width: 5 } ), "", 'Empty string'); | ||
equal(wrap("My name is", { width: 0 } ), "My name is", "Just return original line if width <= 0"); | ||
equal(wrap("My name is", { width: -1 } ), "My name is", "Just return original line if width <= 0"); | ||
equal(wrap(null, { width: 5 } ), "", 'null'); | ||
equal(wrap(undefined, { width: 5 } ), "", 'undefined'); | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Wrap | ||
// wraps a string by a certain width | ||
|
||
makeString = require('./helper/makeString'); | ||
|
||
module.exports = function wrap(str, options){ | ||
str = makeString(str); | ||
|
||
options = options || {}; | ||
|
||
width = options.width || 75; | ||
seperator = options.seperator || '\n'; | ||
cut = options.cut || false; | ||
preserveSpaces = options.preserveSpaces || false; | ||
trailingSpaces = options.trailingSpaces || false; | ||
|
||
if(width <= 0){ | ||
return str; | ||
} | ||
|
||
else if(!cut){ | ||
|
||
words = str.split(" "); | ||
result = ""; | ||
current_column = 0; | ||
|
||
while(words.length > 0){ | ||
|
||
// if adding a space and the next word would cause this line to be longer than width... | ||
if(1 + words[0].length + current_column > width){ | ||
//start a new line if this line is not already empty | ||
if(current_column > 0){ | ||
// add a space at the end of the line is preserveSpaces is true | ||
if (preserveSpaces){ | ||
result += ' '; | ||
current_column++; | ||
} | ||
// fill the rest of the line with spaces if trailingSpaces option is true | ||
else if(trailingSpaces){ | ||
while(current_column < width){ | ||
result += ' '; | ||
current_column++; | ||
} | ||
} | ||
//start new line | ||
result += seperator; | ||
current_column = 0; | ||
} | ||
} | ||
|
||
// if not at the begining of the line, add a space in front of the word | ||
if(current_column > 0){ | ||
result += " "; | ||
current_column++; | ||
} | ||
|
||
// tack on the next word, update current column, a pop words array | ||
result += words[0]; | ||
current_column += words[0].length; | ||
words.shift(); | ||
|
||
} | ||
|
||
// fill the rest of the line with spaces if trailingSpaces option is true | ||
if(trailingSpaces){ | ||
while(current_column < width){ | ||
result += ' '; | ||
current_column++; | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
else { | ||
|
||
index = 0; | ||
result = ""; | ||
|
||
// walk through each character and add seperators where appropriate | ||
while(index < str.length){ | ||
if(index % width == 0 && index > 0){ | ||
result += seperator; | ||
} | ||
result += str.charAt(index); | ||
index++; | ||
} | ||
|
||
// fill the rest of the line with spaces if trailingSpaces option is true | ||
if(trailingSpaces){ | ||
while(index % width > 0){ | ||
result += ' '; | ||
index++; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; |