@nesvet/n
Version:
Various utilities
24 lines • 889 B
JavaScript
import { isEmpty } from "./isEmpty.js";
import { isPlain } from "./isPlain.js";
export function flat(object, strict = true, prefix) {
if (typeof strict == "string") {
prefix = strict;
strict = true;
}
const entries = Object.entries(object);
for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];
if (value && typeof value == "object" && isPlain(value, strict) && !isEmpty(value)) {
entries.splice(i, 1);
let j = i--;
for (const subkey in value) // eslint-disable-line guard-for-in
entries.splice(j++, 0, [`${key}.${subkey}`, value[subkey]]);
}
}
if (prefix) {
prefix += ".";
return Object.fromEntries(entries.map(([key, value]) => [prefix + key, value]));
}
return Object.fromEntries(entries);
}
//# sourceMappingURL=flat.js.map