diff --git a/scripts/index.js b/scripts/index.js new file mode 100644 index 0000000..3bc278f --- /dev/null +++ b/scripts/index.js @@ -0,0 +1,95 @@ +import fetch from "node-fetch"; +import fs from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const toc = async () => { + // Fetch from API + const data = await fetch( + "https://api.github.com/repos/yashksaini-coder/August-Leetcode-Daily-2024/contents?ref=main" + ) + .then((response) => response.json()) + .then((data) => data); + + var arr = Object.values(data); + + // Filter out the folders + const folders = arr.filter( + (item) => item.type === "dir" && item.name[0] !== "." + ); + + // Make a table of contents + var toc = []; + folders.forEach((item) => { + var num = parseInt(item.name.split("-")[0]); + toc[num] = item.name; + }); + + // Sort toc by key + var sorted = Object.keys(toc) + .sort() + .reduce((obj, key) => { + obj[key] = toc[key]; + return obj; + }, {}); + + // Generate the table of solutions + let solutionsTable = ` + +| Leetcode Problem | Problem Statement | Solution | +|---:|:-----|:----:| +`; + for (var key in sorted) { + var str = sorted[key].split("-"); + var name = str.slice(1).map(word => { + const lowerCaseWord = word.toLowerCase(); + if (["in", "of", "for", "and", "or", "the", "a", "an", "to", "by", "at", "from", "on", "off", "up", "down", "over", "under", "again", "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each", "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than", "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"].includes(lowerCaseWord)) { + return lowerCaseWord; + } else if (lowerCaseWord.startsWith("i") && lowerCaseWord.length <= 3) { + return lowerCaseWord.toUpperCase(); + } else { + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); + } + }).join(" "); + name = name.charAt(0).toUpperCase() + name.slice(1); + var num = key; + var folderName = str.join("-"); + var fileName = str.slice(1).join("-") + ".java"; + var solutionPath = `./${folderName}/${fileName}`; + solutionsTable += `| [${num}](https://leetcode.com/problems/${str.slice(1).join("-")}/) | ${name} | [Solution](${solutionPath}) |\n`; + } + solutionsTable += ""; + + // Read the existing README content + const readmePath = path.join(__dirname, "..", "README.md"); + let readmeContent = fs.readFileSync(readmePath, "utf8"); + + // Check if the solutions table already exists + if (readmeContent.includes("")) { + // Replace the existing table + readmeContent = readmeContent.replace( + /[\s\S]*/, + solutionsTable + ); + } else { + // Find the "## Solutions" heading and insert the table after it + const solutionsHeading = "## Solutions"; + const headingIndex = readmeContent.indexOf(solutionsHeading); + if (headingIndex !== -1) { + const insertIndex = headingIndex + solutionsHeading.length; + readmeContent = readmeContent.slice(0, insertIndex) + "\n\n" + solutionsTable + readmeContent.slice(insertIndex); + } else { + console.error("Could not find '## Solutions' heading in README.md"); + return; + } + } + + // Write the updated content back to README.md + fs.writeFileSync(readmePath, readmeContent); + console.log("README.md has been updated with the solutions table!"); +}; + +toc(); \ No newline at end of file diff --git a/scripts/package-lock.json b/scripts/package-lock.json new file mode 100644 index 0000000..a76e55d --- /dev/null +++ b/scripts/package-lock.json @@ -0,0 +1,102 @@ +{ + "name": "scripts", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "node-fetch": "^3.2.0" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "license": "MIT", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "license": "MIT", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + } + } +} diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..ff5cb57 --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,7 @@ +{ + "type": "module", + "dependencies": { + "node-fetch": "^3.2.0" + } + } + \ No newline at end of file