Skip to content

Commit

Permalink
feat: add IsValid top level func
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Sep 23, 2024
1 parent 7357f70 commit 07c6e8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ gron.IsDue(expr) // true|false, nil
gron.IsDue(expr, time.Date(2021, time.April, 1, 1, 1, 0, 0, time.UTC)) // true|false, nil
```

> Validity can be checked without instantiation:
```go
import "github.com/adhocore/gronx"

gronx.IsValid("* * * * *") // true
```

### Batch Due Check

If you have multiple cron expressions to check due on same reference time use `BatchDue()`:
Expand Down
10 changes: 8 additions & 2 deletions gronx.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func New() *Gronx {
// IsDue checks if cron expression is due for given reference time (or now).
// It returns bool or error if any.
func (g *Gronx) IsDue(expr string, ref ...time.Time) (bool, error) {
ref = append(ref, time.Now())
if len(ref) == 0 {
ref = append(ref, time.Now())
}
g.C.SetRef(ref[0])

segs, err := Segments(expr)
Expand Down Expand Up @@ -157,12 +159,16 @@ func (g *Gronx) SegmentsDue(segs []string) (bool, error) {
return true, nil
}

// IsValid checks if cron expression is valid.
// It returns bool.
func (g *Gronx) IsValid(expr string) bool { return IsValid(expr) }

// checker for validity
var checker = &SegmentChecker{ref: time.Now()}

// IsValid checks if cron expression is valid.
// It returns bool.
func (g *Gronx) IsValid(expr string) bool {
func IsValid(expr string) bool {
segs, err := Segments(expr)
if err != nil {
return false
Expand Down
3 changes: 3 additions & 0 deletions gronx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func TestIsValid(t *testing.T) {
if !gron.IsValid("* 00 * * *") {
t.Errorf("expected true, got false")
}
if expr := "* * * * *"; IsValid(expr) != gron.IsValid(expr) {
t.Error("IsValid func and method must return same")
}
})

t.Run("is not valid", func(t *testing.T) {
Expand Down

0 comments on commit 07c6e8c

Please sign in to comment.