]> git.ipfire.org Git - thirdparty/blocklistproject/lists.git/commitdiff
Update remove-duplicates.js
authorgap579137 <30596626+gap579137@users.noreply.github.com>
Fri, 20 Sep 2024 15:07:07 +0000 (10:07 -0500)
committerGitHub <noreply@github.com>
Fri, 20 Sep 2024 15:07:07 +0000 (10:07 -0500)
scripts/remove-duplicates.js

index 5392acad9ce9601ac2a474fa4a1f730c4583f979..02586f11311c7164b8f9e67377b1b8b2ad2f185e 100644 (file)
@@ -1,36 +1,35 @@
-const fs = require("fs").promises;
-const path = require("path");
+const fs = require("node:fs").promises;
+const path = require("node:path");
 
 (async () => {
        try {
                const directoryPath = path.join(__dirname, "..");
-               const files = (await fs.readdir(directoryPath)).filter(file => file.endsWith(".txt"));
+               const files = (await fs.readdir(directoryPath)).filter((file) =>
+                       file.endsWith(".txt"),
+               );
 
-               await Promise.all(files.map(async file => {
-                       const filePath = path.join(directoryPath, file);
-                       let fileContents = await fs.readFile(filePath, "utf8");
+               await Promise.all(
+                       files.map(async (file) => {
+                               const filePath = path.join(directoryPath, file);
+                               const fileContents = await fs.readFile(filePath, "utf8");
 
-                       const lines = fileContents.split("\n");
-                       const existingDomains = new Set();
-                       const filteredLines = [];
-
-                       lines.forEach(line => {
-                               if (line.startsWith("0.0.0.0 ")) {
-                                       const domain = line.replace("0.0.0.0 ", "");
-                                       if (!existingDomains.has(domain)) {
-                                               existingDomains.add(domain);
-                                               filteredLines.push(line);
+                               const lines = fileContents.split("\n");
+                               const existingDomains = new Set();
+                               const filteredLines = lines.filter((line) => {
+                                       if (line.startsWith("0.0.0.0 ")) {
+                                               const domain = line.replace("0.0.0.0 ", "");
+                                               if (!existingDomains.has(domain)) {
+                                                       existingDomains.add(domain);
+                                                       return true;
+                                               }
+                                               return false;
                                        }
-                               } else {
-                                       filteredLines.push(line);
-                               }
-                       });
-
-                       // Combine the filtered lines back into a single string
-                       const updatedContents = filteredLines.join("\n");
+                                       return true;
+                               });
 
-                       await fs.writeFile(filePath, updatedContents, "utf8");
-               }));
+                               await fs.writeFile(filePath, filteredLines.join("\n"), "utf8");
+                       }),
+               );
        } catch (error) {
                console.error("Error processing files:", error);
        }