Skip to content

Commit

Permalink
feat: handle potential NaN values from Number conversion #91
Browse files Browse the repository at this point in the history
  • Loading branch information
rmenner committed Jan 16, 2025
1 parent 667b1af commit 2cae200
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion components/counter/src/auro-counter-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ export class AuroCounterGroup extends LitElement {
});
}

/**
* Safely converts a value to a number, returning 0 if invalid.
* @private
* @param {*} value - The value to convert.
* @returns {number} The converted number or 0 if invalid.
*/
safeNumberConversion(value) {
const num = Number(value);
return Number.isNaN(num) ? 0 : num;
}

/**
* Updates the aggregate value based on the values of contained auro-counter components.
* This method queries for all `auro-counter` elements, sums their values, and updates the component's `value` property.
Expand All @@ -165,7 +176,7 @@ export class AuroCounterGroup extends LitElement {
updateValue() {
const counters = this.querySelectorAll("auro-counter");
this.value = Array.from(counters).reduce(
(total, counter) => total + Number(counter.value),
(total, counter) => total + this.safeNumberConversion(counter.value),
0,
);
counters.forEach((counter) => {
Expand Down

0 comments on commit 2cae200

Please sign in to comment.