Skip to content

Commit

Permalink
feat(receive): sort assett list
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchann committed Dec 22, 2021
1 parent a58d5d8 commit 977bab3
Showing 1 changed file with 29 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map } from 'ramda'
import { isEmpty, map, prop, sortBy } from 'ramda'

import { CoinfigType } from '@core/types'
import { createDeepEqualSelector } from '@core/utils'
Expand All @@ -11,55 +11,33 @@ import { SwapAccountType, SwapBaseCounterTypes } from 'data/components/swap/type

import { REQUEST_FORM } from '../model'

// https://stackoverflow.com/a/11958496
const levDist = function (s, t) {
const d: Array<Array<number>> = [] // 2d matrix
const fuzzyAlgorithm = (a: string, b: string): number => {
if (a === b) return 0
let smallStr = a.toLowerCase()
let bigStr = b.toLowerCase()

// Step 1
const n = s.length
const m = t.length

if (n === 0) return m
if (m === 0) return n

// Create an array of arrays in javascript (a descending loop is quicker)
for (let i = n; i >= 0; i -= 1) d[i] = []

// Step 2
for (let i = n; i >= 0; i -= 1) d[i][0] = i
for (let j = m; j >= 0; j -= 1) d[0][j] = j

// Step 3
for (let i = 1; i <= n; i += 1) {
const s_i = s.charAt(i - 1)

// Step 4
for (let j = 1; j <= m; j += 1) {
// Check the jagged ld total so far
if (i === j && d[i][j] > 4) return n

const t_j = t.charAt(j - 1)
const cost = s_i === t_j ? 0 : 1 // Step 5

// Calculate the minimum
let mi = d[i - 1][j] + 1
const b = d[i][j - 1] + 1
const c = d[i - 1][j - 1] + cost

if (b < mi) mi = b
if (c < mi) mi = c

d[i][j] = mi // Step 6

// Damerau transposition
if (i > 1 && j > 1 && s_i === t.charAt(j - 2) && s.charAt(i - 2) === t_j) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost)
}
if (a.length !== b.length) {
if (a.length > b.length) {
const temp = smallStr
smallStr = bigStr
bigStr = temp
}
}

// Step 7
return d[n][m]
if (!isEmpty(smallStr) && bigStr.startsWith(a.substr(0, Math.floor(smallStr.length / 1.5) + 1))) {
return 0
}
if (isEmpty(smallStr)) return isEmpty(bigStr) ? 0 : 1

let j = 0
for (let i = 0; i < bigStr.length; i += 1) {
const char = bigStr[i]
const char2 = smallStr[j]
if (char === char2) j += 1
if (j >= smallStr.length) return 0.1
}

return bigStr.length > 1 ? 1 : 0
}

export const getData = createDeepEqualSelector(
Expand Down Expand Up @@ -103,18 +81,11 @@ export const getData = createDeepEqualSelector(
accounts
)

// this sorts the accounts based on the position of their symbol in the coinsort array
const sortedAccounts = prunedAccounts.sort((acc1, acc2) => {
const balanceDiff = Number(acc2.balance) - Number(acc1.balance)
if (balanceDiff !== 0) {
return balanceDiff
}

return (
levDist(window.coins[acc1.coin].coinfig.name, lowerSearch) -
levDist(window.coins[acc2.coin].coinfig.name, lowerSearch)
)
})
const sortedAccounts = prunedAccounts.sort(
(acc1, acc2) =>
fuzzyAlgorithm(window.coins[acc1.coin].coinfig.name, lowerSearch) -
fuzzyAlgorithm(window.coins[acc2.coin].coinfig.name, lowerSearch)
)

return { accounts: sortedAccounts, ethAccount, formValues, isAtLeastTier1 }
}
Expand Down

0 comments on commit 977bab3

Please sign in to comment.