UNPKG

@itwin/presentation-hierarchies

Version:

A package for creating hierarchies based on data in iTwin.js iModels.

46 lines 1.88 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { from, reduce } from "rxjs"; /** * Reduces the source observable into a `Map<string, TMapItem[]>`, where all input items are grouped based on their key * and placed into arrays. * * @param key A function to calculate map key for the item. * @param value A function to calculate entry value for the item. * * @internal */ export function reduceToMergeMapList(key, value) { return reduceToMergeMapItem(key, (item, list) => { if (!list) { list = []; } list.push(value(item)); return list; }); } /** * Reduces the source observable into a `Map<string, TMapItem>`, where all input items are grouped based on their key * and merged into a single value using provided merge function. * * @param key A function to calculate map key for the item. * @param value A function to create map value using the _current_ item and an item already _existing_ in the map with the same key. * * @internal */ export function reduceToMergeMapItem(key, mergeFunc) { return (source) => from(source).pipe(reduce((mergedMap, item) => { if (mergedMap === EMPTY_MAP) { // this helps us avoid creating an empty map for cases when source observable // doesn't emit any values mergedMap = new Map(); } const mergeKey = key(item); mergedMap.set(mergeKey, mergeFunc(item, mergedMap.get(mergeKey))); return mergedMap; }, EMPTY_MAP)); } const EMPTY_MAP = new Map(); //# sourceMappingURL=ReduceToMergeMap.js.map