changesOf method

Map changesOf(
  1. Map newData, [
  2. bool includeNonExistingKeys = false,
  3. bool testFunc(
    1. dynamic
    )?
])

Implementation

Map changesOf(Map newData, [
  bool includeNonExistingKeys = false,
  bool Function(dynamic)? testFunc,
]) {
  if (newData.isEmpty) return {};

  bool Function(dynamic) isDataEmpty = testFunc ?? __isDataEmpty;

  Map changes = {};
  for (String key in newData.keys) {
    if (!includeNonExistingKeys && !data.containsKey(key)) continue;

    dynamic oldValue = data[key];
    dynamic newValue = newData[key];

    if (isDataEmpty(oldValue) && isDataEmpty(newValue)) continue;

    if (data[key] != newData[key]) {
      changes[key] = newData[key];
    }
  }

  return changes;
}