UNPKG

686 BJavaScriptView Raw
1"use strict";
2// https://github.com/rails/rails/blob/v4.2.0/actionview/lib/action_view/helpers/text_helper.rb#L240
3function wordWrap(str, options = {}) {
4 if (typeof str !== 'string')
5 throw new TypeError('str must be a string!');
6 const width = options.width || 80;
7 const regex = new RegExp(`(.{1,${width}})(\\s+|$)`, 'g');
8 const lines = str.split('\n');
9 for (let i = 0, len = lines.length; i < len; i++) {
10 const line = lines[i];
11 if (line.length > width) {
12 lines[i] = line.replace(regex, '$1\n').trim();
13 }
14 }
15 return lines.join('\n');
16}
17module.exports = wordWrap;
18//# sourceMappingURL=word_wrap.js.map
\No newline at end of file