Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analytics for address element (#5344)
- Loading branch information
1 parent
f986673
commit a2a04d7
Showing
18 changed files
with
479 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AddressUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.stripe.android.paymentsheet.addresselement | ||
|
||
import kotlin.math.min | ||
|
||
// https://gist.github.com/ademar111190/34d3de41308389a0d0d8 | ||
|
||
fun CharSequence.levenshtein(other: CharSequence): Int { | ||
if (this == other) { return 0 } | ||
if (this.isEmpty()) { return other.length } | ||
if (other.isEmpty()) { return this.length } | ||
|
||
val thisLength = this.length + 1 | ||
val otherLength = other.length + 1 | ||
|
||
var cost = Array(thisLength) { it } | ||
var newCost = Array(thisLength) { 0 } | ||
|
||
for (i in 1 until otherLength) { | ||
newCost[0] = i | ||
|
||
for (j in 1 until thisLength) { | ||
val match = if (this[j - 1] == other[i - 1]) 0 else 1 | ||
|
||
val costReplace = cost[j - 1] + match | ||
val costInsert = cost[j] + 1 | ||
val costDelete = newCost[j - 1] + 1 | ||
|
||
newCost[j] = min(min(costInsert, costDelete), costReplace) | ||
} | ||
|
||
val swap = cost | ||
cost = newCost | ||
newCost = swap | ||
} | ||
|
||
return cost[thisLength - 1] | ||
} | ||
|
||
internal fun AddressDetails.editDistance(otherAddress: AddressDetails?): Int { | ||
var editDistance = 0 | ||
editDistance += (city ?: "").levenshtein(otherAddress?.city ?: "") | ||
editDistance += (country ?: "").levenshtein(otherAddress?.country ?: "") | ||
editDistance += (line1 ?: "").levenshtein(otherAddress?.line1 ?: "") | ||
editDistance += (line2 ?: "").levenshtein(otherAddress?.line2 ?: "") | ||
editDistance += (postalCode ?: "").levenshtein(otherAddress?.postalCode ?: "") | ||
editDistance += (state ?: "").levenshtein(otherAddress?.state ?: "") | ||
return editDistance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.