Skip to content

Commit

Permalink
Merge pull request #155 from hoangchungk53qx1/master
Browse files Browse the repository at this point in the history
refactor and optimize QueryString
  • Loading branch information
Tlaster authored Oct 13, 2023
2 parents 2c90785 + 32fb866 commit 3ef948a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ data class QueryString(
) {
val map by lazy {
rawInput
.split("?")
.lastOrNull()
.let {
it ?: ""
}
.split("&")
.asSequence()
.substringAfter("?")
.splitToSequence("&")
.map { it.split("=") }
.filter { !it.firstOrNull().isNullOrEmpty() }
.filter { it.size in 1..2 }
.map { it[0] to it.elementAtOrNull(1) }
.groupBy { it.first }
.map { it.key to it.value.mapNotNull { it.second.takeIf { !it.isNullOrEmpty() } } }
.toList()
.filter { it.size in 1..2 && it[0].isNotEmpty() }
.groupBy({ it[0] }, { it.getOrNull(1) })
.map { it -> it.key to it.value.mapNotNull { it?.takeIf { it.isNotEmpty() } } }
.toMap()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ class QueryStringTest {
assertTrue(it.map.containsValue(listOf("bar")))
assertEquals(it.query("foo"), "bar")
}

QueryString("foo=bar&").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("foo"))
assertTrue(it.map.containsValue(listOf("bar")))
assertEquals(it.query("foo"), "bar")
}

QueryString("foo=bar&&").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("foo"))
assertTrue(it.map.containsValue(listOf("bar")))
assertEquals(it.query("foo"), "bar")
}

QueryString("foo=bar").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("foo"))
assertTrue(it.map.containsValue(listOf("bar")))
assertEquals(it.query("foo"), "bar")
}

QueryString("a=1&b=2").let {
assertTrue(it.map.size == 2)
assertTrue(it.map.containsKey("a"))
Expand All @@ -40,6 +44,7 @@ class QueryStringTest {
assertEquals(it.query("a"), "1")
assertEquals(it.query("b"), "2")
}

QueryString("a=1&b=2&").let {
assertTrue(it.map.size == 2)
assertTrue(it.map.containsKey("a"))
Expand All @@ -49,6 +54,7 @@ class QueryStringTest {
assertEquals(it.query("a"), "1")
assertEquals(it.query("b"), "2")
}

QueryString("a=1&&b=2&").let {
assertTrue(it.map.size == 2)
assertTrue(it.map.containsKey("a"))
Expand All @@ -58,32 +64,34 @@ class QueryStringTest {
assertEquals(it.query("a"), "1")
assertEquals(it.query("b"), "2")
}

QueryString("a=1&a=2").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("a"))
assertTrue(it.map.containsValue(listOf("1", "2")))
assertEquals(it.queryList("a"), listOf("1", "2"))
}
QueryString("a=1;a=2").let {
assertTrue(it.map.isEmpty())
}

assertTrue(QueryString("a=1;a=2").map.isEmpty())

QueryString("a=").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("a"))
assertEquals(it.queryList("a"), emptyList<String>())
}

QueryString("a=&").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("a"))
assertEquals(it.queryList("a"), emptyList<String>())
}

QueryString("a=&&").let {
assertTrue(it.map.size == 1)
assertTrue(it.map.containsKey("a"))
assertEquals(it.queryList("a"), emptyList<String>())
}
QueryString("").let {
assertTrue(it.map.isEmpty())
}

assertTrue(QueryString("").map.isEmpty())
}
}

0 comments on commit 3ef948a

Please sign in to comment.