index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*!
  2. * word-wrap <https://github.com/jonschlinkert/word-wrap>
  3. *
  4. * Copyright (c) 2014-2023, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. function trimTabAndSpaces(str) {
  8. const lines = str.split('\n');
  9. const trimmedLines = lines.map((line) => line.trimEnd());
  10. return trimmedLines.join('\n');
  11. }
  12. module.exports = function(str, options) {
  13. options = options || {};
  14. if (str == null) {
  15. return str;
  16. }
  17. var width = options.width || 50;
  18. var indent = (typeof options.indent === 'string')
  19. ? options.indent
  20. : '';
  21. var newline = options.newline || '\n' + indent;
  22. var escape = typeof options.escape === 'function'
  23. ? options.escape
  24. : identity;
  25. var regexString = '.{1,' + width + '}';
  26. if (options.cut !== true) {
  27. regexString += '([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)';
  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. if (options.trim === true) {
  38. result = trimTabAndSpaces(result);
  39. }
  40. return result;
  41. };
  42. function identity(str) {
  43. return str;
  44. }