mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
replaced github.com/xhit/go-str2duration with my own func + tests
This commit is contained in:
6
Makefile
6
Makefile
@@ -55,7 +55,11 @@ install: buildlocal
|
|||||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(tool) releases
|
rm -rf $(tool) releases coverage.out
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v ./...
|
go test -v ./...
|
||||||
|
|
||||||
|
cover-report:
|
||||||
|
go test ./... -cover -coverprofile=coverage.out
|
||||||
|
go tool cover -html=coverage.out
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -8,7 +8,6 @@ require (
|
|||||||
github.com/gookit/color v1.5.2
|
github.com/gookit/color v1.5.2
|
||||||
github.com/olekukonko/tablewriter v0.0.5
|
github.com/olekukonko/tablewriter v0.0.5
|
||||||
github.com/spf13/cobra v1.5.0
|
github.com/spf13/cobra v1.5.0
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -31,8 +31,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
|||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0 h1:uFtk6FWB375bP7ewQl+/1wBcn840GPhnySOdcz/okPE=
|
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
|
|
||||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
||||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ var (
|
|||||||
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
||||||
|
|
||||||
// main program version
|
// main program version
|
||||||
Version = "v1.0.10"
|
Version = "v1.0.11"
|
||||||
|
|
||||||
// generated version string, used by -v contains lib.Version on
|
// generated version string, used by -v contains lib.Version on
|
||||||
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
||||||
|
|||||||
48
lib/sort.go
48
lib/sort.go
@@ -19,7 +19,7 @@ package lib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
str2duration "github.com/xhit/go-str2duration/v2"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
@@ -63,15 +63,9 @@ func compare(a string, b string) bool {
|
|||||||
}
|
}
|
||||||
comp = left < right
|
comp = left < right
|
||||||
case "duration":
|
case "duration":
|
||||||
left, err := str2duration.ParseDuration(a)
|
left := duration2int(a)
|
||||||
if err != nil {
|
right := duration2int(b)
|
||||||
left = 0
|
comp = left < right
|
||||||
}
|
|
||||||
right, err := str2duration.ParseDuration(b)
|
|
||||||
if err != nil {
|
|
||||||
right = 0
|
|
||||||
}
|
|
||||||
comp = left.Seconds() < right.Seconds()
|
|
||||||
case "time":
|
case "time":
|
||||||
left, _ := dateparse.ParseAny(a)
|
left, _ := dateparse.ParseAny(a)
|
||||||
right, _ := dateparse.ParseAny(b)
|
right, _ := dateparse.ParseAny(b)
|
||||||
@@ -86,3 +80,37 @@ func compare(a string, b string) bool {
|
|||||||
|
|
||||||
return comp
|
return comp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
We could use time.ParseDuration(), but this doesn't support days.
|
||||||
|
|
||||||
|
We could also use github.com/xhit/go-str2duration/v2, which does
|
||||||
|
the job, but it's just another dependency, just for this little
|
||||||
|
gem. And we don't need a time.Time value. And int is good enough
|
||||||
|
for duration comparision.
|
||||||
|
|
||||||
|
Convert a durartion into an integer. Valid time units are "s",
|
||||||
|
"m", "h" and "d".
|
||||||
|
*/
|
||||||
|
func duration2int(duration string) int {
|
||||||
|
re := regexp.MustCompile(`(\d+)([dhms])`)
|
||||||
|
seconds := 0
|
||||||
|
|
||||||
|
for _, match := range re.FindAllStringSubmatch(duration, -1) {
|
||||||
|
if len(match) == 3 {
|
||||||
|
v, _ := strconv.Atoi(match[1])
|
||||||
|
switch match[2][0] {
|
||||||
|
case 'd':
|
||||||
|
seconds += v * 86400
|
||||||
|
case 'h':
|
||||||
|
seconds += v * 3600
|
||||||
|
case 'm':
|
||||||
|
seconds += v * 60
|
||||||
|
case 's':
|
||||||
|
seconds += v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return seconds
|
||||||
|
}
|
||||||
|
|||||||
79
lib/sort_test.go
Normal file
79
lib/sort_test.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2022 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDuration2Seconds(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
dur string
|
||||||
|
expect int
|
||||||
|
}{
|
||||||
|
{"1d", 60 * 60 * 24},
|
||||||
|
{"1h", 60 * 60},
|
||||||
|
{"10m", 60 * 10},
|
||||||
|
{"2h4m10s", (60 * 120) + (4 * 60) + 10},
|
||||||
|
{"88u", 0},
|
||||||
|
{"19t77X what?4s", 4},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("duration-%s", tt.dur)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
seconds := duration2int(tt.dur)
|
||||||
|
if seconds != tt.expect {
|
||||||
|
t.Errorf("got %d, want %d", seconds, tt.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompare(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
mode string
|
||||||
|
a string
|
||||||
|
b string
|
||||||
|
want bool
|
||||||
|
desc bool
|
||||||
|
}{
|
||||||
|
// ascending
|
||||||
|
{"numeric", "10", "20", true, false},
|
||||||
|
{"duration", "2d4h5m", "45m", false, false},
|
||||||
|
{"time", "12/24/2022", "1/1/1970", false, false},
|
||||||
|
|
||||||
|
// descending
|
||||||
|
{"numeric", "10", "20", false, true},
|
||||||
|
{"duration", "2d4h5m", "45m", true, true},
|
||||||
|
{"time", "12/24/2022", "1/1/1970", true, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("compare-mode-%s-a-%s-b-%s-desc-%t", tt.mode, tt.a, tt.b, tt.desc)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
SortMode = tt.mode
|
||||||
|
SortDescending = tt.desc
|
||||||
|
got := compare(tt.a, tt.b)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("got %t, want %t", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user