Files
ThreadNet-Web/modules/widget-lifecycle/src/utils/constructWidgetPermissions.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

29 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-02-25 09:36:58 +00:00
/*
Copyright 2026 Element Creations Ltd.
2026-03-04 18:02:17 +00:00
Copyright 2023 Nordeck IT + Consulting GmbH
2026-02-25 09:36:58 +00:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import type { WidgetLifecycleModuleConfig, WidgetConfiguration } from "../config";
import { matchPattern } from "./matchPattern";
/**
* Returns the WidgetConfiguration for a widget.
* If multiple WidgetConfigurations match, the most specific match wins per field.
*/
export function constructWidgetPermissions(
config: WidgetLifecycleModuleConfig,
widgetUrl: string,
): WidgetConfiguration {
const widgetPermissionsMatched = Object.keys(config).filter((pattern) => matchPattern(widgetUrl, pattern));
return widgetPermissionsMatched.sort(sortLongestMatchLast).reduce((prev, key) => ({ ...prev, ...config[key] }), {});
}
/** Sort strings alphabetically so longer, more-specific patterns are applied last. */
export function sortLongestMatchLast(a: string, b: string): number {
return a.localeCompare(b, "en", { sensitivity: "base" });
}