Skip to content

Commit

Permalink
fix: fix util fct
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Sep 19, 2023
1 parent 931338b commit bd90602
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ui/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,27 @@ export const isAbleToExportAccount = (keyringType = '') => {
/**
* Checks if a tokenId in Hex or decimal format already exists in an object.
*
* @param {*} address - collection address.
* @param {*} tokenId - tokenId to search for
* @param {string} address - collection address.
* @param {string} tokenId - tokenId to search for
* @param {*} obj - object to look into
* @returns {boolean} `false` if tokenId does not already exist.
*/
export const checkTokenIdExists = (address, tokenId, obj) => {
// check if input tokenId is hexadecimal
// If it is convert to decimal and compare with existing tokens
// if it is decimal convert to hexadecimal and compare
// if it is neither leave as it is
const isHex = isStrictHexString(tokenId);
let convertedTokenId;
if (isHex) {
// Convert to decimal
convertedTokenId = hexToDecimal(tokenId);
} else {
// eslint-disable-next-line no-negated-condition
} else if (!isNaN(tokenId)) {
// Convert to hex
convertedTokenId = `0x${decimalToHex(tokenId)}`;
} else {
convertedTokenId = tokenId;
}

if (obj[address]) {
Expand Down
23 changes: 23 additions & 0 deletions ui/helpers/utils/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,19 @@ describe('util', () => {
},
],
},
'0xf4910C763eD4e47A585E2D34baA9A4b611aE448C': {
collectionName: 'foo',
nfts: [
{
address: '0xf4910C763eD4e47A585E2D34baA9A4b611aE448C',
description: 'foo',
favorite: false,
name: 'toto#111486581076844052489180254627234340268504869259922513413248833349282110111749',
tokenId:
'111486581076844052489180254627234340268504869259922513413248833349282110111749',
},
],
},
};
it('should return true if it exists', () => {
expect(
Expand All @@ -977,6 +990,16 @@ describe('util', () => {
).toBeTruthy();
});

it('should return true if is exists but input is not decimal nor hex', () => {
expect(
util.checkTokenIdExists(
'0xf4910C763eD4e47A585E2D34baA9A4b611aE448C',
'111486581076844052489180254627234340268504869259922513413248833349282110111749',
data,
),
).toBeTruthy();
});

it('should return false if it does not exists', () => {
expect(
util.checkTokenIdExists(
Expand Down

0 comments on commit bd90602

Please sign in to comment.