Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olivbau committed Dec 21, 2024
1 parent d0720c7 commit 8609597
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/helpers/defaultTransformToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { uniformTokenAddress } from '@sonarwatch/portfolio-core';
import { Token } from '../types';
import { isUri } from './isUri';

function isLowerCase(str: string): boolean {
return str === str.toLowerCase();
Expand Down Expand Up @@ -30,6 +31,7 @@ export async function defaultTransformToken(token: Token): Promise<Token> {
symbol,
name,
address: uniformTokenAddress(token.address, token.networkId),
logoURI: isUri(token.logoURI) ? token.logoURI : undefined,
};

return nToken;
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/isUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const NOT_URI_FRAGMENT = /\/|:/;
const URI =
/^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;

export function isUri(str?: string): boolean {
if (!str) return false;
// http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
return NOT_URI_FRAGMENT.test(str) && URI.test(str);
}
11 changes: 11 additions & 0 deletions tests/isUri.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isUri } from '../src/helpers/isUri';

describe('isUri', () => {
it('sould be an uri', async () => {
expect(isUri('https://domain.com')).toBeTruthy();
});

it('sould not be an uri', async () => {
expect(isUri('domain.com')).toBeFalsy();
});
});

0 comments on commit 8609597

Please sign in to comment.