@itwin/presentation-hierarchies
Version:
A package for creating hierarchies based on data in iTwin.js iModels.
50 lines • 2.07 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduceToMergeMapList = reduceToMergeMapList;
exports.reduceToMergeMapItem = reduceToMergeMapItem;
const rxjs_1 = require("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
*/
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
*/
function reduceToMergeMapItem(key, mergeFunc) {
return (source) => (0, rxjs_1.from)(source).pipe((0, rxjs_1.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