Skip to content

Commit

Permalink
Merge pull request #3 from edoardottt/devel
Browse files Browse the repository at this point in the history
Refine Functions
  • Loading branch information
edoardottt authored Dec 14, 2023
2 parents 7e39cb1 + 29d072e commit cf921a2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ import reading "github.com/edoardottt/go-readingtime"

func main() {
t := reading.RawEstimate(`Lorem ipsum dolor sit amet, consectetur...`)
fmt.Println(t) // 2m0s
fmt.Println(t) // 120

t := reading.Estimate(`Lorem ipsum dolor sit amet, consectetur...`)
fmt.Println(t) // 2m0s

t := reading.HumanEstimate(`Lorem ipsum dolor sit amet, consectetur...`)
fmt.Println(t) // 2 minutes
}
```
Expand Down
35 changes: 21 additions & 14 deletions readingtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,29 @@ const (
exceedMinute = 30
)

// Estimate returns a string represting a humanly readable
// RawEstimate returns a float64 (seconds) represting an
// estimation of how long it would take to read the input text.
func Estimate(input string) string {
func RawEstimate(input string) float64 {
words := strings.Fields(input)
minutes := len(words) / wpm
seconds := len(words) % wpm
f := float64(minutes*secondsInMinute + durationLessThanAMinute(seconds))

return f
}

// Estimate returns a time.Duration object represting an
// estimation of how long it would take to read the input text.
func Estimate(input string) time.Duration {
f := RawEstimate(input)
return duration(f)
}

// HumanEstimate returns a string represting a humanly readable
// estimation of how long it would take to read the input text.
func HumanEstimate(input string) string {
var (
rawMinutes = RawEstimate(input).Minutes()
rawMinutes = Estimate(input).Minutes()
fmtResult = "%s minute"
)

Expand All @@ -40,17 +58,6 @@ func Estimate(input string) string {
return fmt.Sprintf(fmtResult, strconv.Itoa(intResult))
}

// RawEstimate returns a time.Time object represting an
// estimation of how long it would take to read the input text.
func RawEstimate(input string) time.Duration {
words := strings.Fields(input)
minutes := len(words) / wpm
seconds := len(words) % wpm
f := float64(minutes*secondsInMinute + durationLessThanAMinute(seconds))

return duration(f)
}

func duration(f float64) time.Duration {
return time.Duration(f * billion)
}
Expand Down
50 changes: 47 additions & 3 deletions readingtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@ import (
)

func TestRawEstimate(t *testing.T) {
tests := []struct {
name string
input string
want float64
}{
{
name: "238 words",
input: `Speedily say has suitable disposal add boy. On forth doubt miles of child. Exercise joy man children rejoiced. Yet uncommonly his ten who diminution astonished. Demesne new manners savings staying had. Under folly balls death own point now men. Match way these she avoid see death. She whose drift their fat off.
Delightful remarkably mr on announcing themselves entreaties favourable. About to in so terms voice at. Equal an would is found seems of. The particular friendship one sufficient terminated frequently themselves. It more shed went up is roof if loud case. Delay music in lived noise an. Beyond genius really enough passed is up.
Needed feebly dining oh talked wisdom oppose at. Applauded use attempted strangers now are middleton concluded had. It is tried no added purse shall no on truth. Pleased anxious or as in by viewing forbade minutes prevent. Too leave had those get being led weeks blind. Had men rose from down lady able. Its son him ferrars proceed six parlors. Her say projection age announcing decisively men. Few gay sir those green men timed downs widow chief. Prevailed remainder may propriety can and.
Good draw knew bred ham busy his hour. Ask agreed answer rather joy nature admire wisdom. Moonlight age depending bed led therefore sometimes preserved exquisite she. An fail up so shot leaf wise in. Minuter highest his arrived for put and. Hopes lived by rooms oh in no death house.`,
want: 60,
},
{
name: "476 words",
input: `Speedily say has suitable disposal add boy. On forth doubt miles of child. Exercise joy man children rejoiced. Yet uncommonly his ten who diminution astonished. Demesne new manners savings staying had. Under folly balls death own point now men. Match way these she avoid see death. She whose drift their fat off.
Delightful remarkably mr on announcing themselves entreaties favourable. About to in so terms voice at. Equal an would is found seems of. The particular friendship one sufficient terminated frequently themselves. It more shed went up is roof if loud case. Delay music in lived noise an. Beyond genius really enough passed is up.
Needed feebly dining oh talked wisdom oppose at. Applauded use attempted strangers now are middleton concluded had. It is tried no added purse shall no on truth. Pleased anxious or as in by viewing forbade minutes prevent. Too leave had those get being led weeks blind. Had men rose from down lady able. Its son him ferrars proceed six parlors. Her say projection age announcing decisively men. Few gay sir those green men timed downs widow chief. Prevailed remainder may propriety can and.
Good draw knew bred ham busy his hour. Ask agreed answer rather joy nature admire wisdom. Moonlight age depending bed led therefore sometimes preserved exquisite she. An fail up so shot leaf wise in. Minuter highest his arrived for put and. Hopes lived by rooms oh in no death house.
Speedily say has suitable disposal add boy. On forth doubt miles of child. Exercise joy man children rejoiced. Yet uncommonly his ten who diminution astonished. Demesne new manners savings staying had. Under folly balls death own point now men. Match way these she avoid see death. She whose drift their fat off.
Delightful remarkably mr on announcing themselves entreaties favourable. About to in so terms voice at. Equal an would is found seems of. The particular friendship one sufficient terminated frequently themselves. It more shed went up is roof if loud case. Delay music in lived noise an. Beyond genius really enough passed is up.
Needed feebly dining oh talked wisdom oppose at. Applauded use attempted strangers now are middleton concluded had. It is tried no added purse shall no on truth. Pleased anxious or as in by viewing forbade minutes prevent. Too leave had those get being led weeks blind. Had men rose from down lady able. Its son him ferrars proceed six parlors. Her say projection age announcing decisively men. Few gay sir those green men timed downs widow chief. Prevailed remainder may propriety can and.
Good draw knew bred ham busy his hour. Ask agreed answer rather joy nature admire wisdom. Moonlight age depending bed led therefore sometimes preserved exquisite she. An fail up so shot leaf wise in. Minuter highest his arrived for put and. Hopes lived by rooms oh in no death house.`,
want: 120,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := readingtime.RawEstimate(tt.input)
require.Equal(t, tt.want, got)
})
}
}

func TestEstimate(t *testing.T) {
tests := []struct {
name string
input string
Expand Down Expand Up @@ -53,13 +97,13 @@ func TestRawEstimate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := readingtime.RawEstimate(tt.input)
got := readingtime.Estimate(tt.input)
require.Equal(t, tt.want, got)
})
}
}

func TestEstimate(t *testing.T) {
func TestHumanEstimate(t *testing.T) {
tests := []struct {
name string
input string
Expand Down Expand Up @@ -126,7 +170,7 @@ func TestEstimate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := readingtime.Estimate(tt.input)
got := readingtime.HumanEstimate(tt.input)
require.Equal(t, tt.want, got)
})
}
Expand Down

0 comments on commit cf921a2

Please sign in to comment.