Permalink
Browse files

passing hard break test with /\b/

  • Loading branch information...
1 parent bfd5fa0 commit 811b8736168b7e92cee63e19030f6b1ad59bcba6 @substack committed Aug 26, 2011
Showing with 20 additions and 7 deletions.
  1. +12 −2 index.js
  2. +8 −5 test/break.js
View
@@ -18,10 +18,20 @@ var wordwrap = module.exports = function (start, stop, params) {
if (!params) params = {};
var mode = params.mode || 'soft';
+ var re = mode === 'hard' ? /\b/ : /(\S+\s+)/;
return function (text) {
- return text.toString().split(/(\S+\s+)/)
- .reduce(function (lines, rawChunk) {
+ var chunks = text.toString()
+ .split(re)
+ .reduce(function (acc, x) {
+ for (var i = 0; i < x.length; i += stop - start) {
+ acc.push(x.slice(i, i + stop - start));
+ }
+ return acc;
+ }, [])
+ ;
+
+ return chunks.reduce(function (lines, rawChunk) {
if (rawChunk === '') return lines;
var chunk = rawChunk.replace(/\t/g, ' ');
View
@@ -1,15 +1,18 @@
var assert = require('assert');
var wordwrap = require('../');
-exports.json = function () {
+exports.hard = function () {
var wrap = wordwrap.hard(80);
var s = 'Assert from {"type":"equal","ok":false,"found":1,"wanted":2,'
+ '"stack":[],"id":"b7ddcd4c409de8799542a74d1a04689b",'
+ '"browser":"chrome/6.0"}'
;
+ var s_ = wordwrap.hard(80)(s);
- assert.equal(
- wordwrap.hard(80)(s),
- s.slice(0,80) + '\n' + s.slice(80)
- );
+ var lines = s_.split('\n');
+ assert.equal(lines.length, 2);
+ assert.ok(lines[0].length < 80);
+ assert.ok(lines[1].length < 80);
+
+ assert.equal(s, s_.replace(/\n/g, ''));
};

0 comments on commit 811b873

Please sign in to comment.