From db9787194594ae0017ef64dff09a25f7de587cc2 Mon Sep 17 00:00:00 2001 From: Ralph Ocdol Date: Sun, 13 Apr 2025 10:46:43 +0800 Subject: [PATCH] added unique to filter arrays with unique items in custom-api --- internal/glance/widget-custom-api.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index a7e836e..dbad2a3 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -547,6 +547,18 @@ var customAPITemplateFuncs = func() template.FuncMap { "concat": func(items ...string) string { return strings.Join(items, "") }, + "unique": func(key string, results []decoratedGJSONResult) []decoratedGJSONResult { + seen := make(map[string]struct{}) + out := make([]decoratedGJSONResult, 0, len(results)) + for _, result := range results { + val := result.String(key) + if _, ok := seen[val]; !ok { + seen[val] = struct{}{} + out = append(out, result) + } + } + return out + }, } for key, value := range globalTemplateFunctions {