Aberdeen - v1.1.0
    Preparing search index...

    Function copy

    • Recursively copies properties or array items from src to dst. It's designed to work efficiently with reactive proxies created by proxy.

      • Minimizes Updates: When copying between objects/arrays (proxied or not), if a nested object exists in dst with the same constructor as the corresponding object in src, copy will recursively copy properties into the existing dst object instead of replacing it. This minimizes change notifications for reactive updates.
      • Handles Proxies: Can accept proxied or unproxied objects/arrays for both dst and src.
      • Cross-Type Copying: Supports copying between Maps and objects. When copying from an object to a Map, object properties become Map entries. When copying from a Map to an object, Map entries become object properties (only for Maps with string/number/symbol keys).

      Type Parameters

      • T extends object

        The type of the objects being copied.

      Parameters

      • dst: T

        The destination object/array/Map (proxied or unproxied).

      • src: T

        The source object/array/Map (proxied or unproxied). It won't be modified.

      Returns boolean

      true if any changes were made to dst, or false if not.

      Error if attempting to copy an array into a non-array or vice versa.

      const source = proxy({ a: 1, b: { c: 2 } });
      const dest = proxy({ b: { d: 3 } });
      copy(dest, source);
      console.log(dest); // proxy({ a: 1, b: { c: 2 } })
      copy(dest, 'b', { e: 4 });
      console.log(dest); // proxy({ a: 1, b: { e: 4 } })
    • Like above, but copies src into dst[dstKey]. This is useful if you're unsure if dst[dstKey] already exists (as the right type of object) or if you don't want to subscribe to dst[dstKey].

      Type Parameters

      • T extends object

      Parameters

      • dst: T
      • dstKey: keyof T

        Optional key in dst to copy into.

      • src: T[keyof T]

      Returns boolean