Added extra property.

This commit is contained in:
Erik C. Thauvin 2020-09-13 15:44:59 -07:00
parent ca60284289
commit 89cd67855b
4 changed files with 23 additions and 5 deletions

View file

@ -34,7 +34,8 @@ ReadingTime(
wpm = 275,
postfix = "min read",
plural = "min read",
excludeImages = false
excludeImages = false,
extra = 0
)
```
@ -46,6 +47,7 @@ Property | Description
`postfix` | The value to be appended to the reading time.
`plural` | The value to be appended if the reading time is more than 1 minute.
`excludeImages` | Images are excluded from the reading time when set.
`extra` | Additional seconds to be added to the total reading time.
### Functions
@ -66,7 +68,8 @@ A JSP tag is also available for easy incorporation into web applications:
wpm="275"
postfix="min read"
plural="min read"
excludeImages="false">some_text</t:readingtime>
excludeImages="false"
extra="0">some_text</t:readingtime>
```
None of the attributes are required.

View file

@ -46,13 +46,15 @@ import java.math.RoundingMode
* @param postfix The value to be appended to the reading time.
* @param plural The value to be appended if the reading time is more than 1 minute.
* @param excludeImages Images are excluded from the reading time when set.
* @param extra Additional seconds to be added to the total reading time.
*/
class ReadingTime @JvmOverloads constructor(
text: String,
wpm: Int = 275,
var postfix: String = "min read",
var plural: String = "min read",
excludeImages: Boolean = false
excludeImages: Boolean = false,
extra: Int = 0
) {
companion object {
private const val INVALID: Double = -1.0
@ -97,6 +99,12 @@ class ReadingTime @JvmOverloads constructor(
field = value
}
var extra: Int = extra
set(value) {
reset(value != extra)
field = value
}
/**
* Calculates and returns the reading time in seconds.
*/
@ -106,7 +114,7 @@ class ReadingTime @JvmOverloads constructor(
readTime += wordCount(text) / (wpm / 60.0)
}
return readTime
return readTime + extra
}
/**

View file

@ -7,6 +7,7 @@
<%@tag body-content="scriptless" import="net.thauvin.erik.readingtime.ReadingTime" trimDirectiveWhitespaces="true"%>
<%@attribute name="debug" type="java.lang.Boolean"%>
<%@attribute name="excludeImages" type="java.lang.Boolean"%>
<%@attribute name="extra" type="java.lang.Integer"%>
<%@attribute name="plural"%>
<%@attribute name="postfix"%>
<%@attribute name="wpm" type="java.lang.Integer"%>
@ -14,6 +15,7 @@
<%
final Boolean debug = (Boolean) getJspContext().getAttribute("debug");
final Boolean excludeImages = (Boolean) getJspContext().getAttribute("excludeImages");
final Integer extra = (Integer) getJspContext().getAttribute("extra");
final Integer wpm = (Integer) getJspContext().getAttribute("wpm");
final String body = (String) getJspContext().getAttribute("body");;
final String plural = (String) getJspContext().getAttribute("plural");
@ -22,6 +24,7 @@
if (body != null) {
final ReadingTime rt = new ReadingTime(body);
if (excludeImages != null) rt.setExcludeImages(excludeImages);
if (extra != null) rt.setExtra(extra);
if (plural != null) rt.setPlural(plural);
if (postfix != null) rt.setPostfix(postfix);
if (wpm != null) rt.setWpm(wpm);
@ -31,7 +34,8 @@
out.write("wpm: " + wpm + " (" + rt.getWpm() + ")\n");
out.write("postfix: " + postfix + " (" + rt.getPostfix() + ")\n");
out.write("plural: " + plural + " (" + rt.getPlural() + ")\n");
out.write("excludeImages: " + excludeImages + " (" + rt.getExcludeImages() + ")\n-->");
out.write("excludeImages: " + excludeImages + " (" + rt.getExcludeImages() + ")\n");
out.write("extra: " + extra + " (" + rt.getExtra() + ")\n-->");
}
}
%>

View file

@ -92,6 +92,9 @@ class ReadingTimeTest {
rt.excludeImages = true
assertEquals(calcReadingTime(rt.text, rt.wpm), rt.calcReadingTimeInSec())
rt.extra = 60
assertEquals(calcReadingTime(rt.text, rt.wpm) + 60L, rt.calcReadingTimeInSec())
rt.extra = 0
rt.excludeImages = false
rt.text = mediumPost