1
0
mirror of https://github.com/thangisme/notes.git synced 2024-11-12 17:06:16 -05:00
notes/node_modules/yargs/lib/wordwrap.js
Patrick Marsceill b7b0d0d7bf
Initial commit
2017-03-09 13:16:08 -05:00

50 lines
1.4 KiB
JavaScript

// Simplified version of https://github.com/substack/node-wordwrap
'use strict';
module.exports = function (start, stop) {
if (!stop) {
stop = start;
start = 0;
}
var re = /(\S+\s+)/;
return function (text) {
var chunks = text.toString().split(re);
return 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;
}
return lines;
}, [ new Array(start + 1).join(' ') ]).join('\n');
};
};