Skip to content

Commit

Permalink
Add isEqualCaseInsensitive to controller-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Oct 17, 2024
1 parent 8fb04fc commit 644f4d0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 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();
}

0 comments on commit 644f4d0

Please sign in to comment.