react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / escodegen / node_modules / optionator / node_modules / wordwrap / index.js
80642 viewsvar wordwrap = module.exports = function (start, stop, params) {1if (typeof start === 'object') {2params = start;3start = params.start;4stop = params.stop;5}67if (typeof stop === 'object') {8params = stop;9start = start || params.start;10stop = undefined;11}1213if (!stop) {14stop = start;15start = 0;16}1718if (!params) params = {};19var mode = params.mode || 'soft';20var re = mode === 'hard' ? /\b/ : /(\S+\s+)/;2122return function (text) {23var chunks = text.toString()24.split(re)25.reduce(function (acc, x) {26if (mode === 'hard') {27for (var i = 0; i < x.length; i += stop - start) {28acc.push(x.slice(i, i + stop - start));29}30}31else acc.push(x)32return acc;33}, [])34;3536return chunks.reduce(function (lines, rawChunk) {37if (rawChunk === '') return lines;3839var chunk = rawChunk.replace(/\t/g, ' ');4041var i = lines.length - 1;42if (lines[i].length + chunk.length > stop) {43lines[i] = lines[i].replace(/\s+$/, '');4445chunk.split(/\n/).forEach(function (c) {46lines.push(47new Array(start + 1).join(' ')48+ c.replace(/^\s+/, '')49);50});51}52else if (chunk.match(/\n/)) {53var xs = chunk.split(/\n/);54lines[i] += xs.shift();55xs.forEach(function (c) {56lines.push(57new Array(start + 1).join(' ')58+ c.replace(/^\s+/, '')59);60});61}62else {63lines[i] += chunk;64}6566return lines;67}, [ new Array(start + 1).join(' ') ]).join('\n');68};69};7071wordwrap.soft = wordwrap;7273wordwrap.hard = function (start, stop) {74return wordwrap(start, stop, { mode : 'hard' });75};767778