@equinor/fusion-observable
Version:
28 lines • 949 B
JavaScript
import { map } from 'rxjs/operators';
/**
* RxJS operator that extracts a nested property from emitted objects using
* a dot-separated string path.
*
* @template TObject - The source object type.
* @template TPath - A dot-separated path string matching nested keys of `TObject`.
* @param path - The dot-separated property path to extract (e.g., `'user.profile.name'`).
* @returns An `OperatorFunction` that maps each emitted object to the value at the given path.
*
* @example
* ```ts
* import { mapProp } from '@equinor/fusion-observable/operators';
*
* state$.pipe(
* mapProp('user.name'),
* ).subscribe(console.log); // emits the user's name
* ```
*/
export const mapProp = (path) => map((obj) => {
return String(path)
.split('.')
.reduce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(cur, attr) => cur[attr], obj);
});
export default mapProp;
//# sourceMappingURL=map-prop.js.map