index.js
1 /*! 2 * word-wrap <https://github.com/jonschlinkert/word-wrap> 3 * 4 * Copyright (c) 2014-2017, Jon Schlinkert. 5 * Released under the MIT License. 6 */ 7 8 module.exports = function(str, options) { 9 options = options || {}; 10 if (str == null) { 11 return str; 12 } 13 14 var width = options.width || 50; 15 var indent = (typeof options.indent === 'string') 16 ? options.indent 17 : ' '; 18 19 var newline = options.newline || '\n' + indent; 20 var escape = typeof options.escape === 'function' 21 ? options.escape 22 : identity; 23 24 var regexString = '.{1,' + width + '}'; 25 if (options.cut !== true) { 26 regexString += '([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)'; 27 } 28 29 var re = new RegExp(regexString, 'g'); 30 var lines = str.match(re) || []; 31 var result = indent + lines.map(function(line) { 32 if (line.slice(-1) === '\n') { 33 line = line.slice(0, line.length - 1); 34 } 35 return escape(line); 36 }).join(newline); 37 38 if (options.trim === true) { 39 result = result.replace(/[ \t]*$/gm, ''); 40 } 41 return result; 42 }; 43 44 function identity(str) { 45 return str; 46 }