The following code traverses an object and its keys. It does a thing for each key depending on what kind of value it refers to. It works well. All is good.
const obj = {
prop1: {
prop2: 1,
prop3: {
prop4: 2
}
},
prop5: "1"
}
function inspectRecursively(node) {
switch (typeof node) {
case "object":
handleObject(node);
break;
case "string":
handleString(node);
break;
case "number":
handleNumber(node);
break;
default:
break;
}
}
function handleObject(node) {
console.log(JSON.stringify(node, null, 2))
for (const [key] of Object.entries(node)) {
inspectRecursively(node[key]);
}
}
function handleString(node) {
console.log(node)
}
function handleNumber(node) {
console.log(node.toString().padStart("0", 3))
}
inspectRecursively(obj)
Say that I think that the file has grown too large. I split it up into modules
main.js
import { importRecursively } from "./importRecursively"
const obj = {
prop1: {
prop2: 1,
prop3: {
prop4: 2
}
},
prop5: "1"
}
inspectRecursively(obj)
importRecursively.js
import { handleObject } from "./handleObject.js"
import { handleString} from "./handleString.js"
import { handleNumber} from "./handleNumber.js"
function inspectRecursively(node) {
switch (typeof node) {
case "object":
handleObject(node);
break;
case "string":
handleString(node);
break;
case "number":
handleNumber(node);
break;
default:
break;
}
}
handleObject.js
import { importRecursively } from "./importRecursively"
function handleObject(node) {
console.log(JSON.stringify(node, null, 2))
for (const [key] of Object.entries(node)) {
inspectRecursively(node[key]);
}
}
handleString.js
function handleString(node) {
console.log(node)
}
handleNumber.js
function handleNumber(node) {
console.log(node.toString().padStart("0", 3))
}
Now I end up with a circular dependency.
main -> inspectRecursively -> handleObject -> importRecursively
I think this is bad, but am not sure about it?
What do I do in this situation? Do I change something to avoid the circular dependency?
importsyntax can handle circular dependencies.