1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-22 09:23:21 +01:00

Move checkExistingAddresses to helpers/utils/util and stop its use of ramda (#8868)

This commit is contained in:
Dan J Miller 2020-07-07 19:48:18 -02:30 committed by GitHub
parent 429af23ea0
commit ac525572eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 18 deletions

View File

@ -333,3 +333,23 @@ export function isExtensionUrl (urlLike) {
}
return false
}
/**
* Checks whether an address is in a passed list of objects with address properties. The check is performed on the
* lowercased version of the addresses.
*
* @param {string} address - The hex address to check
* @param {array} list - The array of objects to check
* @returns {boolean} Whether or not the address is in the list
*/
export function checkExistingAddresses (address, list = []) {
if (!address) {
return false
}
const matchesAddress = (obj) => {
return obj.address.toLowerCase() === address.toLowerCase()
}
return list.some(matchesAddress)
}

View File

@ -319,4 +319,33 @@ describe('util', function () {
})
})
})
describe('checkExistingAddresses', function () {
const tokenList = [
{ address: 'A' },
{ address: 'n' },
{ address: 'Q' },
{ address: 'z' },
]
it('should return true when a lowercase address matches an uppercase address in the passed list', function () {
assert(util.checkExistingAddresses('q', tokenList) === true)
})
it('should return true when an uppercase address matches a lowercase address in the passed list', function () {
assert(util.checkExistingAddresses('N', tokenList) === true)
})
it('should return true when a lowercase address matches a lowercase address in the passed list', function () {
assert(util.checkExistingAddresses('z', tokenList) === true)
})
it('should return true when an uppercase address matches an uppercase address in the passed list', function () {
assert(util.checkExistingAddresses('Q', tokenList) === true)
})
it('should return false when the passed address is not in the passed list', function () {
assert(util.checkExistingAddresses('b', tokenList) === false)
})
})
})

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ethUtil from 'ethereumjs-util'
import { checkExistingAddresses } from './util'
import { checkExistingAddresses } from '../../helpers/utils/util'
import { tokenInfoGetter } from '../../helpers/utils/token-util'
import { CONFIRM_ADD_TOKEN_ROUTE } from '../../helpers/constants/routes'
import TextField from '../../components/ui/text-field'

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import { checkExistingAddresses } from '../util'
import { checkExistingAddresses } from '../../../helpers/utils/util'
import TokenListPlaceholder from './token-list-placeholder'
export default class InfoBox extends Component {

View File

@ -1,13 +0,0 @@
import R from 'ramda'
export function checkExistingAddresses (address, tokenList = []) {
if (!address) {
return false
}
const matchesAddress = (existingToken) => {
return existingToken.address.toLowerCase() === address.toLowerCase()
}
return R.any(matchesAddress)(tokenList)
}

View File

@ -5,9 +5,7 @@ import {
INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR,
} from '../../send.constants'
import { isValidAddress, isEthNetwork } from '../../../../helpers/utils/util'
import { checkExistingAddresses } from '../../../add-token/util'
import { isValidAddress, isEthNetwork, checkExistingAddresses } from '../../../../helpers/utils/util'
import ethUtil from 'ethereumjs-util'
import contractMap from 'eth-contract-metadata'