Aberdeen - v1.0.4
    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.

      Type Parameters

      • T extends object

        The type of the objects being copied.

      Parameters

      • dst: T

        The destination object/array (proxied or unproxied).

      • src: T

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

      • flags: number = 0

        Bitmask controlling copy behavior:

        • MERGE: Performs a partial update. Properties in dst not present in src are kept. null/undefined in src delete properties in dst. Handles partial array updates via object keys.
        • SHALLOW: Performs a shallow copy; when an array/object of the right type doesn't exist in dst yet, a reference to the array/object in src will be made, instead of creating a copy. If the array/object already exists, it won't be replaced (by a reference), but all items will be individually checked and copied like normal, keeping changes (and therefore UI updates) to a minimum.

      Returns void

      Error if attempting to copy an array into a non-array or vice versa (unless MERGE is set, allowing for sparse array updates).

      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 } })
      const source = { b: { c: 99 }, d: undefined }; // d: undefined will delete
      const dest = proxy({ a: 1, b: { x: 5 }, d: 4 });
      copy(dest, source, MERGE);
      console.log(dest); // proxy({ a: 1, b: { c: 99, x: 5 } })
      const messages = proxy(['msg1', 'msg2', 'msg3']);
      const update = { 1: 'updated msg2' }; // Update using object key as index
      copy(messages, update, MERGE);
      console.log(messages); // proxy(['msg1', 'updated msg2', 'msg3'])
      const source = { nested: [1, 2] };
      const dest = {};
      copy(dest, source, SHALLOW);
      dest.nested.push(3);
      console.log(source.nested); // [1, 2, 3] (source was modified)