1. /**
  2. * Aggregates user inputs with a dry-run mode.
  3. *
  4. * @param {Array<Object>} inputs An array of input objects. Each object should have a 'value' property.
  5. * @param {Object} options Configuration options.
  6. * @param {boolean} options.dryRun If true, only logs the aggregation steps without actually performing the aggregation. Defaults to false.
  7. * @returns {Promise<Object|null>} A promise that resolves to the aggregated result or null if there are no inputs.
  8. */
  9. async function aggregateInputs(inputs, options = {}) {
  10. const { dryRun = false } = options;
  11. if (!inputs || inputs.length === 0) {
  12. console.log("No inputs provided.");
  13. return null;
  14. }
  15. let aggregatedResult = null;
  16. for (let i = 0; i < inputs.length; i++) {
  17. const input = inputs[i];
  18. if (!input || typeof input.value !== 'number') {
  19. console.warn(`Invalid input at index ${i}. Skipping.`);
  20. continue;
  21. }
  22. if (aggregatedResult === null) {
  23. aggregatedResult = input.value; // Initialize with the first value
  24. } else {
  25. aggregatedResult += input.value; // Sum the values
  26. }
  27. if (dryRun) {
  28. console.log(`Dry Run: Adding ${input.value} to aggregated result. Current result: ${aggregatedResult}`);
  29. }
  30. }
  31. if (dryRun) {
  32. console.log(`Dry Run: Aggregated result: ${aggregatedResult}`);
  33. return null;
  34. }
  35. return aggregatedResult;
  36. }

Add your comment