UNPKG

@itwin/presentation-backend

Version:

Backend of iTwin.js Presentation library

111 lines 4.86 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. *--------------------------------------------------------------------------------------------*/ /* eslint-disable @typescript-eslint/ban-ts-comment */ /** @packageDocumentation * @module Core */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLocalizedStringEN = getLocalizedStringEN; exports.getElementKey = getElementKey; exports.normalizeVersion = normalizeVersion; exports.combineDiagnosticsOptions = combineDiagnosticsOptions; exports.reportDiagnostics = reportDiagnostics; const semver_1 = require("semver"); const presentation_common_1 = require("@itwin/presentation-common"); const internal_1 = require("@itwin/presentation-common/internal"); // @ts-ignore TS complains about `with` in CJS builds, but not ESM const Presentation_json_1 = __importDefault(require("@itwin/presentation-common/locales/en/Presentation.json")); /** @internal */ function getLocalizedStringEN(key) { let result = Presentation_json_1.default; 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 */ function getElementKey(imodel, id) { const className = imodel.elements.tryGetElementProps(id)?.classFullName; return className ? { className, id } : undefined; } /** @internal */ function normalizeVersion(version) { if (version) { const parsedVersion = (0, semver_1.parse)(version, true); if (parsedVersion) { return `${parsedVersion.major}.${parsedVersion.minor}.${parsedVersion.patch}`; } } return "0.0.0"; } /** @internal */ 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 = (0, internal_1.combineDiagnosticsSeverities)(d.dev, combinedOptions.dev); if (combinedDev) { combinedOptions.dev = combinedDev; } const combinedEditor = (0, internal_1.combineDiagnosticsSeverities)(d.editor, combinedOptions.editor); if (combinedEditor) { combinedOptions.editor = combinedEditor; } }); return combinedOptions.dev || combinedOptions.editor || combinedOptions.perf ? combinedOptions : undefined; } /** @internal */ 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 (presentation_common_1.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 && (0, internal_1.compareDiagnosticsSeverities)(entry.severity.dev, options.dev) >= 0; const matchesEditorSeverity = entry.severity.editor && (0, internal_1.compareDiagnosticsSeverities)(entry.severity.editor, options.editor) >= 0; if (matchesDevSeverity || matchesEditorSeverity) { stripped.push({ ...entry }); } } }); return stripped.length > 0 ? stripped : undefined; } //# sourceMappingURL=Utils.js.map