@nevware21/ts-utils
    Preparing search index...

    Function objMergeIf

    • Copies own enumerable properties from source to target only when the predicate returns truthy for that key/value pair. All other own properties on target are left unchanged.

      Security behavior: this helper iterates source keys via forEachOwnKeySafe, so unsafe keys (__proto__, constructor, prototype) are ignored even if the predicate returns true. It also skips all writes when target is a guarded built-in prototype object per isUnsafeTarget.

      Type Parameters

      • T

        The type of the target object

      Parameters

      • target: T

        The target object to merge into

      • source: Record<PropertyKey, any>

        The source object to merge from. Null or undefined source is safely ignored.

      • predicate: (key: PropertyKey, srcValue: any, tgtValue: any) => boolean

        A function (key, srcValue, tgtValue) => boolean called for each own enumerable safe property key of source (string and symbol). The property is merged only when the predicate returns truthy.

      Returns T

      The target object (mutated in place).

      0.14.0

      const target = { a: 1, b: 2 };
      const source = { b: 99, c: 3 };

      // Only merge keys whose source value is greater than 2
      objMergeIf(target, source, (key, srcVal) => srcVal > 2);
      // target => { a: 1, b: 99, c: 3 }

      // Only merge when the key does not already exist in target
      const t2 = { x: 10 };
      objMergeIf(t2, { x: 99, y: 5 }, (key, _sv, tgtVal) => tgtVal === undefined);
      // t2 => { x: 10, y: 5 }

      // Unsafe keys are filtered even if predicate returns true
      const t3: any = {};
      objMergeIf(t3, { constructor: "ignored", safe: 1 } as any, () => true);
      // t3 => { safe: 1 }