Added lastOrEmpty() extension function.
This commit is contained in:
parent
10f1ee8518
commit
97a9fd1597
3 changed files with 15 additions and 16 deletions
|
@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Utils.appendIfMissing
|
|||
import net.thauvin.erik.mobibot.Utils.bot
|
||||
import net.thauvin.erik.mobibot.Utils.getIntProperty
|
||||
import net.thauvin.erik.mobibot.Utils.isChannelOp
|
||||
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
|
||||
import net.thauvin.erik.mobibot.Utils.sendList
|
||||
import net.thauvin.erik.mobibot.Utils.sendMessage
|
||||
import net.thauvin.erik.mobibot.Utils.toIsoLocalDate
|
||||
|
@ -106,7 +107,7 @@ import java.util.regex.Pattern
|
|||
import kotlin.system.exitProcess
|
||||
|
||||
@Version(properties = "version.properties", className = "ReleaseInfo", template = "version.mustache", type = "kt")
|
||||
class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() {
|
||||
class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() {
|
||||
// The bot configuration.
|
||||
private val config: Configuration
|
||||
|
||||
|
@ -116,9 +117,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
// Tell module
|
||||
private val tell: Tell
|
||||
|
||||
/** Main channel. */
|
||||
val channel: String
|
||||
|
||||
/** Logger. */
|
||||
val logger: Logger = LoggerFactory.getLogger(Mobibot::class.java)
|
||||
|
||||
|
@ -185,9 +183,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
if (logger.isTraceEnabled) logger.trace("<<< ${user.nick}: ${event.message}")
|
||||
val cmds = event.message.trim().split(" ".toRegex(), 2)
|
||||
val cmd = cmds[0].lowercase()
|
||||
val args = if (cmds.size > 1) {
|
||||
cmds[1].trim()
|
||||
} else ""
|
||||
val args = cmds.lastOrEmpty().trim()
|
||||
if (cmd.startsWith(Constants.HELP_CMD)) { // help
|
||||
helpResponse(event, args)
|
||||
} else if (!addons.exec(channel, cmd, args, event)) { // Execute command or module
|
||||
|
@ -217,9 +213,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
if (logger.isTraceEnabled) logger.trace(">>> $sender: $message")
|
||||
val cmds = message.substring(message.indexOf(':') + 1).trim().split(" ".toRegex(), 2)
|
||||
val cmd = cmds[0].lowercase()
|
||||
val args = if (cmds.size > 1) {
|
||||
cmds[1].trim()
|
||||
} else ""
|
||||
val args = cmds.lastOrEmpty().trim()
|
||||
if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help
|
||||
helpResponse(event, args)
|
||||
} else {
|
||||
|
@ -354,7 +348,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
* Initialize the bot.
|
||||
*/
|
||||
init {
|
||||
this.channel = channel
|
||||
val ircServer = p.getProperty("server", Constants.DEFAULT_SERVER)
|
||||
config = Configuration.Builder().apply {
|
||||
name = nickname
|
||||
|
|
|
@ -35,6 +35,7 @@ package net.thauvin.erik.mobibot.commands.links
|
|||
import net.thauvin.erik.mobibot.Utils.bot
|
||||
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
|
||||
import net.thauvin.erik.mobibot.Utils.helpFormat
|
||||
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
|
||||
import net.thauvin.erik.mobibot.Utils.sendMessage
|
||||
import net.thauvin.erik.mobibot.commands.AbstractCommand
|
||||
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
|
||||
|
@ -77,11 +78,7 @@ class View : AbstractCommand() {
|
|||
val split = query.split(" ", limit = 2)
|
||||
try {
|
||||
start = split[0].toInt() - 1
|
||||
query = if (split.size == 2) {
|
||||
split[1].trim()
|
||||
} else {
|
||||
""
|
||||
}
|
||||
query = split.lastOrEmpty().trim()
|
||||
if (start > entries.links.size) {
|
||||
start = 0
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ import net.thauvin.erik.mobibot.Utils.encodeUrl
|
|||
import net.thauvin.erik.mobibot.Utils.getIntProperty
|
||||
import net.thauvin.erik.mobibot.Utils.green
|
||||
import net.thauvin.erik.mobibot.Utils.helpFormat
|
||||
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
|
||||
import net.thauvin.erik.mobibot.Utils.obfuscate
|
||||
import net.thauvin.erik.mobibot.Utils.plural
|
||||
import net.thauvin.erik.mobibot.Utils.red
|
||||
|
@ -187,6 +188,14 @@ class UtilsTest {
|
|||
assertThat(localDateTime.toIsoLocalDate(), "isoLocalDate(localDate)").isEqualTo("1952-02-17")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLastOrEmpty() {
|
||||
val two = listOf("1", "2")
|
||||
assertThat(two.lastOrEmpty(), "two").isEqualTo("2")
|
||||
val one = listOf("1")
|
||||
assertThat(one.lastOrEmpty(), "one").isEqualTo("")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testObfuscate() {
|
||||
assertThat(ascii.obfuscate(), "obfuscate()").all {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue