// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. export function cloneDeep(obj: TObject): TObject { return cloneDeepInner(obj, new Set()); } export function merge(base: TBase, other: TOther): (TBase & TOther) | TOther { if (typeof other === 'object' && other !== null && !Array.isArray(other)) { for (const [key, value] of Object.entries(other)) { if (key in base) { const baseValue: unknown = (base as Record)[key]; if (typeof baseValue === 'object' && baseValue !== null && !Array.isArray(baseValue)) { (base as Record)[key] = merge(baseValue, value); } else { (base as Record)[key] = value; } } else { (base as Record)[key] = value; } } return base as TBase & TOther; } else { return other; } } function cloneDeepInner(obj: TObject, seenObjects: Set): TObject { if (seenObjects.has(obj)) { throw new Error('Circular reference detected'); } else if (typeof obj === 'object') { if (obj === null) { return null as TObject; } else { seenObjects.add(obj); if (Array.isArray(obj)) { const result: unknown[] = []; for (const item of obj) { result.push(cloneDeepInner(item, new Set(seenObjects))); } return result as TObject; } else { const result: Record = {}; for (const key of Object.getOwnPropertyNames(obj)) { const value: unknown = (obj as Record)[key]; result[key] = cloneDeepInner(value, new Set(seenObjects)); } return result as TObject; } } } else { return obj; } } /** * Performs a partial deep comparison between `obj` and `source` to * determine if `obj` contains equivalent property values. */ export function isMatch(obj: TObject, source: TObject): boolean { return obj === source || (typeof obj === typeof source && isMatchInner(obj, source)); } function isMatchInner(obj: TObject, source: TObject): boolean { if (obj === null || obj === undefined) { return false; } for (const k of Object.keys(source as object)) { const key: keyof TObject = k as keyof TObject; const sourceValue: unknown = source[key]; if (isStrictComparable(sourceValue)) { if (obj[key] !== sourceValue) { return false; } } else if (!isMatchInner(obj[key], sourceValue)) { return false; } } return true; } /** * Check if `value` is suitable for strict equality comparisons, i.e. `===`. */ function isStrictComparable(value: T): boolean { const type: string = typeof value; return ( // eslint-disable-next-line no-self-compare value === value && !(value !== null && value !== undefined && (type === 'object' || type === 'function')) ); } /** * Removes `undefined` and `null` direct properties from an object. * * @remarks * Note that this does not recurse through sub-objects. */ export function removeNullishProps(obj: T): Partial { const result: Partial = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { if (obj[key] !== undefined && obj[key] !== null) { result[key] = obj[key]; } } } return result; }