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.

35 lines
1017 B

4 years ago
  1. import wrapString from './wrapString';
  2. import wrapWord from './wrapWord';
  3. /**
  4. * Wrap a single cell value into a list of lines
  5. *
  6. * Always wraps on newlines, for the remainder uses either word or string wrapping
  7. * depending on user configuration.
  8. *
  9. * @param {string} cellValue
  10. * @param {number} columnWidth
  11. * @param {boolean} useWrapWord
  12. * @returns {Array}
  13. */
  14. export default (cellValue, columnWidth, useWrapWord) => {
  15. // First split on literal newlines
  16. const cellLines = cellValue.split('\n');
  17. // Then iterate over the list and word-wrap every remaining line if necessary.
  18. for (let lineNr = 0; lineNr < cellLines.length;) {
  19. let lineChunks;
  20. if (useWrapWord) {
  21. lineChunks = wrapWord(cellLines[lineNr], columnWidth);
  22. } else {
  23. lineChunks = wrapString(cellLines[lineNr], columnWidth);
  24. }
  25. // Replace our original array element with whatever the wrapping returned
  26. cellLines.splice(lineNr, 1, ...lineChunks);
  27. lineNr += lineChunks.length;
  28. }
  29. return cellLines;
  30. };