@itwin/presentation-backend
Version:
Backend of iTwin.js Presentation library
101 lines • 4.39 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/ban-ts-comment */
/** @packageDocumentation
* @module Core
*/
import { parse as parseVersion } from "semver";
import { DiagnosticsLogEntry, } from "@itwin/presentation-common";
import { combineDiagnosticsSeverities, compareDiagnosticsSeverities, } from "@itwin/presentation-common/internal";
// @ts-ignore TS complains about `with` in CJS builds, but not ESM
import presentationStrings from "@itwin/presentation-common/locales/en/Presentation.json" with { type: "json" };
/** @internal */
export function getLocalizedStringEN(key) {
let result = presentationStrings;
const [namespace, identifier] = key.split(":", 2);
if (namespace !== "Presentation") {
return key;
}
const keySteps = identifier.split(".");
for (const keyStep of keySteps) {
if (typeof result !== "object" || keyStep in result === false) {
return key;
}
result = result[keyStep];
}
return typeof result === "string" ? result : key;
}
/** @internal */
export function getElementKey(imodel, id) {
const className = imodel.elements.tryGetElementProps(id)?.classFullName;
return className ? { className, id } : undefined;
}
/** @internal */
export function normalizeVersion(version) {
if (version) {
const parsedVersion = parseVersion(version, true);
if (parsedVersion) {
return `${parsedVersion.major}.${parsedVersion.minor}.${parsedVersion.patch}`;
}
}
return "0.0.0";
}
/** @internal */
export function combineDiagnosticsOptions(...options) {
const combinedOptions = {};
options.forEach((d) => {
if (!d) {
return;
}
if (d.perf === true ||
(typeof d.perf === "object" &&
(!combinedOptions.perf || (typeof combinedOptions.perf === "object" && d.perf.minimumDuration < combinedOptions.perf.minimumDuration)))) {
combinedOptions.perf = d.perf;
}
const combinedDev = combineDiagnosticsSeverities(d.dev, combinedOptions.dev);
if (combinedDev) {
combinedOptions.dev = combinedDev;
}
const combinedEditor = combineDiagnosticsSeverities(d.editor, combinedOptions.editor);
if (combinedEditor) {
combinedOptions.editor = combinedEditor;
}
});
return combinedOptions.dev || combinedOptions.editor || combinedOptions.perf ? combinedOptions : undefined;
}
/** @internal */
export function reportDiagnostics(diagnostics, options, context) {
const stripped = diagnostics.logs ? stripDiagnostics(options, diagnostics.logs) : undefined;
stripped && options.handler({ logs: stripped }, context);
}
function stripDiagnostics(options, diagnostics) {
const stripped = [];
diagnostics.forEach((entry) => {
if (DiagnosticsLogEntry.isScope(entry)) {
const scopeLogs = stripDiagnostics(options, entry.logs ?? []);
const strippedScope = { ...entry, logs: scopeLogs };
if (!strippedScope.logs) {
delete strippedScope.logs;
}
if (entry.duration !== undefined && (options.perf === true || (typeof options.perf === "object" && entry.duration >= options.perf.minimumDuration))) {
stripped.push(strippedScope);
}
else if (scopeLogs) {
delete strippedScope.duration;
delete strippedScope.scopeCreateTimestamp;
stripped.push(strippedScope);
}
}
else {
const matchesDevSeverity = entry.severity.dev && compareDiagnosticsSeverities(entry.severity.dev, options.dev) >= 0;
const matchesEditorSeverity = entry.severity.editor && compareDiagnosticsSeverities(entry.severity.editor, options.editor) >= 0;
if (matchesDevSeverity || matchesEditorSeverity) {
stripped.push({ ...entry });
}
}
});
return stripped.length > 0 ? stripped : undefined;
}
//# sourceMappingURL=Utils.js.map