From 5a38c8dd2f7432914a9848b33cfd21deef56ae9e Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Wed, 9 Apr 2025 17:05:18 +0100 Subject: [PATCH] Add replaceMatches --- docs/custom-api.md | 1 + internal/glance/widget-custom-api.go | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/custom-api.md b/docs/custom-api.md index 99e3a5e..1930f40 100644 --- a/docs/custom-api.md +++ b/docs/custom-api.md @@ -389,6 +389,7 @@ The following helper functions provided by Glance are available: - `trimSuffix(suffix string, str string) string`: Trims the suffix from a string. - `trimSpace(str string) string`: Trims whitespace from a string on both ends. - `replaceAll(old string, new string, str string) string`: Replaces all occurrences of a string in a string. +- `replaceMatches(pattern string, replacement string, str string) string`: Replaces all occurrences of a regular expression in a string. - `findMatch(pattern string, str string) string`: Finds the first match of a regular expression in a string. - `findSubmatch(pattern string, str string) string`: Finds the first submatch of a regular expression in a string. - `sortByString(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a string key in either ascending or descending order. diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index e092fc3..5d82c5a 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -468,16 +468,15 @@ var customAPITemplateFuncs = func() template.FuncMap { return strings.TrimSuffix(s, suffix) }, "trimSpace": strings.TrimSpace, - "replaceAll": func(oldOrPattern, new, s string) string { + "replaceAll": func(old, new, s string) string { + return strings.ReplaceAll(s, old, new) + }, + "replaceMatches": func(pattern, replacement, s string) string { if s == "" { return "" } - regex := getCachedRegexp(oldOrPattern) - if regex == nil { - return strings.ReplaceAll(s, oldOrPattern, new) - } - return regex.ReplaceAllString(s, new) + return getCachedRegexp(pattern).ReplaceAllString(s, replacement) }, "findMatch": func(pattern, s string) string { if s == "" {