Untitled

Anonymous
Plain Text
09/08/2025 8:15 PM
1.0 KB
15
Indexable
Never
var product = await properties.thing1;
    var values = await properties.thing2;
    if (!product) throw new Error("Product is required");
    if (!Array.isArray(values) || values.length === 0) {
      return [];
    }

    // Group values by ProductOption slug
    const byOption = new Map<string, object[]>();
    for (const v of this.values) {
      const key = v.productOption.slug;
      const list = byOption.get(key) ?? [];
      list.push(v);
      byOption.set(key, list);
    }

    const groups = Array.from(byOption.values());
    if (groups.length === 0) return [];

    // Cartesian product over groups
    const combinations: [] = [];

    const backtrack = (idx: number, path: ProductOptionValue[]) => {
      if (idx === groups.length) {
        combinations.push({ product: this.product, values: path.slice() });
        return;
      }
      const group = groups[idx] ?? [];
      for (const value of group) {
        path.push(value);
        backtrack(idx + 1, path);
        path.pop();
      }
    };

    backtrack(0, []);
    return combinations;
Editor is loading...
Leave a Comment