Skip to content

Commit

Permalink
fix house rents being incorrectly parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarzaa90 committed Mar 16, 2024
1 parent 09072e5 commit f373755
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package com.galarzaa.tibiakt.core.utils

import kotlin.math.pow

/**
* Split a string enumerating elements, using a different separator for the last item.
*/
Expand Down Expand Up @@ -62,7 +64,7 @@ public fun String.findInteger(): Int = filter { it.isDigit() }.toInt()
public fun String?.nullIfBlank(): String? = takeIf { !it.isNullOrBlank() }

/**
* Parses strings with numbers using "k" as a thousand suffix.
* Parses strings with numbers using "k" as suffix to represent thousands.
*/
public fun String.parseThousandSuffix(): Int =
remove("k", true).parseInteger() * (count { it == 'k' || it == 'K' } * 1000)
remove("k", true).parseInteger() * (1000.0.pow(count { it == 'k' || it == 'K' }).toInt())
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Allan Galarza
* Copyright © 2024 Allan Galarza
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,27 +16,34 @@

package com.galarzaa.tibiakt.core.utils

import io.kotest.core.spec.style.StringSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class StringExtensionsTests : StringSpec({
class StringExtensionsTests : FunSpec({

"splitList" {
test("splitList") {
"One, Two, Three and Four".splitList() shouldBe listOf("One", "Two", "Three", "Four")
"A, B, C , D".splitList() shouldBe listOf("A", "B", "C", "D")
(null as String?).splitList() shouldBe emptyList()
"".splitList() shouldBe emptyList()
}

"remove" {
test("remove") {
"H_e_l_l_o".remove("_") shouldBe "Hello"
"Hello".remove("L") shouldBe "Hello"
"Hello".remove("L", true) shouldBe "Heo"
}

"clean" {
test("clean") {
" Hello ".clean() shouldBe "Hello"
"Lorem\u00A0Ipsum".clean() shouldBe "Lorem Ipsum"
"HTML String".clean() shouldBe "HTML String"
}

test("parseThousandSuffix") {
"1kk".parseThousandSuffix() shouldBe 1_000_000
"500k".parseThousandSuffix() shouldBe 500_000
"5kk".parseThousandSuffix() shouldBe 5_000_000
"10".parseThousandSuffix() shouldBe 10
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import kotlinx.serialization.json.Json

fun main() {
embeddedServer(CIO, port = 8080, module = Application::tibiaKtModule).start(wait = true)
embeddedServer(CIO, port = 8090, module = Application::tibiaKtModule).start(wait = true)
}

fun Application.tibiaKtModule() {
Expand Down

0 comments on commit f373755

Please sign in to comment.