I have a data structure that looks like this:
interface Node {
originalWeight: number;
currentWeight: number;
}
where both properties are floats between 0 and 1.
To check if a node has been modified, I wrote a simple isNodeModified function:
isNodeModified(node: Node): boolean {
return Math.abs(node.originalWeight- node.currentWeight) > this.EPSILON;
}
where EPSILON is my tolerance.
However, I also need to do a very similar comparison in a slightly different scenario, which was originally handled like this:
if (Math.abs(event.value - node.originalWeight) > this.EPSILON) {
// do something
}
where event is another object with a value property.
To avoid duplication, I replaced both methods with something like the following:
isNodeModified(node: Node, event?: Event): boolean {
let original = node.originalWeight;
if (event) {
original = event.value;
}
return Math.abs(original - node.currentWeight) > this.EPSILON;
}
So that I can call isNodeModified(node) or isNodeModified(node, event) depending on what I need.
Is there a better / cleaner solution to this problem?
eventis? \$\endgroup\$originalWeight, but only ifevent.value !== currentWeight\$\endgroup\$