From 2b9d463ed51196b073d1afee8833c039ba942cc4 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 2 Oct 2020 00:40:11 -0700 Subject: [PATCH] Fixed calculations. --- .../thauvin/erik/readingtime/ReadingTime.kt | 4 +- .../erik/readingtime/ReadingTimeTest.kt | 58 +++++++++++++++++-- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt b/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt index 5787f0a..fedf139 100644 --- a/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt +++ b/src/main/kotlin/net/thauvin/erik/readingtime/ReadingTime.kt @@ -67,7 +67,7 @@ class ReadingTime @JvmOverloads constructor( @JvmStatic fun wordCount(words: String): Int { val s = Jsoup.parse(words).text().trim() - return if (s.isEmpty()) 0 else s.split("\\s+".toRegex()).size + return if (s.isBlank()) 0 else s.split("\\s+".toRegex()).size } /** @@ -111,7 +111,7 @@ class ReadingTime @JvmOverloads constructor( fun calcReadingTimeInSec(): Double { if (readTime == INVALID) { readTime = if (!excludeImages) calcImgReadingTime().toDouble() else 0.0 - readTime += wordCount(text) / (wpm / 60.0) + readTime += (wordCount(text) / wpm) * 60.0 } return readTime + extra diff --git a/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt b/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt index 864bed3..e105d66 100644 --- a/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/readingtime/ReadingTimeTest.kt @@ -40,6 +40,21 @@ class ReadingTimeTest { private val rt = ReadingTime("This is a test.\nWith an image: ") private val blogPost = File("src/test/resources/post.html").readText() private val mediumPost = File("src/test/resources/medium.html").readText() + private val twoSeventyFive = "one two three four five six seven eight nine ten one two three four five six seven " + + "eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine " + + "ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten one two " + + "three four five six seven eight nine ten one two three four five six seven eight nine ten one two three four " + + "five six seven eight nine ten one two three four five six seven eight nine ten one two three four five six " + + "seven eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight " + + "nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten " + + "one two three four five six seven eight nine ten one two three four five six seven eight nine ten one two " + + "three four five six seven eight nine ten one two three four five six seven eight nine ten one two three four " + + "five six seven eight nine ten one two three four five six seven eight nine ten one two three four five six " + + "seven eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight " + + "nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten " + + "one two three four five six seven eight nine ten one two three four five" + private val tenImages = " " + + " " private fun calcImgTime(imgCount: Int): Double { var time = 0.0 @@ -55,7 +70,7 @@ class ReadingTimeTest { } private fun calcReadingTime(text: String, wpm: Int): Double { - return ReadingTime.wordCount(text) / (wpm / 60.0) + return (ReadingTime.wordCount(text) / wpm) * 60.0 } @Test @@ -66,6 +81,8 @@ class ReadingTimeTest { assertEquals(7, ReadingTime.wordCount(rt.text)) assertEquals(505, ReadingTime.wordCount(blogPost)) assertEquals(391, ReadingTime.wordCount(mediumPost)) + assertEquals(275, ReadingTime.wordCount(twoSeventyFive)) + assertEquals(275, ReadingTime.wordCount("$twoSeventyFive ")) } @Test @@ -101,15 +120,39 @@ class ReadingTimeTest { rt.wpm = 300 assertEquals(calcReadingTime(rt.text, 300) + calcImgTime(3), rt.calcReadingTimeInSec()) rt.wpm = 275 + + rt.text = "This is a test" + assertEquals(0.0, rt.calcReadingTimeInSec()) + + rt.text = twoSeventyFive + assertEquals(60.0, rt.calcReadingTimeInSec()) + + rt.text = "$twoSeventyFive " + assertEquals(72.0, rt.calcReadingTimeInSec()) + + rt.text = "$twoSeventyFive " + assertEquals(83.0, rt.calcReadingTimeInSec()) + + rt.text = "$twoSeventyFive $tenImages" + assertEquals(60.0 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3, rt.calcReadingTimeInSec()) + + rt.text = "$twoSeventyFive $tenImages " + assertEquals(135.0 + 3, rt.calcReadingTimeInSec()) + + rt.text = "$twoSeventyFive $twoSeventyFive" + assertEquals(120.0, rt.calcReadingTimeInSec()) + + rt.text = "" + assertEquals(0.0, rt.calcReadingTimeInSec()) } @Test fun testReadingTime() { rt.text = blogPost - assertEquals("4 min read", rt.calcReadingTime()) + assertEquals("3 min read", rt.calcReadingTime()) rt.plural = "mins read" - assertEquals("4 mins read", rt.calcReadingTime()) + assertEquals("3 mins read", rt.calcReadingTime()) rt.text = mediumPost rt.plural = "" @@ -117,10 +160,15 @@ class ReadingTimeTest { rt.text = "This is a test." rt.postfix = "" - assertEquals("1", rt.calcReadingTime()) + assertEquals("0", rt.calcReadingTime()) rt.text = "" assertEquals("0", rt.calcReadingTime()) - assertEquals(0.0, rt.calcReadingTimeInSec()) + + rt.text = twoSeventyFive + assertEquals("1", rt.calcReadingTime()) + + rt.text = "$twoSeventyFive $twoSeventyFive" + assertEquals("2", rt.calcReadingTime()) } }