tools-在每行末尾加上//符号

be aware of the fucking "\r"

// Node.js program to demonstrate
// the fsPromises.readFile() method

// Include fs module
const fs = require("fs");
const fsPromises = require("fs").promises;

async function readFile() {
  const file = await fsPromises.readFile("./file.txt", "utf-8");
  return file;
}

readFile().then((file) => {
  const manyRows = file.split("\n");
  let longestRow = manyRows.length;
  const removePrefix = manyRows
    .map((row) => {
      if (row.length < 10) {
        if (row.includes("{") || row.includes("}")) {
          return row.replace(/\r/, "             ") + "//" + "\r";
        }
        return row.replace(/\r/, "              ") + "//" + "\r";
      }
      return row.replace(/\r/, "") + "//" + "\r";
    })
    .join("\n");

  fsPromises.writeFile("./resultWithCommentSign.txt", removePrefix);
});

posted @ 2024-11-26 09:00  刘老六  阅读(8)  评论(0)    收藏  举报