Skip to content

Commit

Permalink
small dry todo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Oct 15, 2024
1 parent 597e837 commit 1872188
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
26 changes: 5 additions & 21 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import type {
ControllerStateChangeEvent,
} from '@metamask/base-controller';
import contractMap from '@metamask/contract-metadata';
import { ChainId, safelyExecute } from '@metamask/controller-utils';
import {
ChainId,
safelyExecute,
isEqualCaseInsensitive,
} from '@metamask/controller-utils';
import type {
KeyringControllerGetStateAction,
KeyringControllerLockEvent,
Expand Down Expand Up @@ -44,26 +48,6 @@ import type {

const DEFAULT_INTERVAL = 180000;

/**
* Compare 2 given strings and return boolean
* eg: "foo" and "FOO" => true
* eg: "foo" and "bar" => false
* eg: "foo" and 123 => false
*
* @param value1 - first string to compare
* @param value2 - first string to compare
* @returns true if 2 strings are identical when they are lowercase
*/
export function isEqualCaseInsensitive(
value1: string,
value2: string,
): boolean {
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
return false;
}
return value1.toLowerCase() === value2.toLowerCase();
}

type LegacyToken = {
name: string;
logo: `${string}.svg`;
Expand Down
1 change: 1 addition & 0 deletions packages/controller-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
toChecksumHexAddress,
toHex,
weiHexToGweiDec,
isEqualCaseInsensitive,
} from './util';
export * from './types';
export * from './siwe';
26 changes: 26 additions & 0 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,29 @@ describe('util', () => {
});
});
});

describe('isEqualCaseInsensitive', () => {
it('returns false for non-string values', () => {
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive(null, 'test')).toBe(false);
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive('test', null)).toBe(false);
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive(5, 'test')).toBe(false);
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive('test', 5)).toBe(false);
});

it('returns false for strings that are not equal', () => {
expect(util.isEqualCaseInsensitive('test', 'test1')).toBe(false);
expect(util.isEqualCaseInsensitive('test1', 'test')).toBe(false);
});

it('returns true for strings that are equal', () => {
expect(util.isEqualCaseInsensitive('test', 'TEST')).toBe(true);
expect(util.isEqualCaseInsensitive('test', 'test')).toBe(true);
expect(util.isEqualCaseInsensitive('TEST', 'TEST')).toBe(true);
expect(util.isEqualCaseInsensitive('test', 'Test')).toBe(true);
expect(util.isEqualCaseInsensitive('Test', 'test')).toBe(true);
});
});
17 changes: 17 additions & 0 deletions packages/controller-utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,20 @@ function logOrRethrowError(error: unknown, codesToCatch: number[] = []) {
throw error;
}
}

/**
* Checks if two strings are equal, ignoring case.
*
* @param value1 - The first string to compare.
* @param value2 - The second string to compare.
* @returns `true` if the strings are equal, ignoring case; otherwise, `false`.
*/
export function isEqualCaseInsensitive(
value1: string,
value2: string,
): boolean {
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
return false;
}
return value1.toLowerCase() === value2.toLowerCase();
}
17 changes: 1 addition & 16 deletions packages/multichain/src/scope/supported.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toHex } from '@metamask/controller-utils';
import { toHex, isEqualCaseInsensitive } from '@metamask/controller-utils';
import type { CaipAccountId, Hex } from '@metamask/utils';
import {
isCaipChainId,
Expand All @@ -17,21 +17,6 @@ import {
parseScopeString,
} from './types';

// TODO Maybe this gets DRY'ed into utils?.. It's used in TokenDetectionController too
/**
* Checks if two strings are equal, ignoring case.
*
* @param value1 - The first string to compare.
* @param value2 - The second string to compare.
* @returns `true` if the strings are equal, ignoring case; otherwise, `false`.
*/
function isEqualCaseInsensitive(value1: string, value2: string): boolean {
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
return false;
}
return value1.toLowerCase() === value2.toLowerCase();
}

export const isSupportedScopeString = (
scopeString: string,
isChainIdSupported: (chainId: Hex) => boolean,
Expand Down

0 comments on commit 1872188

Please sign in to comment.