wordWrap.js 725 B

12345678910111213141516171819
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.wordWrap = void 0;
  4. const lineMap = (line) => line.slice(-1) === '\n' ? line.slice(0, line.length - 1).replace(/[ \t]*$/gm, '') : line;
  5. const lineReduce = (acc, line) => {
  6. acc.push(...line.split('\n'));
  7. return acc;
  8. };
  9. const wordWrap = (str, options = {}) => {
  10. if (!str)
  11. return [];
  12. const width = options.width || 50;
  13. const regexString = '.{1,' + width + '}([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)';
  14. const re = new RegExp(regexString, 'g');
  15. const lines = (str.match(re) || []).map(lineMap).reduce(lineReduce, []);
  16. return lines;
  17. };
  18. exports.wordWrap = wordWrap;
  19. //# sourceMappingURL=wordWrap.js.map