From 16b2420bbf6c0d1c28cc4c28779d1a76aea2bfb5 Mon Sep 17 00:00:00 2001 From: Timo Date: Mon, 30 Jun 2025 18:48:30 +0200 Subject: [PATCH] add: getEnumParam to ParamParser --- src/UrlParams.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/UrlParams.ts b/src/UrlParams.ts index e2ceee14..b61831a7 100644 --- a/src/UrlParams.ts +++ b/src/UrlParams.ts @@ -257,6 +257,17 @@ class ParamParser { return this.fragmentParams.get(name) ?? this.queryParams.get(name); } + public getEnumParam( + name: string, + type: { [s: string]: T } | ArrayLike, + ): T | undefined { + const value = this.getParam(name); + if (value && Object.values(type).includes(value as T)) { + return value as T; + } + return undefined; + } + public getAllParams(name: string): string[] { return [ ...this.fragmentParams.getAll(name), @@ -299,11 +310,9 @@ export const getUrlParams = ( * In short: either provide url query parameters of UrlConfiguration or set the intent * (or the global defaults will be used). */ - let intent = parser.getParam("intent") as UserIntent | null; + const intent = + parser.getEnumParam("intent", UserIntent) ?? UserIntent.Unknown; - if (!intent || !Object.values(UserIntent).includes(intent as UserIntent)) { - intent = UserIntent.Unknown; - } // Here we only use constants and `platform` to determine the intent preset. let intentPreset: UrlConfiguration; switch (intent) {