Generic function with same logic but different input structure 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) &gt; 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) &gt; 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) &gt; 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?
You could create a more general function which simple compares two values.
Like:

hasDifference(oldValue: number, newValue: number, offset?: number): boolean {
  const offsetToUse: number = offset === undefined ? this.EPSILON : offset;

  return Math.abs(oldValue - newValue) &gt; offsetToUse;
}