UNPKG

@trapi/query

Version:

A tiny library which provides utility types/functions for request and response query handling.

40 lines 1.23 kB
"use strict"; /* * Copyright (c) 2021-2021. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeDeep = void 0; function isObject(item) { return (item && typeof item === 'object' && !Array.isArray(item)); } /** * Deep merge two objects. * @param target * @param sources */ function mergeDeep(target, ...sources) { if (!sources.length) return target; const source = sources.shift(); if (isObject(target) && isObject(source)) { const keys = Object.keys(source); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (isObject(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }); mergeDeep(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } } } return mergeDeep(target, ...sources); } exports.mergeDeep = mergeDeep; //# sourceMappingURL=merge-deep.js.map