Skip to content

Commit 29d072e

Browse files
committed
v0.0.2
1 parent 7e39cb1 commit 29d072e

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ import reading "github.com/edoardottt/go-readingtime"
4242

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

4747
t := reading.Estimate(`Lorem ipsum dolor sit amet, consectetur...`)
48+
fmt.Println(t) // 2m0s
49+
50+
t := reading.HumanEstimate(`Lorem ipsum dolor sit amet, consectetur...`)
4851
fmt.Println(t) // 2 minutes
4952
}
5053
```

readingtime.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,29 @@ const (
2323
exceedMinute = 30
2424
)
2525

26-
// Estimate returns a string represting a humanly readable
26+
// RawEstimate returns a float64 (seconds) represting an
2727
// estimation of how long it would take to read the input text.
28-
func Estimate(input string) string {
28+
func RawEstimate(input string) float64 {
29+
words := strings.Fields(input)
30+
minutes := len(words) / wpm
31+
seconds := len(words) % wpm
32+
f := float64(minutes*secondsInMinute + durationLessThanAMinute(seconds))
33+
34+
return f
35+
}
36+
37+
// Estimate returns a time.Duration object represting an
38+
// estimation of how long it would take to read the input text.
39+
func Estimate(input string) time.Duration {
40+
f := RawEstimate(input)
41+
return duration(f)
42+
}
43+
44+
// HumanEstimate returns a string represting a humanly readable
45+
// estimation of how long it would take to read the input text.
46+
func HumanEstimate(input string) string {
2947
var (
30-
rawMinutes = RawEstimate(input).Minutes()
48+
rawMinutes = Estimate(input).Minutes()
3149
fmtResult = "%s minute"
3250
)
3351

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

43-
// RawEstimate returns a time.Time object represting an
44-
// estimation of how long it would take to read the input text.
45-
func RawEstimate(input string) time.Duration {
46-
words := strings.Fields(input)
47-
minutes := len(words) / wpm
48-
seconds := len(words) % wpm
49-
f := float64(minutes*secondsInMinute + durationLessThanAMinute(seconds))
50-
51-
return duration(f)
52-
}
53-
5461
func duration(f float64) time.Duration {
5562
return time.Duration(f * billion)
5663
}

readingtime_test.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,50 @@ import (
1616
)
1717

1818
func TestRawEstimate(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
input string
22+
want float64
23+
}{
24+
{
25+
name: "238 words",
26+
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.
27+
28+
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.
29+
30+
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.
31+
32+
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.`,
33+
want: 60,
34+
},
35+
{
36+
name: "476 words",
37+
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.
38+
39+
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.
40+
41+
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.
42+
43+
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.
44+
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.
45+
46+
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.
47+
48+
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.
49+
50+
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.`,
51+
want: 120,
52+
},
53+
}
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
got := readingtime.RawEstimate(tt.input)
57+
require.Equal(t, tt.want, got)
58+
})
59+
}
60+
}
61+
62+
func TestEstimate(t *testing.T) {
1963
tests := []struct {
2064
name string
2165
input string
@@ -53,13 +97,13 @@ func TestRawEstimate(t *testing.T) {
5397
}
5498
for _, tt := range tests {
5599
t.Run(tt.name, func(t *testing.T) {
56-
got := readingtime.RawEstimate(tt.input)
100+
got := readingtime.Estimate(tt.input)
57101
require.Equal(t, tt.want, got)
58102
})
59103
}
60104
}
61105

62-
func TestEstimate(t *testing.T) {
106+
func TestHumanEstimate(t *testing.T) {
63107
tests := []struct {
64108
name string
65109
input string
@@ -126,7 +170,7 @@ func TestEstimate(t *testing.T) {
126170
}
127171
for _, tt := range tests {
128172
t.Run(tt.name, func(t *testing.T) {
129-
got := readingtime.Estimate(tt.input)
173+
got := readingtime.HumanEstimate(tt.input)
130174
require.Equal(t, tt.want, got)
131175
})
132176
}

0 commit comments

Comments
 (0)