You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _sliceAnsi = _interopRequireDefault(require("slice-ansi"));
  7. var _stringWidth = _interopRequireDefault(require("string-width"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @param {string} input
  11. * @param {number} size
  12. * @returns {Array}
  13. */
  14. const wrapWord = (input, size) => {
  15. let subject;
  16. subject = input;
  17. const chunks = []; // https://regex101.com/r/gY5kZ1/1
  18. const re = new RegExp('(^.{1,' + size + '}(\\s+|$))|(^.{1,' + (size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
  19. do {
  20. let chunk;
  21. chunk = subject.match(re);
  22. if (chunk) {
  23. chunk = chunk[0];
  24. subject = (0, _sliceAnsi.default)(subject, (0, _stringWidth.default)(chunk));
  25. chunk = chunk.trim();
  26. } else {
  27. chunk = (0, _sliceAnsi.default)(subject, 0, size);
  28. subject = (0, _sliceAnsi.default)(subject, size);
  29. }
  30. chunks.push(chunk);
  31. } while ((0, _stringWidth.default)(subject));
  32. return chunks;
  33. };
  34. var _default = wrapWord;
  35. exports.default = _default;
  36. //# sourceMappingURL=wrapWord.js.map