@klodianimeri/pipejs
Version:
Pipe functions that provide convenient and efficient ways to work with iterators.
15 lines (13 loc) • 629 B
text/typescript
import { Pipe } from "../pipe.js";
export function distinctUntilKeyChanged(key: string, comparator?: (previous: any, current: any) => boolean): Pipe {
comparator = typeof comparator === 'function' ? comparator : (a, b) => a === b;
return () => {
let lastValue: any;
return (result: IteratorResult<any>): IteratorResult<any> => {
if (result?.done || !result.value?.hasOwnProperty(key) || !lastValue?.hasOwnProperty(key) || !comparator(lastValue[key], result.value[key])) {
lastValue = result.value;
return result;
}
};
}
}