splitIntoLines.js 447 B

123456789101112131415161718192021
  1. const splitIntoLines = str => {
  2. const results = [];
  3. const len = str.length;
  4. let i = 0;
  5. for (; i < len; ) {
  6. const cc = str.charCodeAt(i);
  7. // 10 is "\n".charCodeAt(0)
  8. if (cc === 10) {
  9. results.push("\n");
  10. i++;
  11. } else {
  12. let j = i + 1;
  13. // 10 is "\n".charCodeAt(0)
  14. while (j < len && str.charCodeAt(j) !== 10) j++;
  15. results.push(str.slice(i, j + 1));
  16. i = j + 1;
  17. }
  18. }
  19. return results;
  20. };
  21. module.exports = splitIntoLines;