Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// generateCombinations.js
/* global adventurejs A */

/**
 * Generate string combinations from an array of substrings.
 * @method adventurejs#generateCombinations
 * @memberOf adventurejs
 * @param {Object} world
 */
adventurejs.generateCombinations = function Adventurejs_generateCombinations(
  arrays
) {
  function combine(prefix, arrays, index, result) {
    if (index === arrays.length) {
      result.push(prefix.join(", "));
      return;
    }
    for (let item of arrays[index]) {
      combine([...prefix, item], arrays, index + 1, result);
    }
  }
  const combined = [];
  // combine variations
  combine([], arrays, 0, combined);
  // console.warn(`generateCombinations combined`, combined);

  // Flatten all phrases into individual parts
  const allParts = combined.flatMap((str) =>
    str.split(", ").map((s) => s.trim())
  );

  // Deduplicate
  const uniqueParts = [...new Set(allParts)];

  // Join into one final string (in array form to match your original pattern)
  const results = [uniqueParts.join(", ")];

  // console.warn("generateCombinations", results);
  return results;
};