import { isArray, isObject } from './assertion';
/** Removes keys from an object if their value is undefined. */
export function filterUndefined(object) {
    return objectFilter(object, val => val !== null && val !== undefined);
}
export function merge(...sources) {
    const target = {};
    sources.forEach(source => {
        if (!isObject(source))
            return;
        Object.keys(source).forEach(key => {
            if (key === '__proto__')
                return;
            if (isObject(target[key]) && isObject(source[key])) {
                // Merge objects
                target[key] = merge(target[key], source[key]);
            }
            else if (isArray(target[key]) && isArray(source[key])) {
                // Concat arrays
                target[key] = target[key].concat(source[key]);
            }
            else if (source[key] !== undefined) {
                // Pick source if it exists
                target[key] = source[key];
            }
        });
    });
    return target;
}
/**
 * Returns the items of an object that meet the condition specified in a callback function.
 */
export function objectFilter(object, fn) {
    const result = {};
    Object.keys(object).forEach(key => {
        const value = object[key];
        const shouldPass = fn(value, key, object);
        if (shouldPass) {
            result[key] = value;
        }
    });
    return result;
}
//# sourceMappingURL=object.js.map