Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix several bugs listed in Issue #3 #4

Open
wants to merge 1 commit into from

1 participant

Commits on Aug 9, 2013
  1. @AnAppAMonth
This page is out of date. Refresh to see the latest.
Showing with 70 additions and 33 deletions.
  1. +31 −33 index.js
  2. +39 −0 test/bugs002.js
View
64 index.js
@@ -4,22 +4,23 @@ var wordwrap = module.exports = function (start, stop, params) {
start = params.start;
stop = params.stop;
}
-
+
if (typeof stop === 'object') {
params = stop;
start = start || params.start;
stop = undefined;
}
-
+
if (!stop) {
stop = start;
start = 0;
}
-
+
if (!params) params = {};
var mode = params.mode || 'soft';
var re = mode === 'hard' ? /\b/ : /(\S+\s+)/;
-
+ var prefix = new Array(start + 1).join(' ');
+
return function (text) {
var chunks = text.toString()
.split(re)
@@ -33,39 +34,36 @@ var wordwrap = module.exports = function (start, stop, params) {
return acc;
}, [])
;
-
- return chunks.reduce(function (lines, rawChunk) {
+
+ chunks = chunks.reduce(function (lines, rawChunk) {
if (rawChunk === '') return lines;
-
+
var chunk = rawChunk.replace(/\t/g, ' ');
-
var i = lines.length - 1;
- if (lines[i].length + chunk.length > stop) {
- lines[i] = lines[i].replace(/\s+$/, '');
-
- chunk.split(/\n/).forEach(function (c) {
- lines.push(
- new Array(start + 1).join(' ')
- + c.replace(/^\s+/, '')
- );
- });
- }
- else if (chunk.match(/\n/)) {
- var xs = chunk.split(/\n/);
- lines[i] += xs.shift();
- xs.forEach(function (c) {
- lines.push(
- new Array(start + 1).join(' ')
- + c.replace(/^\s+/, '')
- );
- });
- }
- else {
- lines[i] += chunk;
- }
-
+ var xs = chunk.split(/\n/),
+ curr = xs[0];
+
+ if (!(lines[i].length + curr.length > stop &&
+ lines[i].length + curr.replace(/\s+$/, '').length > stop &&
+ lines[i].length > start)) {
+ lines[i] += xs.shift();
+ }
+
+ xs.forEach(function (c) {
+ if (i === lines.length -1 ) {
+ lines[i] = lines[i].replace(/\s+$/, '');
+ if (!lines[i]) lines[i] = prefix;
+ }
+ lines.push(prefix + c.replace(/^\s+/, ''));
+ });
+
return lines;
- }, [ new Array(start + 1).join(' ') ]).join('\n');
+ }, [ prefix ]);
+
+ var last = chunks.length - 1;
+ chunks[last] = chunks[last].replace(/\s+$/, '');
+ if (!chunks[last]) chunks[last] = prefix;
+ return chunks.join('\n');
};
};
View
39 test/bugs002.js
@@ -0,0 +1,39 @@
+var assert = require('assert');
+var wordwrap = require('../');
+
+// Exposes bugs in wordwrap 0.0.2
+exports.bug1 = function() {
+ var text = "I love u\n\n\n";
+ var res = wordwrap(10)(text);
+ assert.equal(res, "I love u\n\n\n");
+};
+
+exports.bug2 = function() {
+ var text = "I hug you, my friend";
+ var res = wordwrap(10)(text);
+ assert.equal(res, "I hug you,\nmy friend");
+};
+
+exports.bug3 = function() {
+ var text = "I love u\n\n\n";
+ var res = wordwrap.hard(10)(text);
+ assert.equal(res, "I love u\n\n\n");
+};
+
+exports.bug4 = function() {
+ var text = "I hug you, my friend";
+ var res = wordwrap.hard(10)(text);
+ assert.equal(res, "I hug you,\nmy friend");
+};
+
+exports.bug5 = function() {
+ var text = "12345678910";
+ var res = wordwrap(10)(text);
+ assert.equal(res, "12345678910");
+};
+
+exports.bug6 = function() {
+ var text = "\n12345678910\n";
+ var res = wordwrap(1, 11)(text);
+ assert.equal(res, " \n 12345678910\n ");
+};
Something went wrong with that request. Please try again.