diff --git a/ui/app/helpers/utils/util.js b/ui/app/helpers/utils/util.js index cc1aba75c..37f852530 100644 --- a/ui/app/helpers/utils/util.js +++ b/ui/app/helpers/utils/util.js @@ -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) +} diff --git a/ui/app/helpers/utils/util.test.js b/ui/app/helpers/utils/util.test.js index 4647b3652..4be1713e9 100644 --- a/ui/app/helpers/utils/util.test.js +++ b/ui/app/helpers/utils/util.test.js @@ -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) + }) + }) }) diff --git a/ui/app/pages/add-token/add-token.component.js b/ui/app/pages/add-token/add-token.component.js index f2d033212..158f96a92 100644 --- a/ui/app/pages/add-token/add-token.component.js +++ b/ui/app/pages/add-token/add-token.component.js @@ -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' diff --git a/ui/app/pages/add-token/token-list/token-list.component.js b/ui/app/pages/add-token/token-list/token-list.component.js index 8843daee5..633ffea9c 100644 --- a/ui/app/pages/add-token/token-list/token-list.component.js +++ b/ui/app/pages/add-token/token-list/token-list.component.js @@ -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 { diff --git a/ui/app/pages/add-token/util.js b/ui/app/pages/add-token/util.js deleted file mode 100644 index b0f3bbd4c..000000000 --- a/ui/app/pages/add-token/util.js +++ /dev/null @@ -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) -} diff --git a/ui/app/pages/send/send-content/add-recipient/add-recipient.js b/ui/app/pages/send/send-content/add-recipient/add-recipient.js index b170738e4..a5407ece0 100644 --- a/ui/app/pages/send/send-content/add-recipient/add-recipient.js +++ b/ui/app/pages/send/send-content/add-recipient/add-recipient.js @@ -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'