Skip to content

Commit

Permalink
Add getFileExt utility and test
Browse files Browse the repository at this point in the history
  • Loading branch information
rickycodes committed Jul 30, 2021
1 parent 5d7c648 commit b90f0e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/assets/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
GetTokenListState,
TokenListMap,
IconPath,
MediaExtType,
ContractMap,
getFileExt,
} from './TokenListController';

const name = 'TokenListController';
Expand All @@ -26,10 +26,7 @@ for (const tokenAddress in contractMap) {
const { erc20, logo: filePath, ...token } = (contractMap as ContractMap)[
tokenAddress
];
const extType = filePath
.substr(filePath.lastIndexOf('.') + 1)
.toUpperCase() as MediaExtType;
const iconPath: IconPath = { filePath, type: extType };
const iconPath: IconPath = { filePath, type: getFileExt(filePath) };
if (erc20) {
staticTokenList[tokenAddress] = {
...token,
Expand Down Expand Up @@ -1146,4 +1143,9 @@ describe('TokenListController', () => {

controller.destroy();
});

it('should get the file extension', () => {
const { iconUrl } = sampleMainnetTokenList[0];
expect(getFileExt(iconUrl)).toBe('PNG');
});
});
8 changes: 4 additions & 4 deletions src/assets/TokenListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ const defaultState: TokenListState = {
tokensChainsCache: {},
};

export const getFileExt = (filePath: string): MediaExtType =>
filePath.substr(filePath.lastIndexOf('.') + 1).toUpperCase() as MediaExtType;

/**
* Controller that passively polls on a set interval for the list of tokens from metaswaps api
*/
Expand Down Expand Up @@ -230,10 +233,7 @@ export class TokenListController extends BaseController<
const { erc20, logo: filePath, ...token } = (contractMap as ContractMap)[
tokenAddress
];
const extType = filePath
.substr(filePath.lastIndexOf('.') + 1)
.toUpperCase() as MediaExtType;
const iconPath: IconPath = { filePath, type: extType };
const iconPath: IconPath = { filePath, type: getFileExt(filePath) };
if (erc20) {
// Specify iconUrl here as filePath for backwards compatibility for extension
tokenList[tokenAddress] = {
Expand Down

0 comments on commit b90f0e2

Please sign in to comment.