diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 13:53:39 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 13:53:39 +0300 |
| commit | dddcf4a1ef3ca2a9960e608e55a57d65655b981e (patch) | |
| tree | 734ffff735dbb6f91288057b8cc9b5eff92ab8c5 | |
| parent | ecadd6b9e6b6691ae1d1ea7340b08a8fe7eff883 (diff) | |
refactor(rpn): extract sortVariableInfos helper (task cj)
Deduplicate identical VariableInfo sorting logic across
ListVariables(), formatVariablesUnsafe(), and Save(). All three
callers now use a single unexported sortVariableInfos helper.
| -rw-r--r-- | internal/rpn/variables.go | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/internal/rpn/variables.go b/internal/rpn/variables.go index ac04cf1..0ebb52f 100644 --- a/internal/rpn/variables.go +++ b/internal/rpn/variables.go @@ -146,10 +146,7 @@ func (v *Variables) ListVariables() []VariableInfo { infos = append(infos, VariableInfo{Name: name, Value: value}) } - // Sort by name for consistent output - slices.SortFunc(infos, func(a, b VariableInfo) int { - return cmp.Compare(a.Name, b.Name) - }) + sortVariableInfos(infos) return infos } @@ -171,10 +168,7 @@ func (v *Variables) formatVariablesUnsafe() string { infos = append(infos, VariableInfo{Name: name, Value: value}) } - // Sort by name for consistent output - slices.SortFunc(infos, func(a, b VariableInfo) int { - return cmp.Compare(a.Name, b.Name) - }) + sortVariableInfos(infos) if len(infos) == 0 { return "No variables defined" @@ -232,10 +226,7 @@ func (v *Variables) Save(path string) error { infos = append(infos, VariableInfo{Name: name, Value: value}) } - // Sort by name for consistent output - slices.SortFunc(infos, func(a, b VariableInfo) int { - return cmp.Compare(a.Name, b.Name) - }) + sortVariableInfos(infos) return saveVariables(path, infos) } @@ -263,6 +254,13 @@ func (v *Variables) Load(path string) error { return nil } +// sortVariableInfos sorts a slice of VariableInfo by name. +func sortVariableInfos(infos []VariableInfo) { + slices.SortFunc(infos, func(a, b VariableInfo) int { + return cmp.Compare(a.Name, b.Name) + }) +} + // saveVariables saves variable info to a file in JSON format. // This is a helper function that does not acquire locks. func saveVariables(path string, infos []VariableInfo) error { |
