word-wrap
Version:
Wrap words to a specified length.
33 lines (27 loc) • 771 B
JavaScript
/*!
* word-wrap <https://github.com/jonschlinkert/word-wrap>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*
* Adapted from http://james.padolsey.com/javascript/wordwrap-for-javascript/
* @attribution
*/
module.exports = function(str, options) {
options = options || {};
if (str == null) {
return str;
}
var width = options.width || 50;
var indent = (typeof options.indent === 'string')
? options.indent
: ' ';
var newline = options.newline || '\n' + indent;
var re = new RegExp('.{1,' + width + '}(\\s+|$)|\\S+?(\\s+|$)', 'g');
var lines = str.match(re) || [];
var res = indent + lines.join(newline);
if (options.trim === true) {
res = res.replace(/[ \t]*$/gm, '');
}
return res;
};