Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ <h2>Mock Data Generator</h2>
<button onclick="generateMockData()">Generate</button>
<button onclick="copyMockOutput('json')">Copy JSON</button>
<button onclick="copyMockOutput('csv')">Copy CSV</button>
<button onclick="exportMockOutput('json')">Export as JSON</button>
<button onclick="exportMockOutput('csv')">Export as CSV</button>
<input type="text" id="userInput" placeholder="File Download Name(optional)" />

<label style="margin-left: 20px">
<input
Expand Down
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,50 @@ function copyMockOutput(format = "json") {
copyToClipboard(text, `Copied ${format.toUpperCase()}!`);
}

function downloadFile(content, filename, type) {
const blob = new Blob([content], { type });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}

let userInputValue = ""; // This variable will store filename input from user using the following event listener:

document.getElementById("userInput").addEventListener("input", function (e) {
userInputValue = e.target.value;
//console.log("Saved input:", userInputValue); // Optional: see it live
});
Comment on lines +1189 to +1194
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling for the missing DOM element.

The event listener assumes that the element with id "userInput" exists in the DOM, but there's no defensive check for this. If the element doesn't exist when this code runs, it will throw an error.

-  document.getElementById("userInput").addEventListener("input", function (e) {
+  const userInputElement = document.getElementById("userInput");
+  if (userInputElement) {
+    userInputElement.addEventListener("input", function (e) {
       userInputValue = e.target.value;
       //console.log("Saved input:", userInputValue); // Optional: see it live
-  });
+    });
+  }

Also, the commented console.log should be removed for production code.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let userInputValue = ""; // This variable will store filename input from user using the following event listener:
document.getElementById("userInput").addEventListener("input", function (e) {
userInputValue = e.target.value;
//console.log("Saved input:", userInputValue); // Optional: see it live
});
let userInputValue = ""; // This variable will store filename input from user using the following event listener:
const userInputElement = document.getElementById("userInput");
if (userInputElement) {
userInputElement.addEventListener("input", function (e) {
userInputValue = e.target.value;
//console.log("Saved input:", userInputValue); // Optional: see it live
});
}


function exportMockOutput(format = "json") {
if (!Array.isArray(latestMockData) || latestMockData.length === 0) {
console.error("No data available to export.");
return;
}

let content = "";
let filename = userInputValue !== "" ? userInputValue : "mock_data"; // Use user input if available

if (format === "json") {
content = JSON.stringify(latestMockData.length === 1 ? latestMockData[0] : latestMockData, null, 2);
filename += ".json";
downloadFile(content, filename, "application/json");
} else if (format === "csv") {
if (!latestMockData.length || typeof latestMockData[0] !== "object") return;

const keys = Object.keys(latestMockData[0]);
const rows = latestMockData.map(obj => keys.map(k => JSON.stringify(obj[k] ?? "")));
content = [keys.join(","), ...rows.map(r => r.join(","))].join("\n");
filename += ".csv";
downloadFile(content, filename, "text/csv");
}
}



const mockgenDocs = `
# 🧪 Mock Data Schema Format
Expand Down