From 9f94e84454af822c1b59f12bf5a2566a452cdf1f Mon Sep 17 00:00:00 2001 From: Eino GOURDIN Date: Wed, 7 Feb 2024 12:37:26 +0100 Subject: [PATCH] feat: allow duplicate values for URL params Allow duplicate same-named params, so as to support multiple hide_filter. --- src/embed_builder.ts | 12 +++++++++--- tests/embed_builder.spec.ts | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/embed_builder.ts b/src/embed_builder.ts index 21c1a0d..04de747 100644 --- a/src/embed_builder.ts +++ b/src/embed_builder.ts @@ -44,13 +44,19 @@ interface LookerEmbedHostSettings { } export interface UrlParams { - [key: string]: string + [key: string]: string | string[] } -function stringify(params: { [key: string]: string }) { +function stringify(params: UrlParams) { const result = [] for (const key in params) { - result.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) + const value = params[key] + const valueArray = Array.isArray(value) ? value : [value] + for (const singleValue of valueArray) { + result.push( + `${encodeURIComponent(key)}=${encodeURIComponent(singleValue)}` + ) + } } return result.join('&') } diff --git a/tests/embed_builder.spec.ts b/tests/embed_builder.spec.ts index abc1726..98773bc 100644 --- a/tests/embed_builder.spec.ts +++ b/tests/embed_builder.spec.ts @@ -250,6 +250,11 @@ describe('LookerEmbedBuilder', () => { expect(builder.embedUrl).toMatch('alpha=1&beta=2') }) + it('should allow multiple values for an url parameter', () => { + builder.withParams({ hide_filter: ['1', '2'] }) + expect(builder.embedUrl).toMatch('hide_filter=1&hide_filter=2') + }) + it('should allow specifying a theme', () => { builder.withTheme('Fancy') expect(builder.embedUrl).toMatch('theme=Fancy')