process-line.js 833 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* eslint-disable camelcase -- cache index access */
  2. /**
  3. * If line is commented out or empty, return null.
  4. * Otherwise, return trimmed line.
  5. *
  6. * @param {string} line
  7. */
  8. const processLine = (line) => {
  9. if (!line) {
  10. return null;
  11. }
  12. const line_0 = line[0];
  13. if (
  14. line_0 === '#'
  15. || line_0 === ' '
  16. || line_0 === '\r'
  17. || line_0 === '\n'
  18. || line_0 === '!'
  19. ) {
  20. return null;
  21. }
  22. const trimmed = line.trim();
  23. if (trimmed === '') {
  24. return null;
  25. }
  26. return trimmed;
  27. };
  28. module.exports.processLine = processLine;
  29. /**
  30. * @param {import('readline').ReadLine} rl
  31. */
  32. module.exports.processLineFromReadline = async (rl) => {
  33. /** @type {string[]} */
  34. const res = [];
  35. for await (const line of rl) {
  36. const l = processLine(line);
  37. if (l) {
  38. res.push(l);
  39. }
  40. }
  41. return res;
  42. };