Aberdeen - v1.0.7
    Preparing search index...

    Function partition

    Reactively partitions items from a source proxy (array or object) into multiple "bucket" proxies based on keys determined by a classifier function.

    This function iterates through the source proxy using onEach. For each item, it calls the classifier func, which should return:

    • A single key (OUT_K): The item belongs to the bucket with this key.
    • An array of keys (OUT_K[]): The item belongs to all buckets specified in the array.
    • undefined: The item is not placed in any bucket.

    The function returns a main proxied object. The keys of this object are the bucket keys (OUT_K) returned by func. Each value associated with a bucket key is another proxied object (the "bucket"). This inner bucket object maps the original keys/indices from the source to the items themselves that were classified into that bucket.

    The entire structure is reactive. Changes in the source proxy (adding/removing/updating items) or changes in dependencies read by the func will cause the output partitioning to update automatically. Buckets are created dynamically as needed and removed when they become empty.

    The input proxied Array or Record (e.g., created by proxy) containing the items to partition.

    A classifier function (value: IN_V, key: IN_K | number) => undefined | OUT_K | OUT_K[]. It receives the item's value and its original key/index from the source. It returns the bucket key(s) the item belongs to, or undefined to ignore the item.

    A proxied object where keys are the bucket identifiers (OUT_K) and values are proxied Records (Record<IN_K | number, IN_V>) representing the buckets. Each bucket maps original source keys/indices to the items belonging to that bucket.

    The type of the keys used for the output buckets (string, number, or symbol).

    The type of the values in the source proxy.

    The type of the keys in the source proxy (if it's a Record).

    interface Product { id: string; category: string; name: string; }
    const products = proxy<Product[]>([
    { id: 'p1', category: 'Fruit', name: 'Apple' },
    { id: 'p2', category: 'Veg', name: 'Carrot' },
    { id: 'p3', category: 'Fruit', name: 'Banana' },
    ]);

    // Partition products by category. Output keys are categories (string).
    // Inner keys are original array indices (number).
    const productsByCategory = partition(products, (product) => product.category);

    // Reactively show the data structure
    dump(productsByCategory);

    // Make random changes to the categories, to show reactiveness
    setInterval(() => products[0|(Math.random()*3)].category = ['Snack','Fruit','Veg'][0|(Math.random()*3)], 2000);
    interface User { id: number; tags: string[]; name: string; }
    const users = proxy({
    'u1': { name: 'Alice', tags: ['active', 'new'] },
    'u2': { name: 'Bob', tags: ['active'] }
    });

    // Partition users by tag. Output keys are tags (string).
    // Inner keys are original object keys (string: 'u1', 'u2').
    const usersByTag = partition(users, (user) => user.tags);

    console.log(usersByTag);
    • When using an object as array.

      Type Parameters

      • OUT_K extends string | number | symbol
      • IN_V

      Parameters

      Returns Record<OUT_K, Record<number, IN_V>>

    • When using an object as source.

      Type Parameters

      • IN_K extends string | number | symbol
      • OUT_K extends string | number | symbol
      • IN_V

      Parameters

      Returns Record<OUT_K, Record<IN_K, IN_V>>