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.

29 lines
709 B

4 years ago
  1. import slice from 'slice-ansi';
  2. import stringWidth from 'string-width';
  3. /**
  4. * Creates an array of strings split into groups the length of size.
  5. * This function works with strings that contain ASCII characters.
  6. *
  7. * wrapText is different from would-be "chunk" implementation
  8. * in that whitespace characters that occur on a chunk size limit are trimmed.
  9. *
  10. * @param {string} subject
  11. * @param {number} size
  12. * @returns {Array}
  13. */
  14. export default (subject, size) => {
  15. let subjectSlice;
  16. subjectSlice = subject;
  17. const chunks = [];
  18. do {
  19. chunks.push(slice(subjectSlice, 0, size));
  20. subjectSlice = slice(subjectSlice, size).trim();
  21. } while (stringWidth(subjectSlice));
  22. return chunks;
  23. };