1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-22 09:23:21 +01:00

feat(mme-17212): convert shared/module/string-utils.js -> Typescript (#17320)

This commit is contained in:
Danica Shen 2023-01-20 17:40:48 +00:00 committed by GitHub
parent 92f6ea6f6b
commit b310c6dcca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 10 deletions

View File

@ -1,10 +0,0 @@
export function isEqualCaseInsensitive(value1, value2) {
if (typeof value1 !== 'string' || typeof value2 !== 'string') {
return false;
}
return value1.toLowerCase() === value2.toLowerCase();
}
export function prependZero(num, maxLength) {
return num.toString().padStart(maxLength, '0');
}

View File

@ -0,0 +1,27 @@
import { isEqualCaseInsensitive, prependZero } from './string-utils';
describe('string-utils', () => {
describe('isEqualCaseInsensitive', () => {
it('should return true for FOO and foo', () => {
expect(isEqualCaseInsensitive('FOO', 'foo')).toBeTruthy();
});
it('should return false for foo and Bar', () => {
expect(isEqualCaseInsensitive('foo', 'Bar')).toBeFalsy();
});
it('should return false for number and string comparision', () => {
expect(isEqualCaseInsensitive('foo', 123)).toBeFalsy();
});
});
describe('prependZero', () => {
it('should return number to given max length string when digit is smaller than maxLength', () => {
expect(prependZero(123, 4)).toStrictEqual('0123');
});
it('should return number to given max length string when digit is large than maxLength', () => {
expect(prependZero(123, 2)).toStrictEqual('123');
});
});
});

View File

@ -0,0 +1,29 @@
/**
* 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();
}
/**
* Takes a number with max length until the resulting string reaches the given length
*
* @param num
* @param maxLength
*/
export function prependZero(num: number, maxLength: number): string {
return num.toString().padStart(maxLength, '0');
}