Skip to content

Commit 24d2c00

Browse files
authored
refactor: Rename some methods and types related to ByRegex. (#86)
I think "regexMatch" makes more sense than "regexToken" because we aren't doing anything special to the raw regex match. I like "regexDidNotMatch" better than "alwaysLast" since the former actually describes how we get that value instead of the business logic implications.
1 parent 3c1b7cd commit 24d2c00

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

keepsorted/block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ func (b block) lessFn() cmpFunc[lineGroup] {
403403
return 1
404404
})
405405

406-
regexTransform := func(lg lineGroup) []regexToken {
407-
return b.metadata.opts.regexTransform(lg.joinedLines())
406+
regexTransform := func(lg lineGroup) []regexMatch {
407+
return b.metadata.opts.matchRegexes(lg.joinedLines())
408408
}
409409

410410
ord := newPrefixOrder(b.metadata.opts)
@@ -442,6 +442,6 @@ func (b block) lessFn() cmpFunc[lineGroup] {
442442
}, numericTokens.compare)
443443

444444
return commentOnlyBlock.
445-
andThen(comparingFunc(regexTransform, compareRegexTokens(prefixOrder.andThen(lexicographically(transformOrder))))).
445+
andThen(comparingFunc(regexTransform, compareRegexMatches(prefixOrder.andThen(lexicographically(transformOrder))))).
446446
andThen(lineGroup.less)
447447
}

keepsorted/options.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,23 @@ func (opts blockOptions) trimIgnorePrefix(s string) string {
375375
return s
376376
}
377377

378-
// regexTransform applies ByRegex to s.
378+
// matchRegexes applies ByRegex to s.
379379
// If ByRegex is empty, returns a slice that contains just s.
380380
// Otherwise, applies each regex to s in sequence:
381381
// If a regex has capturing groups, the capturing groups will be added to the
382382
// resulting slice.
383383
// If a regex does not have capturing groups, all matched text will be added to
384384
// the resulting slice.
385-
func (opts blockOptions) regexTransform(s string) []regexToken {
385+
func (opts blockOptions) matchRegexes(s string) []regexMatch {
386386
if len(opts.ByRegex) == 0 {
387-
return []regexToken{{s}}
387+
return []regexMatch{{s}}
388388
}
389389

390-
var ret []regexToken
390+
var ret []regexMatch
391391
for _, regex := range opts.ByRegex {
392392
m := regex.FindStringSubmatch(s)
393393
if m == nil {
394-
ret = append(ret, alwaysLast)
394+
ret = append(ret, regexDidNotMatch)
395395
continue
396396
}
397397
if len(m) == 1 {
@@ -405,19 +405,19 @@ func (opts blockOptions) regexTransform(s string) []regexToken {
405405
return ret
406406
}
407407

408-
// regexToken is the result of matching a regex to a string. It has 3 forms:
408+
// regexMatch is the result of matching a regex to a string. It has 3 forms:
409409
// 1. If the regex matched and the regex had capturing groups, it's the value
410410
// of those capturing groups.
411411
// 2. If the regex matched and the regex didn't have capturing groups, it's the
412412
// value of the matched string as a singleton slice.
413-
// 3. IF the regex didn't match, it's alwaysLast / nil.
414-
type regexToken []string
413+
// 3. If the regex didn't match, it's regexDidNotMatch / nil.
414+
type regexMatch []string
415415

416-
var alwaysLast regexToken = nil
416+
var regexDidNotMatch regexMatch = nil
417417

418-
func compareRegexTokens(fn cmpFunc[[]string]) cmpFunc[[]regexToken] {
419-
alwaysLast := comparingFunc(func(t regexToken) bool { return t == nil }, falseFirst())
420-
delegate := comparingFunc(func(t regexToken) []string { return []string(t) }, fn)
418+
func compareRegexMatches(fn cmpFunc[[]string]) cmpFunc[[]regexMatch] {
419+
alwaysLast := comparingFunc(func(t regexMatch) bool { return t == nil }, falseFirst())
420+
delegate := comparingFunc(func(t regexMatch) []string { return t }, fn)
421421
return lexicographically(alwaysLast.andThen(delegate))
422422
}
423423

keepsorted/options_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ func TestBlockOptions_regexTransform(t *testing.T) {
312312
opts.ByRegex = append(opts.ByRegex, regexp.MustCompile(regex))
313313
}
314314

315-
gotTokens := opts.regexTransform(tc.in)
315+
gotTokens := opts.matchRegexes(tc.in)
316316
got := make([][]string, len(gotTokens))
317317
for i, t := range gotTokens {
318318
got[i] = []string(t)
319319
}
320320
if diff := cmp.Diff(tc.want, got); diff != "" {
321-
t.Errorf("%q.regexTransform(%q) diff (-want +got)\n%s", opts, tc.in, diff)
321+
t.Errorf("%q.matchRegexes(%q) diff (-want +got)\n%s", opts, tc.in, diff)
322322
}
323323
})
324324
}

0 commit comments

Comments
 (0)