rxjs-distinct-deep
Version:
A custom RxJS operator that extends distinctUntilChanged with deep equality comparison, allowing you to detect changes in deeply nested objects or structures. This comparison is done without any external libraries, such as Lodash, using a custom recursive
27 lines (26 loc) • 758 B
JavaScript
import { distinctUntilChanged } from "rxjs/operators";
function deepEqual(a, b) {
if (a === b) {
return true;
}
if (typeof a !== "object" ||
typeof b !== "object" ||
a === null ||
b === null) {
return false;
}
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length)
return false;
for (const key of keysA) {
// Recursively compare values
if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {
return false;
}
}
return true;
}
export function distinctUntilChangedDeep(comparator = deepEqual) {
return (source) => source.pipe(distinctUntilChanged((prev, curr) => comparator(prev, curr)));
}