FFFF [pull] main from miniflux:main by pull[bot] · Pull Request #306 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions internal/reader/readingtime/readingtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package readingtime

import (
"math"
"strings"
"unicode"
"unicode/utf8"
Expand All @@ -19,9 +18,9 @@ func EstimateReadingTime(content string, defaultReadingSpeed, cjkReadingSpeed in
truncationPoint := min(len(sanitizedContent), 50)

if isCJK(sanitizedContent[:truncationPoint]) {
return int(math.Ceil(float64(utf8.RuneCountInString(sanitizedContent)) / float64(cjkReadingSpeed)))
return (utf8.RuneCountInString(sanitizedContent) + cjkReadingSpeed - 1) / cjkReadingSpeed
}
return int(math.Ceil(float64(countWords(sanitizedContent)) / float64(defaultReadingSpeed)))
return (countWords(sanitizedContent) + defaultReadingSpeed - 1) / defaultReadingSpeed
}

func countWords(s string) int {
Expand Down
19 changes: 18 additions & 1 deletion internal/reader/readingtime/readingtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package readingtime

import "testing"
import (
"strings"
"testing"
)

var samples = map[string]string{
"shortenglish": `This is a short paragraph in english, less than 250 chars.`,
Expand Down Expand Up @@ -79,6 +82,20 @@ func TestEstimateReadingTime(t *testing.T) {
}
}

func TestEmptyEstimateReadingTime(t *testing.T) {
got := EstimateReadingTime("", 200, 500)
if got != 0 {
t.Errorf(`Wrong reading time, got %d instead of %d`, got, 0)
}
}

func TestRepeatedEstimateReadingTime(t *testing.T) {
got := EstimateReadingTime(strings.Repeat("word ", 200), 200, 500)
if got != 1 {
t.Errorf(`Wrong reading time, got %d instead of %d`, got, 1)
}
}

func BenchmarkEstimateReadingTime(b *testing.B) {
for b.Loop() {
for _, sample := range samples {
Expand Down
Loading
0