@itwin/presentation-common
Version:
Common pieces for iModel.js presentation packages
100 lines • 3.26 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_KEYS_BATCH_SIZE = exports.getInstancesCount = void 0;
exports.omitUndefined = omitUndefined;
exports.deepReplaceNullsToUndefined = deepReplaceNullsToUndefined;
exports.createCancellableTimeoutPromise = createCancellableTimeoutPromise;
const Key_js_1 = require("./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
*/
const getInstancesCount = (keys) => {
let count = keys.instanceKeysCount;
keys.nodeKeys.forEach((key) => {
if (Key_js_1.NodeKey.isInstancesNodeKey(key)) {
count += key.instanceKeys.length;
}
else if (Key_js_1.NodeKey.isGroupingNodeKey(key)) {
count += key.groupedInstancesCount;
}
});
return count;
};
exports.getInstancesCount = getInstancesCount;
/**
* Default (recommended) keyset batch size for cases when it needs to be sent
* over HTTP. Sending keys in batches helps avoid HTTP413 error.
*
* @public
*/
exports.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
*/
function omitUndefined(obj) {
Object.entries(obj).forEach(([key, value]) => {
if (value === undefined) {
delete obj[key];
}
});
return obj;
}
/** @internal */
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 */
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