/**
* Aggregates user inputs with a dry-run mode.
*
* @param {Array<Object>} inputs An array of input objects. Each object should have a 'value' property.
* @param {Object} options Configuration options.
* @param {boolean} options.dryRun If true, only logs the aggregation steps without actually performing the aggregation. Defaults to false.
* @returns {Promise<Object|null>} A promise that resolves to the aggregated result or null if there are no inputs.
*/
async function aggregateInputs(inputs, options = {}) {
const { dryRun = false } = options;
if (!inputs || inputs.length === 0) {
console.log("No inputs provided.");
return null;
}
let aggregatedResult = null;
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
if (!input || typeof input.value !== 'number') {
console.warn(`Invalid input at index ${i}. Skipping.`);
continue;
}
if (aggregatedResult === null) {
aggregatedResult = input.value; // Initialize with the first value
} else {
aggregatedResult += input.value; // Sum the values
}
if (dryRun) {
console.log(`Dry Run: Adding ${input.value} to aggregated result. Current result: ${aggregatedResult}`);
}
}
if (dryRun) {
console.log(`Dry Run: Aggregated result: ${aggregatedResult}`);
return null;
}
return aggregatedResult;
}
Add your comment