Skip to content

feat: add xtime.FormatDuration and xtime.ParseDuration #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -11,9 +11,8 @@ comes in the form of a collection of packages.

## Dependencies

The packages are dependency free meaning. Packages added to this module must not
use any external dependencies unless listed below.

The packages are dependency free, meaning they must not use any external
dependencies unless explicitly listed.
Exceptions:

- `golang.org/x/*` - maintained by go and dependency free
@@ -28,20 +27,19 @@ Do *NOT* add exceptions to this list without peer review.

- Prefix names for packages that mirror a go standard library package with `x`.
- Prefix names for packages that are likely to mirror future go standard library
Packages with `x`.
packages with `x`.
- Use singular names for package (except in the mentioned cases).

## Testing

- Unit testing is mandatory.
- Go for > 95% coverage, preferably 100%.
- Go for > 90% coverage, preferably 100%.

## Documentation

- Document all exported (public) identifiers
- Maintain a `doc.go` in each package with introduction, installation
instructions and usage examples.
- Use `make gen` to generate `README.md` files

### doc.go minimal content

@@ -57,11 +55,11 @@ package mypkg
- `file` - file operations
- `set` - set data structure
- `unit` - unit formatting and conversion package
- `version` - version functions
- `xjson` - JSON functions
- `xslices` - slice data type functions
- `xstrings` - string data type functions
- `xstructs` - struct data type functions
- `xtime` - time functions

## Installation

12 changes: 0 additions & 12 deletions version/compare.go

This file was deleted.

49 changes: 0 additions & 49 deletions version/compare_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions version/doc.go

This file was deleted.

14 changes: 14 additions & 0 deletions xstrings/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package xstrings

import "github.com/neticdk/go-stdlib/xslices"

// Coalesce returns the first non-empty string from the given slice.
func Coalesce(strs ...string) string {
s, found := xslices.FindFunc(strs, func(s string) bool {
return s != ""
})
if found {
return s
}
return ""
}
68 changes: 68 additions & 0 deletions xstrings/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package xstrings_test

import (
"testing"

"github.com/neticdk/go-stdlib/assert"
"github.com/neticdk/go-stdlib/xstrings"
)

func TestCoalesce(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{
name: "Empty slice",
args: []string{},
want: "",
},
{
name: "All empty strings",
args: []string{"", "", ""},
want: "",
},
{
name: "First string non-empty",
args: []string{"first", "second", "third"},
want: "first",
},
{
name: "Second string non-empty",
args: []string{"", "second", "third"},
want: "second",
},
{
name: "Last string non-empty",
args: []string{"", "", "third"},
want: "third",
},
{
name: "Mixed empty and non-empty",
args: []string{"", "second", "", "fourth"},
want: "second",
},
{
name: "Single non-empty string",
args: []string{"single"},
want: "single",
},
{
name: "Single empty string",
args: []string{""},
want: "",
},
{
name: "Nil slice (variadic converts nil to empty)",
args: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := xstrings.Coalesce(tt.args...)
assert.Equal(t, got, tt.want, "Coalesce()/%s", tt.name)
})
}
}
Loading