"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeUndefinedProperties = exports.removeEmptyObjects = exports.isEmptyObject = exports.flatten = void 0;
const flat_1 = __importDefault(require("flat"));
/**
 * Flattens the given object.
 * e.g. `{ a: { b: { c: 42 } } }` becomes `{ 'a.b.c': 42 }`
 */
function flatten(obj) {
    const flattened = flat_1.default.flatten(obj);
    removeEmptyObjects(flattened);
    return flattened;
}
exports.flatten = flatten;
/**
 * Checks whether the given object has any properties.
 */
function isEmptyObject(obj) {
    if (typeof (obj) !== 'object') {
        return false;
    }
    for (const _ in obj) {
        return false;
    }
    return true;
}
exports.isEmptyObject = isEmptyObject;
/**
 * Recursively removes all properties with an empty object or array as its value from the given object.
 */
function removeEmptyObjects(obj) {
    Object.keys(obj).forEach(key => {
        if (typeof (obj[key]) === 'object') {
            // recursive remove empty object or array properties in nested objects
            removeEmptyObjects(obj[key]);
        }
        if (isEmptyObject(obj[key])) {
            delete obj[key];
        }
    });
}
exports.removeEmptyObjects = removeEmptyObjects;
/**
 * Recursively removes all properties with `undefined` as its value from the given object.
 */
function removeUndefinedProperties(obj) {
    Object.keys(obj).forEach(key => {
        if (obj[key] === undefined) {
            delete obj[key];
        }
        else if (typeof (obj[key]) === 'object') {
            removeUndefinedProperties(obj[key]); // recursive remove `undefined` properties in nested objects
        }
    });
}
exports.removeUndefinedProperties = removeUndefinedProperties;
//# sourceMappingURL=object.js.map