Skip to content

Commit b8b226b

Browse files
jiexiGudahttadonesky1
authored
feat: add isEqualCaseInsensitive() to @metamask/controller-utils (#4811)
## Explanation Moves `isEqualCaseInsensitive()` from assets-controllers into controller-utils because it will be used elsewhere downstream ## References Upstream: None. This is the start. Downstream: #4812 ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/controller-utils` - **ADDED**: Export `isEqualCaseInsensitive()` helper which performs case insensitive comparison against two strings and returns true if they are equivalent. ## Checklist - [X] I've updated the test suite for new or updated code as appropriate - [X] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [X] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [X] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes --------- Co-authored-by: Mark Stacey <markjstacey@gmail.com> Co-authored-by: Alex Donesky <adonesky@gmail.com>
1 parent d75a971 commit b8b226b

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ChainId,
1515
ERC20,
1616
safelyExecute,
17+
isEqualCaseInsensitive,
1718
} from '@metamask/controller-utils';
1819
import type {
1920
KeyringControllerGetStateAction,
@@ -54,26 +55,6 @@ import type {
5455

5556
const DEFAULT_INTERVAL = 180000;
5657

57-
/**
58-
* Compare 2 given strings and return boolean
59-
* eg: "foo" and "FOO" => true
60-
* eg: "foo" and "bar" => false
61-
* eg: "foo" and 123 => false
62-
*
63-
* @param value1 - first string to compare
64-
* @param value2 - first string to compare
65-
* @returns true if 2 strings are identical when they are lowercase
66-
*/
67-
export function isEqualCaseInsensitive(
68-
value1: string,
69-
value2: string,
70-
): boolean {
71-
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
72-
return false;
73-
}
74-
return value1.toLowerCase() === value2.toLowerCase();
75-
}
76-
7758
type LegacyToken = {
7859
name: string;
7960
logo: `${string}.svg`;

packages/controller-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
toChecksumHexAddress,
2828
toHex,
2929
weiHexToGweiDec,
30+
isEqualCaseInsensitive,
3031
} from './util';
3132
export * from './types';
3233
export * from './siwe';

packages/controller-utils/src/util.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,33 @@ describe('util', () => {
611611
});
612612
});
613613
});
614+
615+
describe('isEqualCaseInsensitive', () => {
616+
it('returns false for non-string values', () => {
617+
// @ts-expect-error Invalid type for testing purposes
618+
expect(util.isEqualCaseInsensitive(null, null)).toBe(false);
619+
// @ts-expect-error Invalid type for testing purposes
620+
expect(util.isEqualCaseInsensitive(5, 5)).toBe(false);
621+
// @ts-expect-error Invalid type for testing purposes
622+
expect(util.isEqualCaseInsensitive(null, 'test')).toBe(false);
623+
// @ts-expect-error Invalid type for testing purposes
624+
expect(util.isEqualCaseInsensitive('test', null)).toBe(false);
625+
// @ts-expect-error Invalid type for testing purposes
626+
expect(util.isEqualCaseInsensitive(5, 'test')).toBe(false);
627+
// @ts-expect-error Invalid type for testing purposes
628+
expect(util.isEqualCaseInsensitive('test', 5)).toBe(false);
629+
});
630+
631+
it('returns false for strings that are not equal', () => {
632+
expect(util.isEqualCaseInsensitive('test', 'test1')).toBe(false);
633+
expect(util.isEqualCaseInsensitive('test1', 'test')).toBe(false);
634+
});
635+
636+
it('returns true for strings that are equal', () => {
637+
expect(util.isEqualCaseInsensitive('test', 'TEST')).toBe(true);
638+
expect(util.isEqualCaseInsensitive('test', 'test')).toBe(true);
639+
expect(util.isEqualCaseInsensitive('TEST', 'TEST')).toBe(true);
640+
expect(util.isEqualCaseInsensitive('test', 'Test')).toBe(true);
641+
expect(util.isEqualCaseInsensitive('Test', 'test')).toBe(true);
642+
});
643+
});

packages/controller-utils/src/util.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,20 @@ function logOrRethrowError(error: unknown, codesToCatch: number[] = []) {
619619
throw error;
620620
}
621621
}
622+
623+
/**
624+
* Checks if two strings are equal, ignoring case.
625+
*
626+
* @param value1 - The first string to compare.
627+
* @param value2 - The second string to compare.
628+
* @returns `true` if the strings are equal, ignoring case; otherwise, `false`.
629+
*/
630+
export function isEqualCaseInsensitive(
631+
value1: string,
632+
value2: string,
633+
): boolean {
634+
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
635+
return false;
636+
}
637+
return value1.toLowerCase() === value2.toLowerCase();
638+
}

0 commit comments

Comments
 (0)