undot = function (obj, separator) {
const undotObject = function (obj, separator = ".") {
const _undot = function (obj, parents = new Map()) {
let result = [];
if (obj) {
for (const [key, value] of Object.entries(obj)) {
const stackedKey = [...parents.keys(), key].join(separator);
if (typeof value !== "function") {
if (typeof value !== "object") {
result.push([stackedKey, value]);
} else if (Array.isArray(value)) {
continue;
} else {
for (const parentValue of parents.values()) {
if (value === parentValue) {
throw new Error("A circular reference was found");
}
}
result = result.concat(
_undot(value, new Map(parents).set(key, value))
);
}
}
}
}
return result;
};
return Object.fromEntries(_undot(obj));
};
return Array.isArray(obj)
? obj.map((i) => undotObject(i, separator))
: undotObject(obj, separator);
}