UNPKG

@itwin/clash-detection-client

Version:

Clash Detection client for the iTwin platform

65 lines 2.58 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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.take = exports.toArray = exports.flatten = exports.map = void 0; /** * Transforms each entity in the async iterator using the provided function. * @param {AsyncIterableIterator<TSource>} iterator source entity iterator. * @param {(entity: TSource) => TTarget} mapFunc function to transform elements from `TSource` to `TTarget`. * @returns {AsyncIterableIterator<TTarget>} iterator of transformed elements. */ async function* map(iterator, mapFunc) { for await (const entity of iterator) { yield mapFunc(entity); } } exports.map = map; /** * Transforms an iterator of entity pages into an iterator of entities. * @param {AsyncIterableIterator<TEntity[]>} pagedIterator iterator of entity pages. * @returns {AsyncIterableIterator<TEntity>} iterator of entities. */ async function* flatten(pagedIterator) { for await (const entityChunk of pagedIterator) { for (const entity of entityChunk) { yield entity; } } } exports.flatten = flatten; /** * Loads all entities from an iterator into an array. * @param {AsyncIterableIterator<TEntity>} iterator entity iterator. * @returns {Promise<TEntity[]>} entity array. */ async function toArray(iterator) { const result = []; for await (const entity of iterator) { result.push(entity); } return result; } exports.toArray = toArray; /** * Loads top n entities from an iterator into an array. * @param {AsyncIterableIterator<TSource>} iterator source entity iterator. * @param {number} entityCount number of entities to load. * @returns {Promise<TEntity[]>} entity array that contains a number of top elements specified. If iterator contains * less items than specified in `entityCount` length of the array will be less than `entityCount`. If * iterator contains no entities the array will be empty. */ async function take(iterator, entityCount) { const result = []; for await (const entity of iterator) { result.push(entity); if (result.length === entityCount) { break; } } return result; } exports.take = take; //# sourceMappingURL=IteratorUtilFunctions.js.map