@itwin/presentation-common
Version:
Common pieces for iModel.js presentation packages
95 lines • 3.01 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
import { NodeKey } from "./hierarchy/Key.js";
/**
* Get total number of instances included in the supplied key set. The
* count is calculated by adding all of the following:
* - `keys.instanceKeysCount`
* - number of `keys.nodeKeys` which are *ECInstance* keys
* - for every grouping node key in `keys.nodeKeys`, number of grouped instances
*
* E.g. if `keys` contains one instance key, one *ECInstance* node key
* and one grouping node key which groups 3 instances, the result is 5.
*
* @public
*/
export const getInstancesCount = (keys) => {
let count = keys.instanceKeysCount;
/* eslint-disable @typescript-eslint/no-deprecated */
keys.nodeKeys.forEach((key) => {
if (NodeKey.isInstancesNodeKey(key)) {
count += key.instanceKeys.length;
}
else if (NodeKey.isGroupingNodeKey(key)) {
count += key.groupedInstancesCount;
}
});
/* eslint-enable @typescript-eslint/no-deprecated */
return count;
};
/**
* Default (recommended) keyset batch size for cases when it needs to be sent
* over HTTP. Sending keys in batches helps avoid HTTP413 error.
*
* @public
*/
export const DEFAULT_KEYS_BATCH_SIZE = 5000;
/**
* Removes all `undefined` properties from given `obj` object and returns
* the same (mutated) object.
*
* Example: `omitUndefined({ a: 1, b: undefined })` will return `{ a: 1 }`
*
* @internal
*/
export function omitUndefined(obj) {
Object.entries(obj).forEach(([key, value]) => {
if (value === undefined) {
delete obj[key];
}
});
return obj;
}
/** @internal */
export function deepReplaceNullsToUndefined(obj) {
/* c8 ignore next 3 */
if (obj === null) {
return undefined;
}
/* c8 ignore next 3 */
if (Array.isArray(obj)) {
return obj.map(deepReplaceNullsToUndefined);
}
if (typeof obj === "object") {
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
if (value !== null && value !== undefined) {
acc[key] = deepReplaceNullsToUndefined(value);
}
return acc;
}, {});
}
return obj;
}
/** @internal */
export function createCancellableTimeoutPromise(timeoutMs) {
let timeout;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
rejectPromise = reject;
timeout = setTimeout(resolve, timeoutMs);
});
return {
promise,
cancel: () => {
clearTimeout(timeout);
rejectPromise();
},
};
}
//# sourceMappingURL=Utils.js.map