Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isEqualCaseInsensitive() to @metamask/controller-utils #4811

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ChainId,
ERC20,
safelyExecute,
isEqualCaseInsensitive,
} from '@metamask/controller-utils';
import type {
KeyringControllerGetStateAction,
Expand Down Expand Up @@ -54,26 +55,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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this is not exported outside of the assets-controller package, and therefore does not require a changelog item

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';
30 changes: 30 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,33 @@ describe('util', () => {
});
});
});

describe('isEqualCaseInsensitive', () => {
it('returns false for non-string values', () => {
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive(null, null)).toBe(false);
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive(5, 5)).toBe(false);
// @ts-expect-error Invalid type for testing purposes
expect(util.isEqualCaseInsensitive(null, 'test')).toBe(false);
jiexi marked this conversation as resolved.
Show resolved Hide resolved
// @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();
}
Loading