Skip to content

Commit

Permalink
Move checkExistingAddresses to helpers/utils/util and stop its use of…
Browse files Browse the repository at this point in the history
… ramda
  • Loading branch information
danjm committed Jul 1, 2020
1 parent 14fa80c commit fbd5d07
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
20 changes: 20 additions & 0 deletions ui/app/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions ui/app/helpers/utils/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
2 changes: 1 addition & 1 deletion ui/app/pages/add-token/add-token.component.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion ui/app/pages/add-token/token-list/token-list.component.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
13 changes: 0 additions & 13 deletions ui/app/pages/add-token/util.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit fbd5d07

Please sign in to comment.