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: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" />
<script src="https://cdn.jsdelivr.net/npm/@faker-js/faker@8.4.0/+esm" type="module"></script>
<script type="module">
import { faker } from "https://cdn.jsdelivr.net/npm/@faker-js/faker@8.4.0/+esm";
window.faker = faker;
</script>
<script src="https://cdn.jsdelivr.net/npm/@faker-js/faker/dist/faker.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel="stylesheet" href="https://uicdn.toast.com/editor/latest/toastui-editor.min.css" />
<script src="https://uicdn.toast.com/editor/latest/toastui-editor-all.min.js"></script>
Expand Down
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,18 @@ function generateMockData() {
outputContainer.innerHTML = `<pre class="code-output">❌ Error: ${e.message}</pre>`;
}
}
/* global faker */
function resoloveFakerPath(path) {
if (!path) throw new Error("Empty faker path");
const parts = path.split(".");
let current = faker;
for (const part of parts) {
if (!current[part]) throw new Error(`Invalid faker path: "${path}"`);
current = current[part];
}
if (typeof current === "function") return current();
throw new Error(`Faker path "${path}" does not resolve to a function`);
}

function mockFromSchema(schema) {
if (Array.isArray(schema)) {
Expand All @@ -1206,13 +1218,7 @@ function mockFromSchema(schema) {
return faker.number.int({ min, max });
}

const fakerFn = schema.split(".");
let val = faker;
for (const part of fakerFn) {
val = val?.[part];
}
if (typeof val === "function") return val();
throw new Error(`Invalid faker path: "${schema}"`);
return resoloveFakerPath(schema);
}
return schema;
}
Expand Down