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.

39 lines
738 B

4 years ago
  1. import slice from 'slice-ansi';
  2. import stringWidth from 'string-width';
  3. /**
  4. * @param {string} input
  5. * @param {number} size
  6. * @returns {Array}
  7. */
  8. export default (input, size) => {
  9. let subject;
  10. subject = input;
  11. const chunks = [];
  12. // https://regex101.com/r/gY5kZ1/1
  13. const re = new RegExp('(^.{1,' + size + '}(\\s+|$))|(^.{1,' + (size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
  14. do {
  15. let chunk;
  16. chunk = subject.match(re);
  17. if (chunk) {
  18. chunk = chunk[0];
  19. subject = slice(subject, stringWidth(chunk));
  20. chunk = chunk.trim();
  21. } else {
  22. chunk = slice(subject, 0, size);
  23. subject = slice(subject, size);
  24. }
  25. chunks.push(chunk);
  26. } while (stringWidth(subject));
  27. return chunks;
  28. };