index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. module.exports = function(str, options) {
  8. options = options || {};
  9. if (str == null) {
  10. return str;
  11. }
  12. var width = options.width || 50;
  13. var indent = (typeof options.indent === 'string')
  14. ? options.indent
  15. : ' ';
  16. var newline = options.newline || '\n' + indent;
  17. var escape = typeof options.escape === 'function'
  18. ? options.escape
  19. : identity;
  20. var regexString = '.{1,' + width + '}';
  21. if (options.cut !== true) {
  22. regexString += '([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)';
  23. }
  24. var re = new RegExp(regexString, 'g');
  25. var lines = str.match(re) || [];
  26. var result = indent + lines.map(function(line) {
  27. if (line.slice(-1) === '\n') {
  28. line = line.slice(0, line.length - 1);
  29. }
  30. return escape(line);
  31. }).join(newline);
  32. if (options.trim === true) {
  33. result = result.replace(/[ \t]*$/gm, '');
  34. }
  35. return result;
  36. };
  37. function identity(str) {
  38. return str;
  39. }