From ab7deacadc1ec90f982dbc86e7f448e2b2b1ff26 Mon Sep 17 00:00:00 2001 From: Stephen Young Date: Wed, 22 Jan 2025 12:54:56 -0500 Subject: [PATCH] - F add date scrubber --- date_scrubber.go | 64 +++++++++++++++++++ date_scrubber_test.go | 19 ++++++ scrubber.go | 2 +- .../scrubber_test.TestScrubGuids.approved.txt | 6 +- 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 date_scrubber.go create mode 100644 date_scrubber_test.go diff --git a/date_scrubber.go b/date_scrubber.go new file mode 100644 index 0000000..4aafb12 --- /dev/null +++ b/date_scrubber.go @@ -0,0 +1,64 @@ +package approvals + +import ( + "fmt" + "regexp" +) + +type DateScrubber struct { + pattern string + replacement func(int) string +} + +func NewDateScrubber(pattern string) scrubber { + return CreateRegexScrubberWithLabeler(regexp.MustCompile(pattern), func(n int) string { + return fmt.Sprintf("[Date%d]", n) + }) +} + +type SupportedFormat struct { + Regex string + Examples []string +} + +func GetSupportedFormats() []SupportedFormat { + return []SupportedFormat{ + {"[a-zA-Z]{3} [a-zA-Z]{3} \\d{2} \\d{2}:\\d{2}:\\d{2}", []string{"Tue May 13 16:30:00"}}, + {"[a-zA-Z]{3} [a-zA-Z]{3} \\d{2} \\d{2}:\\d{2}:\\d{2} [a-zA-Z]{3,4} \\d{4}", []string{"Wed Nov 17 22:28:33 EET 2021"}}, + {"(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d{4} \\d{2}:\\d{2}:\\d{2} GMT", []string{"Wed, 21 Oct 2015 07:28:00 GMT"}}, + {"[a-zA-Z]{3} [a-zA-Z]{3} \\d{2} \\d{4} \\d{2}:\\d{2}:\\d{2}.\\d{3}", []string{"Tue May 13 2014 23:30:00.789"}}, + {"[a-zA-Z]{3} [a-zA-Z]{3} \\d{2} \\d{2}:\\d{2}:\\d{2} -\\d{4} \\d{4}", []string{"Tue May 13 16:30:00 -0800 2014"}}, + {"\\d{2} [a-zA-Z]{3} \\d{4} \\d{2}:\\d{2}:\\d{2},\\d{3}", []string{"13 May 2014 23:50:49,999"}}, + {"[A-Za-z]{3} \\d{2} \\d{2}:\\d{2}", []string{"Oct 13 15:29"}}, + {"[a-zA-Z]{3} \\d{2}, \\d{4} \\d{2}:\\d{2}:\\d{2} [a-zA-Z]{2} [a-zA-Z]{3}", []string{"May 13, 2014 11:30:00 PM PST"}}, + {"\\d{2}:\\d{2}:\\d{2}", []string{"23:30:00"}}, + {"\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?", []string{"2014/05/13 16:30:59.786", "2014/05/13 16:30:59"}}, + {"\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}Z", []string{"2020-9-10T08:07Z", "2020-09-9T08:07Z", "2020-09-10T8:07Z", "2020-09-10T08:07Z"}}, + {"\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}Z", []string{"2020-09-10T08:07:89Z"}}, + {"\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}\\:\\d{2}\\.\\d{3}Z", []string{"2020-09-10T01:23:45.678Z"}}, + {"\\d{8}T\\d{6}Z", []string{"20210505T091112Z"}}, + {"\\d{4}-\\d{2}-\\d{2}", []string{"2024-12-17"}}, + {"\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}(\\.\\d{1,9})?Z", []string{"2024-12-18T14:04:46.746130Z", "2024-12-18T14:04:46Z", "2024-12-18T14:04:46.746130834Z"}}, + {"\\d{2}[-/.]\\d{2}[-/.]\\d{4}\\s\\d{2}:\\d{2}(:\\d{2})?( (?:pm|am|PM|AM))?", []string{"13/05/2014 23:50:49", "13.05.2014 23:50:49", "13-05-2014 23:50:49", "13.05.2014 23:50", "05/13/2014 11:50:49 PM"}}, + } +} + +// func GetScrubberFor(formattedExample string) (*DateScrubber, error) { +// for _, pattern := range GetSupportedFormats() { +// regex := regexp.MustCompile(pattern.Regex) +// if regex.MatchString(formattedExample) { +// return NewDateScrubber(pattern.Regex), nil +// } +// } +// return nil, fmt.Errorf("no match found for %s. Current supported formats are: %s", +// formattedExample, strings.Join(GetSupportedFormatsRegex(), ", ")) +// } + +func GetSupportedFormatsRegex() []string { + formats := GetSupportedFormats() + regexList := make([]string, len(formats)) + for i, format := range formats { + regexList[i] = format.Regex + } + return regexList +} diff --git a/date_scrubber_test.go b/date_scrubber_test.go new file mode 100644 index 0000000..814e3e7 --- /dev/null +++ b/date_scrubber_test.go @@ -0,0 +1,19 @@ +package approvals_test + +import ( + "testing" + + approvals "github.com/approvals/go-approval-tests" +) + +func TestSupportedFormatWorksForExamples(t *testing.T) { + for _, supportedFormat := range approvals.GetSupportedFormats() { + dateScrubber := approvals.NewDateScrubber(supportedFormat.Regex) + for _, example := range supportedFormat.Examples { + result := dateScrubber(example) + if result != "[Date1]" { + t.Errorf("did not work for\nregex: %s\nexample: %s\ngot: %s", supportedFormat.Regex, example, result) + } + } + } +} diff --git a/scrubber.go b/scrubber.go index a39615e..541696d 100644 --- a/scrubber.go +++ b/scrubber.go @@ -30,7 +30,7 @@ func CreateRegexScrubberWithLabeler(regex *regexp.Regexp, replacer func(int) str idx = len(seen) seen[s] = idx } - return replacer(idx) + return replacer(idx + 1) } return regex.ReplaceAllStringFunc(s, replacefn) } diff --git a/testdata/scrubber_test.TestScrubGuids.approved.txt b/testdata/scrubber_test.TestScrubGuids.approved.txt index d83bfef..00cdfc7 100644 --- a/testdata/scrubber_test.TestScrubGuids.approved.txt +++ b/testdata/scrubber_test.TestScrubGuids.approved.txt @@ -1,8 +1,8 @@ guids -guid_0 guid_1 guid_2 -guid_0 -guid_0 and text \ No newline at end of file +guid_3 +guid_1 +guid_1 and text \ No newline at end of file