Skip to content

Commit

Permalink
Refactor asCamelCase to use string builder (mozilla-mobile#6141)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods authored and mcarare committed Oct 29, 2019
1 parent 9f64e55 commit 01339ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,31 @@ private class EventWrapper<T : Enum<T>>(
private val recorder: ((Map<T, String>?) -> Unit),
private val keyMapper: ((String) -> T)? = null
) {
private val String.asCamelCase: String
get() = this.split("_").reduceIndexed { index, acc, s ->
if (index == 0) acc + s
else acc + s.capitalize()

/**
* Converts snake_case string to camelCase.
*/
private fun String.asCamelCase(): String {
val parts = split("_")
val builder = StringBuilder()

for ((index, part) in parts.withIndex()) {
if (index == 0) {
builder.append(part)
} else {
builder.append(part[0].toUpperCase())
builder.append(part.substring(1))
}
}

return builder.toString()
}

fun track(event: Event) {
val extras = if (keyMapper != null) {
event.extras?.mapKeys { keyMapper.invoke(it.key.toString().asCamelCase) }
event.extras?.mapKeys { (key) ->
keyMapper.invoke(key.toString().asCamelCase())
}
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ sealed class Event {
}

override val extras: Map<Events.browserMenuActionKeys, String>?
get() = mapOf(Events.browserMenuActionKeys.item to item.toString().toLowerCase())
get() = mapOf(Events.browserMenuActionKeys.item to item.toString().toLowerCase(Locale.ROOT))
}

sealed class Search
Expand Down

0 comments on commit 01339ff

Please sign in to comment.