Skip to content

Commit 77ee3d8

Browse files
committed
🎨 improve code readability by simplifying conditional expressions and comments
1 parent c465cc7 commit 77ee3d8

File tree

2 files changed

+21
-38
lines changed

2 files changed

+21
-38
lines changed

qs-kotlin/src/main/kotlin/io/github/techouse/qskotlin/Decoder.kt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ internal object Decoder {
122122
if (obj.containsKey(key) && obj[key] is List<*>) {
123123
(obj[key] as List<*>).size
124124
} else 0,
125-
),
126-
{ v: Any? -> options.getDecoder((v as String?), charset) },
127-
)
125+
)
126+
) { v: Any? ->
127+
options.getDecoder((v as String?), charset)
128+
}
128129
}
129130

130131
if (
@@ -135,10 +136,8 @@ internal object Decoder {
135136
) {
136137
value =
137138
Utils.interpretNumericEntities(
138-
when (value) {
139-
is Iterable<*> -> value.joinToString(",") { it.toString() }
140-
else -> value.toString()
141-
}
139+
if (value is Iterable<*>) value.joinToString(",") { it.toString() }
140+
else value.toString()
142141
)
143142
}
144143

@@ -299,14 +298,11 @@ internal object Decoder {
299298
maxDepth: Int,
300299
strictDepth: Boolean,
301300
): List<String> {
302-
// Apply dot→bracket *before* splitting, but when depth == 0 we do NOT split at all and do
301+
// Apply dot→bracket *before* splitting, but when depth == 0, we do NOT split at all and do
303302
// NOT throw.
304-
val key =
305-
if (allowDots) {
306-
originalKey.replace(DOT_TO_BRACKET) { "[${it.groupValues[1]}]" }
307-
} else {
308-
originalKey
309-
}
303+
val key: String =
304+
if (allowDots) originalKey.replace(DOT_TO_BRACKET) { "[${it.groupValues[1]}]" }
305+
else originalKey
310306

311307
// Depth 0 semantics: use the original key as a single segment; never throw.
312308
if (maxDepth <= 0) {
@@ -330,13 +326,13 @@ internal object Decoder {
330326
}
331327

332328
if (open >= 0) {
333-
// When depth > 0, strictDepth can apply to remainder.
329+
// When depth > 0, strictDepth can apply to the remainder.
334330
if (strictDepth) {
335331
throw IndexOutOfBoundsException(
336332
"Input depth exceeded depth option of $maxDepth and strictDepth is true"
337333
)
338334
}
339-
// Stash remainder as a single segment (qs behavior).
335+
// Stash the remainder as a single segment.
340336
segments.add("[" + key.substring(open) + "]")
341337
}
342338

qs-kotlin/src/main/kotlin/io/github/techouse/qskotlin/Encoder.kt

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal object Encoder {
1919
/**
2020
* Encodes the given data into a query string format.
2121
*
22-
* @param data The data to encode, can be any type.
22+
* @param data The data to encode; can be any type.
2323
* @param undefined If true, will not encode undefined values.
2424
* @param sideChannel A mutable map for tracking cyclic references.
2525
* @param prefix An optional prefix for the encoded string.
@@ -126,10 +126,7 @@ internal object Encoder {
126126
return when {
127127
(encoder != null) -> {
128128
val keyValue: String =
129-
when {
130-
encodeValuesOnly -> prefix
131-
else -> encoder(prefix, null, null)
132-
}
129+
if (encodeValuesOnly) prefix else encoder(prefix, null, null)
133130

134131
"${formatter(keyValue)}=${formatter(encoder(obj, null, null))}"
135132
}
@@ -180,17 +177,11 @@ internal object Encoder {
180177
}
181178
}
182179

183-
val encodedPrefix: String =
184-
when {
185-
encodeDotInKeys -> prefix.replace(".", "%2E")
186-
else -> prefix
187-
}
180+
val encodedPrefix: String = if (encodeDotInKeys) prefix.replace(".", "%2E") else prefix
188181

189182
val adjustedPrefix: String =
190-
when {
191-
(commaRoundTrip && obj is Iterable<*> && obj.count() == 1) -> "$encodedPrefix[]"
192-
else -> encodedPrefix
193-
}
183+
if ((commaRoundTrip && obj is Iterable<*> && obj.count() == 1)) "$encodedPrefix[]"
184+
else encodedPrefix
194185

195186
if (allowEmptyLists && obj is Iterable<*> && !obj.iterator().hasNext()) {
196187
return "$adjustedPrefix[]"
@@ -239,16 +230,12 @@ internal object Encoder {
239230
}
240231

241232
val encodedKey: String =
242-
when {
243-
(allowDots && encodeDotInKeys) -> key.toString().replace(".", "%2E")
244-
else -> key.toString()
245-
}
233+
if ((allowDots && encodeDotInKeys)) key.toString().replace(".", "%2E")
234+
else key.toString()
246235

247236
val keyPrefix: String =
248-
when {
249-
obj is Iterable<*> -> generateArrayPrefix(adjustedPrefix, encodedKey)
250-
else -> "$adjustedPrefix${if (allowDots) ".$encodedKey" else "[$encodedKey]"}"
251-
}
237+
if (obj is Iterable<*>) generateArrayPrefix(adjustedPrefix, encodedKey)
238+
else "$adjustedPrefix${if (allowDots) ".$encodedKey" else "[$encodedKey]"}"
252239

253240
// Record the current container in this frame so children can detect cycles.
254241
if (obj is Map<*, *> || obj is Iterable<*>) {

0 commit comments

Comments
 (0)