Skip to content

Commit

Permalink
feat: add isEqualCaseInsensitive() to @metamask/controller-utils (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
Co-authored-by: Alex Donesky <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent d75a971 commit b8b226b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
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(
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);
// @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();
}

0 comments on commit b8b226b

Please sign in to comment.